return a random integer between min and max inclusive
randint(min, max)
return a random integer between min and max-1 inclusive
randrange(min, max)
add a value to the end of a list
my_list.append(value)
remove the element at the index i from a list
my_list.pop(i)
remove the first element whose value matches
my_list.remove(‘abc’)
get the length of a list # of items in the list or in a set
len(list) len(set)
produce a new list by adding two lists together, order of definition is order in list
list1 + list2
find the element in the list with the smallest value
min(list)
find the element in the list with the largest value
max(list)
find the sum of all list elements (numbers only)
sum(list)
find the index of the first matching value in the list
list.index(value)
count the number of occurrences in a list
list.count(value)
add elements from one set to another set
add elements from one dictionary to another dictionary
set1.update(set2)
my_dict1.update(my_dict2)
add a value into the set
set.add(value)
remove value from a set. raises KeyError if not found
set.remove(value)
remove a random element from the set
set.pop()
clear all elements from a set
set.clear()
return a new set containing only the elements in common between set and provided sets
set.intersection(set_a, set_b, set_c)
return a new set containing all of the unique elements in all sets
set.union(set_a, set_b, set_c)
return a set containing only the elements of the set that are not found in any of the provided sets
set.difference(set_a, set_b, set_c)
return a set containing only the elements that appear in exactly one of set_a or set_b
set_a.symmetric_difference(set_b)
Adding new entries to a dictionary:
dict[k] = v: Adds the new key-value pair k-v, if dict[k] does not already exist.
Example: students[‘John’] = ‘A+’
Modifying existing entries in a dictionary:
dict[k] = v: Updates the existing entry dict[k], if dict[k] already exists.
Example: students[‘Jessica’] = ‘A+’
Removing entries from a dictionary:
del dict[k]: Deletes the entry dict[k].
Example: del students[‘Rachel’]