PROGRAMMING:
PYTHON
SUMMARY +
ASSIGNMENTS
@ECOsummaries
→ 20% discount
1
, Introduction to python
Python basics
Simple calculations.
E.g. print(7 + 10), , 45 * 234, print(346 * 345)
Harder calculations.
E.g. 4 ** 2 = 16, 18 % 7 = 4 → (2 rest 4)
Comments (use #)
E.g. # division, # addition
Creating variable
E.g. x = 5, savings = 100
Integer (int) = number without a fractional part e.g. 100
Floating (float) = number with a fractional part e.g. 100.5
String (str) = text e.g. “compound” or ‘compound’
Boolean (bool) = True or False e.g. profit = True, profit = False
type(a) = gives the type of a e.g. type(a) = float, type(a) = bool
Pi_float = float(pi_string) → converting from string to float
Pi_bool = bool(pi_float) → converting from float to bool
Python lists
Lists
E.g. kitchen = a, hall = b etc. → areas = [a, b, c, d]
Strings in lists
E.g. liv = 10, bat = 20 → areas = [‘living room’, liv, ‘bathroom’, bat] → living room 10, bathroom
20
Sublists
E.g. house = [[‘hallway’, hall], [‘kitchen’, kit], [‘living room’, liv]]
house[1] gives [‘kitchen’, kit], since it is 1 department of the big list house.
! always use a comma if it’s not the end !
indexing
e.g. fam = [‘liz, 1.73, ‘emma’, 1.68, ‘mom’ , 1.71]
fam[4] = ‘mom’ ! zero indexing !
fam [-1] = 1.71 ! negative indexing !
Slicing
e.g. fam = [‘liz, 1.73, ‘emma’, 1.68, ‘mom’ , 1.71]
fam[3:5] = [1.68, ‘mom’] ! 3 is inclusive, but 5 is exclusive !
fam[:3] = [‘liz, 1.73, ‘emma’, 1.68]
fam[4:] = [‘mom, 1.71]
if house[-1] = [‘bathroom’, 9.5] → house[-1][1] = [9.5]
list manipulation - changing
e.g. fam = [‘liz, 1.73, ‘emma’, 1.68, ‘mom’ , 1.71]
fam[1] = 1.75, the list will change accordingly
fam[0:2] = [‘lisa’, 1.75], the list will change accordingly
list manipulation – adding
fam + [‘me’, 1.87] it will be added to the list
2