Professional Documents
Culture Documents
my_set = set()
my_set = {1, 2, 3, 4}
2 in my_set
#
#
#
empty set
create a set
True
SET Common numeric operations
LIST
my_list = [] # empty list Strings
my_list = [1, 2, 3, 1, 2] # create a list
my_list[0] = 2 # change a value x = "hi" # create a string
my_list.append(3) # append the value 3 "hi" + "!" # "hi!"
[1, 12, 3, -5, 12].count(12) # count the number of occurrences (result: 2) "a" * 4 # "aaaa"
len(my_list) # number of items in the list "a b c".split() # ["a", "b", "c"]
my_list.clear() # clear the list "hi".upper() # "HI"
"Hi".lower() # "hi"
4 in my_list # True if the item is in the list, False otherwise
"hi".capitalize() # "Hi"
del my_list[3] # remove item at index 3
" x ".strip() # "x"
my_list.index(5) # index of the first occurrence of 5 "hi".startswith("h") # True
# raises a ValueError if the item is not in the list "hi".endswith("x") # False
[1, 2, 3] + [4, 5, 6] # concatenate two lists (result: [1, 2, 3, 4, 5, 6]) "hi".replace("i", "o") # "ho"
Boolean operations
TUPLE
my_tuple = () # empty tuple x or y # if x is false, then y, else x
my_tuple = (1, 2, 3) # create a tuple x and y # if x is false, then x, else y
a, b, c = my_tuple # unpack the tuple not x # if x is false, then True, else False
my_tuple.count(1) # number of 1s in the tuple
Comparisons
my_tuple[1] # 2
< # strictly less than
my_tuple.index(2) # index of the value 2
<= # less than or equal
len(my_tuple) # length of the tuple
> # strictly greater than
a, b = b, a # using a tuple to swap two values >= # greater than or equal
(1, 2) + (3, 4) # (1, 2, 3, 4) == # equal
(1, 2) * 3 # (1, 2, 1, 2, 1, 2) != # not equal version 1.2
3 in (1, 2, 3) # True is # object identity (c) ArjanCodes 2022
is not # negated object identity www.arjancodes.com