Bubble sort
flamingo.height
if flamingos[0].height > flamingos[1].height:
print("flamingos[0] is taller")
swap
x = 5 y = 10 temp = x x = y y = temp print(x) print(y)
>>> 10
>>> 5
Assignment of flamingos
flamingos[0] = flamingos[1]
Number of rows in 2d-list
len(2d_list_name)
my_2d_list = [ [1,2,3,5], [2,4,8], [2,4,6,8], [4,3] ] print(len(my_2d_list))
>>> 4
Number of columns in 2d-list
len(2d_list_name[row])
my_2d_list = [ [1,2,3,5], [2,4,8], [2,4,6,8], [4,3] ] print(len(my_2d_list[1])) print(len(my_2d_list[3]))
>>> 3
>>> 2
None
my_var = None
Cloud()
clouds[1][0] = Cloud()
2d-list
my_2d_list = [ [1,2,3,4], [2,4,6,8], [3,6,9,12] ]
Items in a 2d-list
2d_list_namet[row][column]
my_2d_list = [ [9,2,3], [2,4,6], [3,6,9] ] my_2d_list[0][0] = 1 print(my_2d_list)
>>> [[1, 2, 3], [2, 4, 6], [3, 6, 9]]
remove
set_name.remove(item)
my_set = {"Annie", "John", "Sara", "David"}
my_set.remove("Sara")
print(my_set)
>>> set(['Annie', 'John', 'David'])
Tuple
pop
set_name.pop()
my_set = {"Annie", "John", "Sara", "David"}
my_set.pop()
print(len(my_set))
>>> 3
add
set_name.add(item)
my_set = {"Annie", "John", "Sara", "David"}
my_set.add("Daniel")
my_set.add("John")
print(my_set)
>>> set(['Annie', 'John', 'Sara', 'David', 'Daniel'])
Set
basket.balloon_colors
basket.balloon_colors
not in
string not in object
print("a" not in "banana")
print("x" not in "banana")
>>> False
>>> True
Add/change dictionary
dictionary[new_key] = new_value
my_dictionary = {"milk":2, "bread":1,"apple":4}
my_dictionary["apple"] = 6
my_dictionary["tomato"] = 5
print(my_dictionary)
>>> {'milk': 2, 'bread': 1, 'apple': 6, 'tomato': 5}
in
rat.get_passwords()
codes_and_passwords = rat.get_passwords()
Dictionary
dictionary_name = {key:value,key:value,..}
my_dictionary = {"milk":2, "bread":1,"apple":4}
print("How many apples should I buy?")
print(my_dictionary["apple"])
>>> How many apples should I buy?
>>> 4
concatenate
my_string = "a" + "b" print(my_string)
>>> ab
gate.open()
password = gate.code + "a" gate.open(password)
gate.code
code = gate.code
int
input
input(prompt)
answer = input("What is your name? ")
print(answer)
>>> What is your name? Alex
>>> Alex
Pigeon
snake.length = 1
drill.drill_right(number of times)
method
def method_name(self, paramerter_1,...):
# code
class Player:
def num_of_star_challenge_1(self):
challenge_1 = 3
print(challenge_1)
class
self
def method_name(self, paramerter_1,...):
# code
init
def __init__(self, paramerter_1,paramerter_2,...):
self.property_1. = paramerter_1
self.property_2. = paramerter_2
...
class Player:
def __init__(self):
self.num_solved = 0
Object of class
object_name = Class_name()
class Player:
def __init__(self):
self.num_solved = 0
daniel = Player()
drill.drill_right()
function
def function_name(paramerter_1, paramerter_2,...):
# code
def print_sum(a, b):
sum = a + b
print(sum)
print_sum(3, 1)
>>> 4
not
num = -10
if not num > 0:
print("The number is not positive")
>>> The number is not positive
well.crocodile.toggle()
well.crocodile.toggle()
well.crocodile.mouth_closed
well.crocodile.mouth_closed
well.crocodile
well.crocodile
or
num = 50
if num < 5 or num > 10:
print("The number is less than 5 or more than 10")
>>> The number is less than 5 or more than 10
obstacle.is_on_ground()
if obstacle.is_on_ground():
print("Obstacle is on the ground")
and
num = 5
if num > 0 and num < 10:
print("The number is between 1 and 9")
>>> The number is between 1 and 9
while loop
while condition:
# code while True
num = 0
while num < 4:
print(num)
num = num + 1
>>> 0
>>> 1
>>> 2
>>> 3
well.water_level
well.water_level
well.max_water_level
well.max_water_level
elephant.spray_at()
elephant.spray_at(well)
else
if condition:
# code when True
else:
# code when False
num = -10
if num > 0:
print("True")
else:
print("False")
>>> False
obstacle.is_fence()
if obstacle.is_fence():
dragon.smash(obstacle)
obstacle.is_box()
if obstacle.is_box():
dragon.smash(obstacle)
obstacle.is_ice()
if obstacle.is_ice():
dragon.fire_at(obstacle)
if
dragon.smash()
dragon.smash(obstacle)
dragon.fire_at()
dragon.fire_at(obstacle)
len
Variable
my_var_1 = 5 my_var_2 = "Python"
print("text to print")
my_list = [1, 2, 3, 4]
print(my_list)
>>> text to print
>>> [1, 2, 3, 4]
for with range
for index in range(start,stop,step):
for index in range(4):
print(index)
>>> 0
>>> 1
>>> 2
>>> 3
range
range(start,stop,step)
print(range(3,6)) print(range(5))
>>> [3, 4, 5]
>>> [0, 1, 2, 3, 4]
Indentation
for loop
for item in list_name:
my_list = ["a", "b", "c"]
for item in my_list:
print(item)
>>> a
>>> b
>>> c
Items in a list
List
whale.blow()
whale.blow(3)
snake.length
snake.length = 4
giraffe.height
giraffe.height = 5