1Best Python Cheat Sheet PDF:
2https://websitesetup.org/wp-content/uploads/2020/04/Python-Cheat-Sheet.pdf
1print('Hello World') # print
2string_name = "This is a string" # string
3string_name[5:] # slicing returns "is a string"
4integer_name = 4 # integer
5float_name = 3.0 # float
6list_name = ['a','b','cats','dogs', 1, 2.0] # list
7list_name.append(3) # add to list
8# list comprehension filters for int types from the previous list
9list_comp = [x for x in list_name if type(x) is int]
10# dictionary
11dic = {"Bob":9766692343,"Alice":6123336678}
12dic["Alice"] # returns 6123336678
13dic_phone_numbers = dic.values() # returns list of values
14dic_names = dic.keys() # returns list of keys
15# class (cat object with 9 lives)
16class Cat:
17 def __init__(self,name):
18 self.name = name
19 self.lives = 9
20 print(f"{name} is a cat!")
21# recursive function (factorial example)
22def factorial(x):
23 if x == 1:
24 return 1
25 else:
26 return (x * factorial(x-1))
1Best python cheat sheet with examples and download pdf.
2https://hackeradda.com/python-cheat-sheet