10000 Update as per Phil's comments. · tecladocode/python-refresher@35015a8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 35015a8

Browse files
committed
Update as per Phil's comments.
1 parent 6eb304f commit 35015a8

File tree

40 files changed

+233
-70
lines changed

40 files changed

+233
-70
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
Python_refresher_notes
12
*.cmproj
23
*.pyc
34
__pycache__
@@ -6,4 +7,4 @@ tempCodeRunnerFile.py
67
.vscode/
78
.idea/
89
videos
9-
exports
10+
exports

10_loops/code.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,24 @@
1313

1414
play = input("Would you like to play? (Y/n) ")
1515

16+
17+
# -- The break keyword --
18+
19+
while True:
20+
play = input("Would you like to play? (Y/n) ")
21+
22+
if play == "n":
23+
break # Exit the loop
24+
25+
user_number = int(input("Guess our number: "))
26+
if user_number == number:
27+
print("You guessed correctly!")
28+
elif abs(number - user_number) == 1:
29+
print("You were off by 1.")
30+
else:
31+
print("Sorry, it's wrong!")
32+
33+
1634
# -- For loop --
1735

1836
friends = ["Rolf", "Jen", "Bob", "Anne"]

12_dictionaries/code.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,8 @@
3232
print(f"Bob: {student_attendance[student]}")
3333
else:
3434
print("Bob isn't a student in this class!")
35+
36+
# -- Calculate an average with `.values()` --
37+
38+
attendace_values = student_attendance.values()
39+
print(sum(attendace_values) / len(attendace_values))

13_functions/code.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def print():
2626
print("Hello, world!") # Error!
2727

2828

29-
# -- Don't reuse names at any stage, it's generally confusing! --
29+
# -- Don't reuse names, it's generally confusing! --
3030
friends = ["Rolf", "Bob"]
3131

3232

14_function_arguments_parameters/code.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,5 @@ def divide(dividend, divisor):
4646
divide(15, 0)
4747
divide(15, divisor=0) # That's OK
4848
# divide(dividend=15, 0) # Not OK, named arguments must go after positional arguments
49+
50+
# I recommend you use keyword arguments whenever possible, just because it makes things much more readable and maintainable long-term.

17_lambda_functions/code.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ def double(x):
2626

2727
sequence = [1, 3, 5, 9]
2828

29+
doubled = [
30+
double(x) for x in sequence
31+
] # Put the result of double(x) in a new list, for each of the values in `sequence`
2932
doubled = map(double, sequence)
3033
print(list(doubled))
3134

19_unpacking_arguments/code.py

Lines changed: 1 addition & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def multiply(*args):
1010
print(multiply(3, 5))
1111
print(multiply(-1))
1212

13-
# The asterisk takes all the parameters and packs them into a tuple.
13+
# The asterisk takes all the arguments and packs them into a tuple.
1414
# The asterisk can be used to unpack sequences into arguments too!
1515

1616

@@ -33,30 +33,6 @@ def add(x, y):
3333

3434
print(add(**nums))
3535

36-
# -- Unpacking kwargs --
37-
def named(**kwargs):
38-
print(kwargs)
39-
40-
41-
named(name="Bob", age=25)
42-
# named({"name": "Bob", "age": 25}) # Error, the dictionary is actually a positional argument.
43-
named(
44-
**{"name": "Bob", "age": 25}
45-
) # Unpack dict into arguments. This is OK, but slightly more confusing. Good when working with variables though.
46-
47-
# -- Unpacking and repacking --
48-
def named(**kwargs):
49-
print(kwargs)
50-
51-
52-
def print_nicely(**kwargs):
53-
named(**kwargs) # Unpack the dictionary into keyword arguments.
54-
for arg, value in kwargs.items():
55-
print(f"{arg}: {value}")
56-
57-
58-
print_nicely(name="Bob", age=25)
59-
6036
# -- Forced named parameter --
6137

6238

@@ -79,33 +55,3 @@ def apply(*args, operator):
7955

8056
print(apply(1, 3, 6, 7, operator="+"))
8157
print(apply(1, 3, 5, "+")) # Error
82-
83-
# -- Both --
84-
85-
86-
def both(*args, **kwargs):
87-
print(args)
88-
print(kwargs)
89-
90-
91-
both(1, 3, 5, name="Bob", age=25)
92-
93-
# This is normally used to accept an unlimited number of arguments and keyword arguments, such that some of them can be passed onto other functions.
94-
# You'll frequently see things like these in Python code:
95-
96-
"""
97-
def post(url, data=None, json=None, **kwargs):
98-
return request('post', url, data=data, json=json, **kwargs)
99-
"""
100-
101-
# While the implementation is irrelevant at this stage, what it allows is for the caller of `post()` to pass arguments to `request()`.
102-
103-
# -- Error when unpacking --
104-
105-
106-
def myfunction(**kwargs):
107-
print(kwargs)
108-
109-
110-
myfunction(**"Bob") # Error, must be mapping
111-
myfunction(**None) # Error

1_variables/code.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# -- Numbers and Decimals --
1+
# -- Numbers and Floats --
22

33
x = 15
44
price = 9.99
@@ -11,6 +11,7 @@
1111

1212
# -- Strings --
1313

14+
name = "Rolf"
1415
name = "Rolf"
1516

1617
print(name)
@@ -29,7 +30,7 @@
2930

3031
b = 17
3132

32-
# Here we've given the value '17' the name 'b'. The name 'a' still is a name for '25'!
33+
# Here we've given the value '17' the name 'b'. The name 'a' is still a name for '25'!
3334

3435
print(a)
3536
print(b)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# -- Unpacking kwargs --
2+
def named(**kwargs):
3+
print(kwargs)
4+
5+
6+
named(name="Bob", age=25)
7+
# named({"name": "Bob", "age": 25}) # Error, the dictionary is actually a positional argument.
8+
9+
# Unpack dict into arguments. This is OK, but slightly more confusing. Good when working with variables though.
10+
named(**{"name": "Bob", "age": 25})
11+
12+
13+
# -- Unpacking and repacking --
14+
def named(**kwargs):
15+
print(kwargs)
16+
17+
18+
def print_nicely(**kwargs):
19+
named(**kwargs) # Unpack the dictionary into keyword arguments.
20+
for arg, value in kwargs.items():
21+
print(f"{arg}: {value}")
22+
23+
24+
print_nicely(name="Bob", age=25)
25+
26+
27+
# -- Both args and kwargs --
28+
29+
30+
def both(*args, **kwargs):
31+
print(args)
32+
print(kwargs)
33+
34+
35+
both(1, 3, 5, name="Bob", age=25)
36+
37+
# This is normally used to accept an unlimited number of arguments and keyword arguments, such that some of them can be passed onto other functions.
38+
# You'll frequently see things like these in Python code:
39+
40+
"""
41+
def post(url, data=None, json=None, **kwargs):
42+
return request('post', url, data=data, json=json, **kwargs)
43+
"""
44+
45+
# While the implementation is irrelevant at this stage, what it allows is for the caller of `post()` to pass arguments to `request()`.
46+
47+
# -- Error when unpacking --
48+
49+
50+
def myfunction(**kwargs):
51+
print(kwargs)
52+
53+
54+
myfunction(**"Bob") # Error, must be mapping
55+
myfunction(**None) # Error

0 commit comments

Comments
 (0)
0