Online Absolute Beginner Python Tutorials
Every Sunday 1pm (UK time/ BST)
by Cheuk Ting Ho
Get this slide deck: bit.ly/PyFlowControl
Python objects - int, float, str, list, dict, bool
Python functions - append, pop, remove, sum, sort, max, min, len, count
mutable vs hashable
>, >=, <, <=, ==, !=
in, is
does not work on None
Try this:
a = [1]
b = [1]
a == b
a is b
True, False
need to be capitalised: true, FALSE
logic operations: and, or, not
False includes 0 and '' while True are the rest
and: if 1st is False, 2nd Ignore
or: if 1st is True, 2nd Ignore
branching, which path to go
if, elif, else
if age < 18:
sorry_no_beer()
elif age < 25:
show_id()
else:
serve_beer()
Sometimes we have to advoid None
parent = "mother" if (gender=="female") else "father"
looping, doing something again and again
e.g. do the same thing for everthing is a list
my_friends = ["Sandrine", "Emanuil", "Enrica"]
for friend in my_friends:
print(f"{friend} is my firend.")
e.g. do things 10 times
count = 0
for _ in range(10):
count = count + 1
print(f"I have done {count} push-ups.")
range(stop) or range(start, stop[, step])
Try these:
for count in range(1, 11):
print(f"So far, I have done {count} push-ups.")
for count in range(2, 22, 2):
print(f"I have {count} shoes.")
A condence way of creating a list via looping.
Instead of doing this:
result = []
for num in range(10):
result.append(num*2)
print(result)
Do this:
result = [num *2 for num in range(10)]
print(result)
key-value pairs, {'a': 'apple', 'b': 'banana'}
how_to.keys(), how_to.values(), how_to.items()
For example:
how_to = {1: 'Open the door',
2:'Put the elephant in',
3:'Close the door'}
for key, value in how_to.items():
print(f"Step number {key}, {value}")
sq_num = {num: num**2 for num in range(10)}
print(sq_num)
Another way of doing something again and again base on the condition rather then through a sequence of things.
For example:
import random
basket = ["apple", "orange", "pear", "banana", "peach"]
while random.choice(basket) != "banana":
print("Try again")
print("I have got banana!")
break and continue (apply to for loop as well)
break: exit the loop
continue: ignore the rest and start next iteration
import random
basket = ["apple", "orange", "pear", "banana", "peach"]
while True: #DANGER
choice = random.choice(basket)
if choice == "banana":
print("I have got banana!")
break
print(f"I got {choice}, try again.")
What if I get everything but oranges?
1. I randomly pick a fruit from the baskets, I put anything I got in my shopping bag except oranges. I really need some bananas, don't stop till I got one. (random_fruit.py)
2. Tom and Matt both have a shopping list, we want to combine them and remove the redundant items, then calculate how much they spend in total at the end. (combine_checkout.py)
https://github.com/Cheukting/python02hero/tree/master/2020-04-19-python-control-flow
1. one_number_bingo.py: I can buy a ticket for £3 with one number assigned to me at random. It could be any number from 1 to 999. How much do I have to pay to win the game in one situation?
2. one_number_bingo.py (bonus): do Monte Carlo similation to get the average money sepnd instead)
https://github.com/Cheukting/python02hero/tree/master/2020-04-19-python-control-flow
Sunday 1pm (UK time/ BST)
There are also Mid Meet Py every Wednesday 1pm