# Python Zero to Heros

Online Absolute Beginner Python Tutorials

Every Sunday 1pm (UK time/ BST)

by Cheuk Ting Ho

Get this slide deck: bit.ly/PythonBasics2

# Recap

Python objects - int, float, str

Python functions - print, len, type, exit

Arithmatic Calculations

String concatination

f strings

"Hello"[1], "Hello"[4:]

# Boolean (bool)

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'

# List

[1, 2, 3], ['a', 'b', 'c'], [10, 'apples', 0.5], []

['happy']*3, ['cat'] + ['dog', 'hamster'], list[3]

Try this:

# List

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)

# List

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

# List

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))

# Dictionary (dict)

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())

# Dictionary (dict)

key of a dict need to be hashable

Try: {[1, 2]: [3, 4]}; {(1, 2): [3, 4]}

### Check if something is in a list or (key of) dict

list = [1, 2, 3]

1 in list; 10 in list

4 in how_to; 'goal' in how_to

# Let's write some programs

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

# Homework 📝

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

## Next week: Flow control (if-else, loop)

Sunday 1pm (UK time/ BST)

There are also Mid Meet Py every Wednesday 1pm

By Cheuk Ting Ho

• 378