Download Python Cheat Sheet: Data Types, Lists, Dictionaries, Control Flow and more Schemes and Mind Maps Web Programming and Technologies in PDF only on Docsity! Illinois Python Cheat Sheet by Elizabeth de Sa e Silva, Tamara Nelson-Fromm, Wade Fagen-Ulmschneider Basic Data Types Integers are whole numbers int1 = 8 int2 = -5 int3 = 0 int4 = int(4.0) Floats have a decimal point float1 = 5.5 float2 = 0.0 float3 = 1e6 float4 = float(2) Strings A string literal has quotes: ‘CS101’, ‘CS107’, ‘5.67’ (it’s literally the exact characters of the string) A variable name does not: course_name, stat107, my_string A string can be indexed the same way as a list Example my_string = ‘literal’ #’literal’ is the literal print(‘my_string’) #prints “my_string” print(my_string) #prints “literal” print(literal) #ERROR Slicing Strings, lists, and other iterable data types (data with many elements) can be indexed over a range of values, or sliced Replace any [i] with a range to select many elements at once: [start:stop:step] Selects position start through position stop, not including stop, but only elements step positions apart; start defaults to zero, so [ :10:7 ] starts at 0 stop defaults to one past the last index, so [ 10: :2 ] selects through the end of the data step defaults to one, so [ 1:5 ] steps by 1 (a negative step will count backwards) Examples my_string = ‘abcdefghijk’ my_string[2:4] == ‘cd’ my_string[:5] == ‘abcde’ my_string[5:] == ‘fghijk’ my_string[:] == ‘abcdefghijk’ my_string[2:8:2] == ‘ceg’ my_string[8:2:-2] == ‘ige’ Lists Creating a new list empty_list = [] my_list =[1,2,3] Adding to a list (appending) list_name.append(v) #adds just the #element v to #list_name list_name += [v1,v2] #adds v1 and v2 #to the end of #list_name Indexing list[i] is equal to the element in list at zero-based index i Negative index values count from the end of the data list[-i] is equal to list[ len(list) - i ] Changing a list #changes the element list[i] = v #in list at position #i to the value v Example my_list = [10,20,30] #my_list is declared as [10,20,30] my_list.append(40) #my_list becomes [10,20,30,40] my_list += [50,60] #my_list becomes [10,20,30,40,50,60] my_list[2] == 30 # True my_list[4] = “fifty” #my_list becomes [1,2,3,4,”fifty”,60] my_list[-1] == “fifty” # True my_list[60] #ERROR Booleans Booleans are True or False values x == y Is True if x is equal to y x in y is True if x is an element of y not x == y Is True is x is not equal to y And True and True = True True and False = False False and False = False Or True or True = True True or False = True False or False = False Dictionaries Creating a new dictionary my_dict = {key1:value1, key2:value2, …, keyn:valuen} empty_dict = {} #keys and values can be any data type Adding to a dictionary (appending) dict_name[key] = value #adds key:value to dict_name Indexing dict[key] is equal to the value in dict with key key Changing a dictionary dict_name[key] = value #changes key’s value to v so dict_name # now has the pair key:v Getting Keys and Values dict_name.keys() #returns a list of keys in dict_name dict_name.values() #returns a list of values in dict_name Example my_dict = {‘a’:5, ‘b’:6} #my_dict is declared as {‘a’:5,’b’:6} my_dict[‘c’] = ‘4’ #my_dict becomes {‘a’:5, 6:’b’, ‘c’:’4’} my_dict[‘a’] == 5 # True my_dict[‘b’] = ‘a’ #my_dict becomes {‘a’:5,‘b’:’a’,‘c’:’4’} my_dict[5] #ERROR my_dict.keys() #equal to [‘a’, ‘b’, ‘c’]