ENGG1330
Computer Programming I
55% feel ready to work independently
45% still need support
97% correct
print(7 // 3, 7 % 3)70% correct
x = 5y = xx += 2print(y)| Lecture L-1 | 71.2% |
| Lecture L-2 | 92.1% |
| Tutorial T-1 | 96.2% |
ENGG1330
Computer Programming I
fruits = ('apple', 'orange', 'pear', 'banana')print(fruits)print(fruits[0])fruits
| 0 | 1 | 2 | 3 |
| apple | orange | pear | banana |
fruits[0]
| 0 | 1 | 2 | 3 |
| apple | orange | pear | banana |
fruits = ('apple', 'orange', 'pear', 'banana')s = 'apple'print(fruits[0])print(s[0])t = (3, 3.14159, 'Bruce Lee', True)print(type(t))print(type(t[0]))print(type(t[1]))t = ((1, 2), (2, 3), (3, 3))print(t)print(len(t))print(t[0])print(len(t[0]))t = 'Dirk Schnieders', 29, 'A-'name, age, grade = tprint('Name:', name)print('Age:', age)print('Grade:', grade)Unpack t into three names
| name | age | grade |
| Dirk Schnieders | 29 | A- |
name
| name | age | grade |
| Dirk Schnieders | 29 | A- |
age
| name | age | grade |
| Dirk Schnieders | 29 | A- |
grade
| name | age | grade |
| Dirk Schnieders | 29 | A- |
t1 = (1, 2, 3, 4, 5, 6)t2 = (7, 8, 9)t = t1 + t2print(t)fruits = ('kiwi', 'strawberry', 'apple', 'orange', 'pear', 'banana')print(fruits[1:3])print(fruits[:3])print(fruits[2:])fruits[1:3]
fruits[:3]
fruits[2:]
| 0 | 1 | 2 | 3 | 4 | 5 |
| kiwi | strawberry | apple | orange | pear | banana |
t = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)print(t[-5:])print(t[-3:])print(t[:-3])t[-5:]
t[-3:]
t[:-3]
| -10 | -9 | -8 | -7 | -6 | -5 | -4 | -3 | -2 | -1 |
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
t = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14)print(t[0:10])print(t[0:10:2])print(t[0::2])print(t[::2])t[0:10]
t[0:10:2]
t[0::2]
t[::2]
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |
ENGG1330
Computer Programming I
fruits = ['apple', 'orange', 'pear', 'banana']print(fruits[-2])print(fruits[-1])print(fruits[-3])print(len(fruits))fruits[-2]
| -4 | -3 | -2 | -1 |
| apple | orange | pear | banana |
fruits[-1]
| -4 | -3 | -2 | -1 |
| apple | orange | pear | banana |
fruits[-3]
| -4 | -3 | -2 | -1 |
| apple | orange | pear | banana |
len(fruits) = 4
| -4 | -3 | -2 | -1 |
| apple | orange | pear | banana |
fruits = ('apple', 'orange', 'pear', 'banana')fruits[-1] = 'pineapple'print(fruits)fruits = ['apple', 'orange', 'pear', 'banana']fruits[-1] = 'pineapple'print(fruits)a = ['apple', 'pear']b = ab[0] = 'kiwi'print('a:', a, 'b:', b)b = ['banana']print('a:', a, 'b:', b)b = a: both names refer to the same list
b[0] = 'kiwi': the shared list changes
b = ['banana']: only b refers to the new list
| Tuple | List (Default) |
|---|---|
| A fixed group of related values | A general collection of items |
(name, age, grade) | ['apple', 'orange'] |
| Prevents accidental element replacement | Add, remove, or replace items |
Tuples usually use less memory and can be dictionary keys
when all their elements are hashable
A variable can refer to a different tuple
fruits = []fruits.append('apple')fruits.append('orange')fruits.insert(1, 'pineapple')print(fruits)append('apple')
| 0 |
| apple |
append('orange')
| 0 | 1 |
| apple | orange |
insert(1, 'pineapple')
| 0 | 1 | 2 |
| apple | pineapple | orange |
fruits = ['apple', 'orange', 'pear', 'banana']fruits.insert(2, 'pineapple')print(fruits)del fruits[2]print(fruits)insert(2, 'pineapple')
| 0 | 1 | 2 | 3 | 4 |
| apple | orange | pineapple | pear | banana |
del fruits[2]: later items shift left
| 0 | 1 | 2 | 3 |
| apple | orange | pear | banana |
fruits = ['apple', 'orange', 'pear']help(fruits.pop)ENGG1330
Computer Programming I
fruit_by_code = {10: 'apple', 20: 'orange', 30: 'pear', 40: 'banana'}print(fruit_by_code[20])print(fruit_by_code[10])fruit_by_code[20]
| 10 | 20 | 30 | 40 |
| apple | orange | pear | banana |
fruit_by_code[10]
| 10 | 20 | 30 | 40 |
| apple | orange | pear | banana |
fruits = {'favourite': 'apple', 10: 'orange', 20: 'pear', 30: 'banana'}print(fruits['favourite'])print(fruits[10])fruits['favourite']
| 'favourite' | 10 | 20 | 30 |
| apple | orange | pear | banana |
fruits[10]
| 'favourite' | 10 | 20 | 30 |
| apple | orange | pear | banana |
mood = {'sunny': ':-)', 'cloudy': ':-|', 'rainy': ':-('}print(mood['sunny'])print(mood['rainy'])mood['sunny']
| 'sunny' | 'cloudy' | 'rainy' |
| :-) | :-| | :-( |
mood['rainy']
| 'sunny' | 'cloudy' | 'rainy' |
| :-) | :-| | :-( |
stock = {'apple': 5, 'bun': 3}print(stock['apple'])stock['apple'] -= 2print(stock['apple'])stock['apple'] = 5
| apple | bun |
| 5 | 3 |
stock['apple'] -= 2
| apple | bun |
| 3 | 3 |
ENGG1330
Computer Programming I
shapes = {'square', 'triangle', 'circle'}print('polygon' in shapes)print('polygon' not in shapes)shapes = set() # Create an empty setshapes.add('triangle')shapes.add('circle')shapes.add('triangle')print(shapes)shapes.add('polygon')print(shapes)add('triangle')
add('circle')
add('triangle') again: no duplicate
add('polygon')
shapes = {'square', 'triangle', 'circle', 'polygon'}shapes.remove('square')print(shapes)| Operation | Python | Meaning |
|---|---|---|
| Subset | s <= t | Every element of s is in t |
| Superset | s >= t | Every element of t is in s |
| Union | s | t | Elements from either set |
| Intersection | s & t | Elements common to both |
| Difference | s - t | Elements in s, but not t |
| Symmetric difference | s ^ t | Elements in exactly one set |
s1 = {0, 2, 4, 6, 8, 10, 12}s2 = {0, 3, 6, 9, 12}print(s1 & s2)Compare membership in the two sets
s1 & s2: present in both
ENGG1330
Computer Programming I
Body
The statements that repeat
Iteration
One repetition of the body
count = 0while count < 3: print(count) count += 1print('Done')count = 0while count < 3: print('8') count += 1answer = 0while answer != 4: answer = int(input('2 * 2 = '))print('Well done. Here is a cookie!')answer = 0count = 0while answer != 4: answer = int(input('2 * 2 = ')) count += 1print('Well done. Here is a cookie!')print('It took you', count, 'attempts.')i = 0while i < 2: j = 0 while j < 2: print(i, j) j += 1 i += 1The inner loop runs for each i
| i | j | Printed pair |
| 0 | 0 | 0 0 |
Advance j
| i | j | Printed pair |
| 0 | 1 | 0 1 |
Advance i, then reset j to 0
| i | j | Printed pair |
| 1 | 0 | 1 0 |
Advance j again
| i | j | Printed pair |
| 1 | 1 | 1 1 |
ENGG1330
Computer Programming I
primes = [2, 3, 5, 7]for prime in primes: print(prime)print('Done')scores = (4, 7, 3)total = 0for score in scores: total += score print(total)print('Final:', total)Start total at 0
| score | total |
| Not read yet | 0 |
0 + 4 = 4
| score | total |
| 4 | 4 |
4 + 7 = 11
| score | total |
| 7 | 11 |
11 + 3 = 14
| score | total |
| 3 | 14 |
The loop has finished
| Final total |
| 14 |
scores = (4, 7, 3, 9, 6)best = scores[0]for score in scores: if score > best: best = score print(score, best)Start with the first score
| score | best |
| Not read yet | 4 |
4 > 4 is false: keep best
| score | best |
| 4 | 4 |
7 > 4 is true: update best
| score | best |
| 7 | 7 |
3 > 7 is false: keep best
| score | best |
| 3 | 7 |
9 > 7 is true: update best
| score | best |
| 9 | 9 |
6 > 9 is false: keep best
| score | best |
| 6 | 9 |
for c in 'Dirk': print(c)mood = {'sunny': ':-)', 'cloudy': ':-|', 'rainy': ':-('}for key in mood: print('Key:', key, 'Value:', mood[key])for x in range(3, 8): print('->', x)range(3, 8): start included, stop excluded
| 3 | 4 | 5 | 6 | 7 | 8 |
| 3 | 4 | 5 | 6 | 7 | Excluded |
The first iteration
| 3 | 4 | 5 | 6 | 7 | 8 |
| 3 | 4 | 5 | 6 | 7 | Excluded |
The last iteration
| 3 | 4 | 5 | 6 | 7 | 8 |
| 3 | 4 | 5 | 6 | 7 | Excluded |
for i in range(2): for j in range(2): print(i, j)First outer value, first inner value
| i | j | Printed pair |
| 0 | 0 | 0 0 |
Advance the inner loop
| i | j | Printed pair |
| 0 | 1 | 0 1 |
The next outer value starts a new inner loop
| i | j | Printed pair |
| 1 | 0 | 1 0 |
Advance the inner loop again
| i | j | Printed pair |
| 1 | 1 | 1 1 |
for i in range(1, 5): if i % 2 == 0: print(i) else: print('odd')num = int(input('Number: '))for i in range(num, 0, -1): print(i)print('Blast off!')fruits = ('apple', 'orange', 'pear', 'banana')for i in range(len(fruits)): print(i, '=', fruits[i])i = 0: fruits[i] = 'apple'
| 0 | 1 | 2 | 3 |
| apple | orange | pear | banana |
i = 1: fruits[i] = 'orange'
| 0 | 1 | 2 | 3 |
| apple | orange | pear | banana |
i = 2: fruits[i] = 'pear'
| 0 | 1 | 2 | 3 |
| apple | orange | pear | banana |
i = 3: fruits[i] = 'banana'
| 0 | 1 | 2 | 3 |
| apple | orange | pear | banana |
fruits = ('apple', 'orange', 'pear', 'banana')for index, value in enumerate(fruits): print(index, '=', value)index = 0, value = 'apple'
| 0 | 1 | 2 | 3 |
| apple | orange | pear | banana |
index = 1, value = 'orange'
| 0 | 1 | 2 | 3 |
| apple | orange | pear | banana |
index = 2, value = 'pear'
| 0 | 1 | 2 | 3 |
| apple | orange | pear | banana |
index = 3, value = 'banana'
| 0 | 1 | 2 | 3 |
| apple | orange | pear | banana |
for val in 'string': if val == 'i': break print(val)print('Done')for val in 'string': if val == 'i': continue print(val)print('Done')L-3 Survey
Complete the full survey during the lecture
Coding Challenge
To Earn Your Participation Mark
Both required · 1 mark · No partial credit