8000 Updated session 8 slides and added multiple choice questions: · zfoxpython/intro-to-python@eed1bea · GitHub
[go: up one dir, main page]

Skip to content

Commit eed1bea

Browse files
committed
Updated session 8 slides and added multiple choice questions:
1 parent 9864d07 commit eed1bea

File tree

3 files changed

+84
-230
lines changed

3 files changed

+84
-230
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# KPMG:Code - Session 8 - Multiple Choice Questions
2+
3+
1. To open the below file, fill in the missing word:
4+
f = ?("my_file.txt", "r")
5+
- input
6+
- read
7+
- open - A
8+
- close
9+
10+
2. To read the below file, fill in the missing word:
11+
f = open("my_file.txt", "r")
12+
print(f.?())
13+
- input
14+
- read - A
15+
- open
16+
- close
17+
18+
3. What does the "a" mode stand for?
19+
- read
20+
- append - A
21+
- write
22+
- create
23+
24+
4. "w" mode, overwrites the contents of your file.
25+
- True - A
26+
- False
27+
28+
5. "a" mode, overwrites the contents of your file.
29+
30+
- True
31+
- False - A
32+
33+
6. "r" mode will fail if the file its passed to doesn't exist.
34+
35+
- True - A
36+
- False
37+
38+
7. To close the below file, fill in the missing word:
39+
f = open("example.txt", "w")
40+
f.write("Hello World")
41+
f.?()
42+
43+
- input
44+
- read
45+
- open
46+
- close - A
47+
48+
8. To create a new file, file in the missing mode:
49+
f = open("example.txt", "?")
50+
51+
- x - A
52+
- c
53+
- r
54+
- s
55+
56+
9. What will be the output of the below:
57+
58+
total = 0
59+
for x in open("example.txt"):
60+
total += 1
61+
62+
print(total)
63+
64+
- all the text from the file will be output
65+
- all the lines will be print out
66+
- the amount of lines in the file will be printed out - A
67+
- a new file will be created
68+
69+
10. What will be the output of the below:
70+
f = open("odd.txt", "w")
71+
72+
for x in open("numbers.txt"):
73+
x = int(x)
74+
if x % 2 == 1:
75+
f.write(str(x) + "\n")
76+
77+
f.close()
78+
79+
- all the even numbers will be written to numbers.txt
80+
- all the odd numbers will be written to numbers.txt
81+
- all the even numbers will be written to even.txt
82+
- all the odd numbers will be written to odd.txt - A

session_08/slides/answers_8.md

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,2 @@
1-
# [fit] IHF: Code
1+
# [fit] KPMG: Code
22
## [fit] Python — Session 8 — Answers
3-
### Live at 10am
4-
5-
---
6-
7-
## Any Questions
8-
### go to sli.do #python2020
9-
10-
---
11-
12-
# [fit] Next Session
13-
### Tuesday 7th July 10am
14-
### Lesson 9

session_08/slides/session_8.md

Lines changed: 1 addition & 217 deletions
Original file line numberDiff line numberDiff line change
@@ -1,217 +1,12 @@
1-
# [fit] IHF: Code
1+
# [fit] KPMG: Code
22
## [fit] Python — Session 8 — Lesson
3-
### Live at 10am
43

54
---
65

76
# Review
87

98
---
109

11-
# Modules
12-
13-
```python
14-
import random
15-
from math import floor
16-
```
17-
18-
---
19-
20-
# Random Module
21-
22-
```python
23-
import random
24-
25-
# Random float from 0.0 to 1.0
26-
print random.random()
27-
28-
# Gets a random number between 1 and 10
29-
number = random.randint(1, 10)
30-
```
31-
32-
---
33-
34-
# Math Module
35-
36-
```python
37-
from math import floor, ceil
38-
39-
number = floor(3.2) # 3
40-
print(floor(9.99)) # 9
41-
42-
number = ceil(3.2) # 4
43-
print(ceil(9.99)) # 10
44-
```
45-
46-
---
47-
48-
# For Loops
49-
50-
```python
51-
names = ["Alice", "Bob", "Charlie"]
52-
53-
for person in names:
54-
print(person)
55-
56-
# Alice
57-
# Bob
58-
# Charlie
59-
```
60-
61-
---
62-
63-
# While Loops
64-
65-
```python
66-
guess = None
67-
while guess != 4:
68-
# Continues to ask for a number until you enter 4
69-
guess = int(input("What's your number? "))
70-
```
71-
72-
---
73-
74-
# Infinite Loops
75-
76-
```python
77-
while True:
78-
# This loops forever
79-
print("Hello")
80-
```
81-
82-
---
83-
84-
# Break Statements
85-
86-
```python
87-
while True:
88-
print("Hello")
89-
break
90-
```
91-
92-
---
93-
94-
# While Vs For Loop
95-
96-
```python
97-
x = 1
98-
while x <= 100:
99-
print(x)
100-
x += 1
101-
102-
for y in range(1, 101):
103-
print(y)
104-
```
105-
106-
---
107-
108-
# Collections
109-
110-
- List
111-
- Tuple
112-
- Set
113-
- Dictionary
114-
115-
---
116-
117-
# Collections — List
118-
119-
```python
120-
names = ["Alice", "Bob", "Charlie"]
121-
122-
names.append("Dave") # ["Alice", "Bob", "Charlie", "Dave"]
123-
124-
names[2] = "Chris" # ["Alice", "Bob", "Chris", "Dave"]
125-
126-
del(names[1])# ["Alice", "Chris", "Dave"]
127-
128-
if "Eve" in names:
129-
print("Eve is here")
130-
131-
for name in names:
132-
print(name)
133-
```
134-
135-
---
136-
137-
# Collections — Tuple
138-
139-
```python
140-
colours = ("Red", "Blue", "Green")
141-
print(colours[0]) # Red
142-
print(colours[1]) # Blue
143-
print(colours[2]) # Green
144-
```
145-
146-
---
147-
148-
# Collections — Set
149-
150-
```python
151-
fruit = {"Apple", "Banana", "Cherry"}
152-
for item in fruit:
153-
print(item)
154-
```
155-
156-
---
157-
158-
# Collections — Dictionary
159-
160-
```python
161-
shirt = {
162-
"size": "Large",
163-
"colour": "Red"
164-
}
165-
166-
print(shirt["size"]) # Large
167-
168-
shirt["material"] = "Cotton" # Add new key/value pair
169-
shirt["colour"] = "Green" # Change existing value
170-
171-
del(shirt["size"]) # Delete key/value pair
172-
173-
if "material" in shirt:
174-
print("The material is: " + shirt["material"])
175-
176-
for key in shirt:
177-
print(str(key) + " = " + str(shirt[key]))
178-
```
179-
180-
---
181-
182-
# Nested Collections
183-
184-
```python
185-
phone_grid = [
186-
[1, 2, 3],
187-
[4, 5, 6],
188-
[7, 8, 9],
189-
["*", 0, "#"]
190-
]
191-
192-
for row in phone_grid:
193-
for column in row:
194-
print(column)
195-
```
196-
197-
---
198-
199-
# List of Dictionaries
200-
201-
```python
202-
contacts = [
203-
{"fname": "Alice", "lname": "Smith"},
204-
{"fname": "Bob", "lname": "Jones", "phone": "555-1234"},
205-
{"fname": "Charlie", "lname": "McCloud"}
206-
]
207-
208-
for person in contacts:
209-
if "phone" in person:
210-
print(person["fname"])
211-
```
212-
213-
---
214-
21510
# Functions
21611

21712
```python
@@ -313,7 +108,6 @@ def calc_factorial(x):
313108
---
314109

315110
## Any Questions?
316-
### sli.do #python2020
317111

318112
---
319113

@@ -404,13 +198,3 @@ f.close()
404198
# [fit] Coding Time
405199
## Section B
406200

407-
---
408-
409-
### Questions?
410-
### go to sli.do #python2020
411-
412-
---
413-
414-
# [fit] Next Session
415-
### Thursday 2nd July 10am
416-
### Answers

0 commit comments

Comments
 (0)
0