Online Absolute Beginner Python Tutorials
Every Sunday 1pm (UK time/ BST)
by Cheuk Ting Ho
Get this slide deck: bit.ly/PythonBasics2
Python objects - int, float, str
Python functions - print, len, type, exit
Arithmatic Calculations
String concatination
f strings
"Hello"[1], "Hello"[4:]
True, False
need to be capitalised: true, FALSE
logic operations: and, or, not
False includes 0 and '' while True are the rest
Try this:
True and True; True and False; not True; True or False
bool(0); bool(''); bool('False'); bool(-1)
0 and 1; 1 and 1; 10 and 4; 3 and 4; 4 and 3; 4 and 0
'apple' and ''; '' and 'apple'; 'apple' and 'orange'
[1, 2, 3], ['a', 'b', 'c'], [10, 'apples', 0.5], []
['happy']*3, ['cat'] + ['dog', 'hamster'], list[3]
Try this:
basket1 = [10, 'apples']
basket2 = [12, 'bananas']
total = [basket1, basket2]; print(total)
print(basket2[0])
basket2[0] = 9; print(basket2, total)
mutable - can change the elements
hence not hashable (what does that mean)
A non-mutable, hashable cusion is Tuple - e.g (1, 2)
str is also a sequence that is hashable
list object elements are by reference so changes will populate
Try this:
row = [0] *3; print(row)
array = [row]*3; print(array)
array[0][0] = 1; print(array)
use copy.copy() if you want to make a brand new copy
Try this:
import copy; row = [0]*3
array = [copy.copy(row), copy.copy(row), copy.copy(row)]
array[0][0] = 1; print(array)
We have imported the copy module,
details about import modules will be covered in the future
append, pop, remove, sum, sort, max, min, len, count
Try this:
['apple'].append(['banana']); ['apple'].append('banana')
list = ['x', 'y', 'z']; list.pop(); print(list)
list = ['x', 'y', 'z']; list.remove('y'); print(list)
list = [1, 2, 3]; print(sum(list));
list = [1, 2, 3]; list.sort(reverse=True); print(list)
list = [1, 2, 3, 1, 2, 1, 2, 3, 2, 1]; print(list.count(1))
key-value pairs, {'a': 'apple', 'b': 'banana'}
Try this:
how_to =
{1: 'Open the door', 2:'Put the elephant in', 3:'Close the door'}
print(how_to[2])
how_to[1.5] = 'Take the giraffe out'; print(how_to)
how_to['goal'] = 'Put the elephant in the fridge'
print(how_to.keys()); print(how_to.values())
print(how_to.items())
key of a dict need to be hashable
Try: {[1, 2]: [3, 4]}; {(1, 2): [3, 4]}
list = [1, 2, 3]
1 in list; 10 in list
4 in how_to; 'goal' in how_to
1. creat a catalog (item and price) and a shopping list, print out the total I have to pay at checkout (shopping_checkout.py)
2. given a family tree, find if someone is someone's grand parent (family_checker.py)
https://github.com/Cheukting/python02hero/tree/master/2020-04-19-python-basics-2
1. There are 3 students who sit through the English and Math exams, print a one-liner report for each student and the average marks amoung them for each subject. (exam_report.py)
2. Tom and Matt both have a shopping list, we want to combine them and remove the redundant items, then count how may items they are buying at the end. (combine_shopping.py)
https://github.com/Cheukting/python02hero/tree/master/2020-04-19-python-basics-2
Sunday 1pm (UK time/ BST)
There are also Mid Meet Py every Wednesday 1pm