3+3
6
10 * 3
30
35 / 5
7.0
35 % 6
5
(1 + 3) * 2
8
1 + 3 * 2
7
True and False
False
False or True
True
True + True
2
True * 8
8
1 == 1
True
2 == 1
False
1 != 1
False
2 != 1
True
1 > 10
False
1 > 10 or 1 < 10
True
2 <= 2
True
Strings are an ordered collection of characters
'This is a string.'
'This is a string.'
"Hello " + "world!"
'Hello world!'
name = "oTree"
name[0]
'o'
name[-3]
'r'
li = []
li
[]
li = [2,3,4]
li
[2, 3, 4]
li.append(5)
li
[2, 3, 4, 5]
li[0]
2
len(li)
4
li [1:3]
[3, 4]
del li[2]
li
[2, 3, 5]
1 in li
False
empty_dict = {}
filled_dict = {"one": 1, "two": 2, "three": 3}
filled_dict["one"]
1
Get all keys as an iterable with "keys()". We need to wrap the call in list()
list(filled_dict.keys())
['one', 'two', 'three']
Get all values as an iterable with "values()". Once again we need to wrap it
list(filled_dict.values())
[1, 2, 3]
Check for existence of keys in a dictionary with "in"
"one" in filled_dict
True
1 in filled_dict
False
filled_dict.update({"four": 4})
some_var = 5
if some_var > 10:
print("some_var is totally bigger than 10.")
elif some_var < 10: # This elif clause is optional.
print("some_var is smaller than 10.")
else: # This is optional too.
print("some_var is indeed 10.")
some_var is smaller than 10.
for i in range(4):
print(i)
0 1 2 3
for animal in ["dog", "cat", "mouse"]:
# You can use format() to interpolate formatted strings
print("{} is a mammal".format(animal))
dog is a mammal cat is a mammal mouse is a mammal
numbers = [3, 4, 5, 6, 7]
[x for x in numbers if x > 5] # => [6, 7]
[6, 7]
Indenting is important!
def add(x, y):
print("x is {} and y is {}".format(x, y))
return x+y # Return values with a return statement
add(5, 6)
x is 5 and y is 6
11
Pokemon is a class with some properties
Two instances of the class →
Charmander:
Height: 2' 00";
Weight 18.7 lbs;
Category: Lizard
Bulbasaur:
Height: 2' 04";
Weight: 15.2 lbs;
Category: Seed
class Pokemon:
nature = "Pokemon" # this property is shared by all instances
def __init__(self, name, height, weight, category):
# Assign the argument to the instance's name attribute
self.name = name
self.height = height
self.weight = weight
self.category = category
self.comment = []
# to add a comment
def add_comment(self, comment):
self.comment.append(comment)
# convert height and weight into metric
def convert_metric(self):
# conversion rates
feet_conv_cm = 30.5
inch_conv_cm = 2.54
lbs_conv_kg = 0.453592
self.height_cm = int(self.height.split(
"'")[0])*feet_conv_cm+int(self.height.split("'")[1])*inch_conv_cm
self.weight_kg = self.weight*lbs_conv_kg
# print the result
print("Height (cm): %f, Weight (Kg): %f" %
(self.height_cm, self.weight_kg))
p_1 = Pokemon("Charmander", "2' 5''", 18.7, "Lizard")
p_2 = Pokemon("Bulbasaur", "2' 04''", 15.2, "Seed")
p_1.name
p_2.name
'Bulbasaur'
p_1.add_comment("Its ability is Blaze")
p_2.add_comment("Its ability is Overgrow")
p_1.convert_metric()
p_2.convert_metric()
Height (cm): 73.700000, Weight (Kg): 8.482170 Height (cm): 71.160000, Weight (Kg): 6.894598
if p_1.height > p_2.height:
print(p_1.name + " is taller")
elif p_1.height < p_2.height:
print(p_2.name + " is taller")
else:
print(p_2.name + " and" + p_1.name + "are equally taller")
Charmander is taller