8000 Updated session 7 slides and answers · zfoxpython/intro-to-python@71bce07 · GitHub
[go: up one dir, main page]

Skip to content

Commit 71bce07

Browse files
committed
Updated session 7 slides and answers
1 parent 8469c7b commit 71bce07

File tree

15 files changed

+109
-33
lines changed

15 files changed

+109
-33
lines changed

session_07/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
In this session we cover:
44
- What are functions and why we use them
55
- Creating your own functions
6-
- DRY Principle
6+
- DRY principle
77
- Calling functions
88
- Parameters
9-
- Returning values from Functions
9+
- Returning values from functions
1010
- Recursion

session_07/answers/A1.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# A1 - Write a function that prints your name
22

3-
43
def print_name():
54
print("My name")
65

session_07/answers/A2.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# A2 - Write a function that accepts a name as a parameter and prints "Hello, "
22

3-
43
def hello(name):
54
print("Hello, " + name + "!")
65

session_07/answers/A3.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# A3 - Loop through the list ["Alice", "Bob", "Charlie"] and call the function you just wrote
2+
23
def hello(name):
34
print("Hello, " + name + "!")
45

session_07/answers/A4.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# A4 - Write a function that prints the area of two passed in parameters
2+
23
def area(w, h):
34
print("The area is " + str(w * h))
45

session_07/answers/A5.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# A5 - Write a function called 'print_list' that accepts a list as a parameter and then prints out each item of the list
2+
23
def print_list(fruits):
34
for fruit in fruits:
45
print(fruit)

session_07/answers/A6.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
# iii. If they are over 16, print 'You're too old for school"
55
# vi. If they are 0, print "You're not born yet!"
66

7-
87
def school_eligibility(age):
98
if age < 11:
109
print("You're too young to go to this school.")

session_07/answers/B1.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# B1 - Write a function called is_odd that will return True if the integer is odd and
22
# False if the integer passed as a parameter is even (hint: x % 2 will return true for all odd numbers)
33

4-
54
def is_odd(number):
65
if number % 2 == 1:
76
return True

session_07/answers/B2.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# B2 - Write a function that accepts a word and returns it backwards, e.g. 'hello' -> 'olleh'
22

3-
43
def reverse_word(name):
54
#METHOD 1
65
new_string = ""

session_07/answers/B3.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
# **
66
# *
77

8-
98
def print_stars(x):
109
star = ""
1110
for y in range(0, x):

session_07/answers/B4.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# B4 - Create a padlock function.
22
# You need to be able to pass in a passcode and if its correct it should return "Unlock", else "Locked"
33

4-
54
def padlock(user_guess):
65

76
pin = 8450

session_07/answers/B6.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# B6 - Write a function called is_prime() that accepts a number and return True or False if the number of prime or not
22

3-
43
def is_prime(num):
54
count = 0
65

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# KPMG:Code - Session 7 - Multiple Choice Questions
2+
3+
1. What is a dictionary in Python?
4+
- a way to allow user input
5+
- a statement that allows a computer to make a decision
6+
- a way to print an output
7+
- a collection of data - A
8+
9+
2. What brackets does a dictionary use?
10+
- {} - A
11+
- ()
12+
- []
13+
- <>
14+
15+
3. A dictionary is an unordered collection of data.
16+
- True - A
17+
- False
18+
19+
4. A tuple can be modified
20+
- True
21+
- False - A
22+
23+
5. What will be the output of the below:
24+
shirt = {
25+
"size": "Large",
26+
"colour": "Red"
27+
}
28+
29+
print(shirt["size"])
30+
31+
- size
32+
- colour
33+
- Large
34+
- Red
35+
36+
6. What would be the ouput of the below:
37+
contacts = [
38+
{"fname": "Ada", "lname": "Lovelace"},
39+
{"fname": "Alan", "lname": "Turing", "phone": "555-1234"},
40+
{"fname": "Steve", "lname": "Shirley"}
41+
]
42+
43+
for person in contacts:
44+
if "phone" in person:
45+
print(person["fname"])
46+
47+
- Ada
48+
- Alan
49+
- Steve - A
50+
- 555-1234
51+
52+
7. What will stop this while loop:
53+
contacts = []
54+
fname = None
55+
56+
while fname != "":
57+
fname = input("What is your first name? ")
58+
lname = input("What is your last name? ")
59+
60+
if fname and lname:
61+
contacts.append({
62+
"fname": fname,
63+
"lname": lname
64+
})
65+
66+
- nothing, it will run forever
67+
- entering a first name
68+
- not entering a first name
69+
- not entering a first name and last name
70+
71+
8. Items in a dictionary are called _____ pairs.
72+
73+
- key, value - A
74+
- value, key
75+
- key, arguments
76+
- value, arguments
77+
78+
9. What will be the output of the below:
79+
shirt = {
80+
"size": "Large",
81+
"colour": "Red"
82+
}
83+
84+
shirt["colour"] = "Green"
85+
86+
- adds a new key/value
87+
- changes an existing value - A
88+
- deletes the key/value pair
89+
- prints out the dictionary
90+
91+
10. What will be the output of the below:
92+
shirt = {
93+
"size": "Large",
94+
"colour": "Red"
95+
}
96+
97+
del(shirt["size"])
98+
99+
- adds a new key/value
100+
- changes an existing value
101+
- deletes the key/value pair - A
102+
- prints out the dictionary

session_07/slides/answers_7.md

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
1-
# [fit] IHF: Code
1+
# [fit] KPMG: Code
22
## [fit] Python — Session 7 — Answers
33
### Live at 10am
44

5-
---
65

7-
## Any Questions
8-
### go to sli.do #python2020
9-
10-
---
11-
12-
# [fit] Next Session
13-
### Tuesday 230th June 10am
14-
### Lesson 8

session_07/slides/session_7.md

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

54
---
65

@@ -201,7 +200,6 @@ for person in contacts:
201200
---
202201

203202
## Any Questions?
204-
### sli.do #python2020
205203

206204
---
207205

@@ -483,13 +481,3 @@ def calc_factorial(x):
483481
# [fit] Coding Time
484482
## Section B
485483

486-
---
487-
488-
### Questions?
489-
### go to sli.do #python2020
490-
491-
---
492-
493-
# [fit] Next Session
494-
### Thursday 25th June 10am
495-
### Answers

0 commit comments

Comments
 (0)
0