8000 Add updated sections 1 and 2. · coding794/complete-python-course@dcbd19d · GitHub
[go: up one dir, main page]

Skip to content

Commit dcbd19d

Browse files
committed
Add updated sections 1 and 2.
1 parent d1141ac commit dcbd19d

File tree

29 files changed

+789
-1
lines changed

29 files changed

+789
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
rebrand-subs/
2+
captions/
13
page_*.jpeg
24
exercises/
35
Subtitles
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Python has two keywords, `and` and `or`
2+
# Here's how to use them:
3+
4+
age = int(input("Enter your age: "))
5+
can_learn_programming = age > 0 and age < 150
6+
7+
print(f"You can learn programming: {can_learn_programming}")
8+
9+
10+
# -- or --
11+
12+
age = int(input("Enter your age: "))
13+
usually_not_working = age < 18 or age > 65
14+
15+
print(f"At {age}, you are usually not working: {usually_not_working}")
16+
17+
# That could be better re-written to:
18+
19+
age = int(input("Enter your age: "))
20+
usually_working = age > 17 and age < 66 # Notice the changes to the numbers!
21+
22+
print(f"At {age}, you are usually working: {usually_not_working}")
23+
24+
25+
# How they work internally
26+
# `and` gives you the first value if it is falsy, otherwise gives you the second value
27+
# `or` gives you the first value if it is truthy, otherwise gives you the second value
28+
29+
# How to tell if a value is "truthy" or "falsy"?
30+
# Pass it through `bool()`.
31+
32+
print(bool(35))
33+
print(bool("Rolf"))
34+
print(bool(0))
35+
print(bool(""))
36+
37+
# More examples linked in the resources section of the lecture
38+
39+
# --
40+
41+
print("" or "Rolf") # Rolf, because "" is falsy
42+
print("" and "Rolf") # "", because it is falsy
43+
44+
print("Rolf" or "") # "Rolf", because it is truthy
45+
print("Rolf" and "") # "", because "Rolf" is not falsy
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
friend1 = "Rolf"
2+
friend2 = "Bob"
3+
friend3 = "Anne"
4+
5+
friends = ["Rolf", "Bob", "Anne"]
6+
7+
print(friends[0]) # This is called a subscript
8+
print(friends[1])
9+
10+
# You can put anything you like inside a list, but it's almost always a good idea to keep it homogeneous.
11+
12+
friends = ["Rolf", 2, "Anne"] # Generally a bad idea
13+
14+
# -- Length of a list --
15+
16+
friends = ["Rolf", "Anne"]
17+
print(len(friends)) # 2
18+
19+
# -- Lists inside lists --
20+
# As mentioned earlier, you can put anything inside a list—and that includes other lists.
21+
22+
friends = [["Rolf", 24], ["Bob", 30], ["Anne", 27]]
23+
print(friends[0][1]) # 24
24+
print(friends[1][0]) # Bob
25+
26+
# -- Long lists --
27+
28+
friends = [
29+
["Rolf", 24],
30+
["Bob", 30],
31+
["Anne", 27],
32+
["Charlie", 37],
33+
["Jen", 25],
34+
["Adam", 29],
35+
]
36+
37+
# -- Adding to a list --
38+
39+
friends = ["Rolf", "Bob", "Anne"]
40+
friends.append("Jen")
41+
42+
print(friends) # ["Rolf", "Bob", "Anne", "Jen"]
43+
44+
# -- Removing from a list --
45+
46+
friends.remove("Bob")
47+
48+
print(friends) # ["Rolf", "Anne", "Jen"]
49+
50+
# Remember if you have a list of lists, for example, you still need the entire thing you want to remove:
51+
52+
friends = [["Rolf", 24], ["Bob", 30], ["Anne", 27]]
53+
54+
friends.remove(["Bob", 30])
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# -- Defining tuples --
2+
3+
short_tuple = "Rolf", "Bob"
4+
a_bit_clearer = ("Rolf", "Bob")
5+
not_a_tuple = "Rolf"
6+
7+
# -- Adding to a tuple --
8+
9+
friends = ("Rolf", "Bob", "Anne")
10+
friends.append("Jen") # ERROR!
11+
12+
print(friends) # ["Rolf", "Bob", "Anne", "Jen"]
13+
14+
# -- Removing from a tuple --
15+
16+
friends.remove("Bob") # ERROR!
17+
18+
print(friends) # ["Rolf", "Anne", "Jen"]
19+
20+
# Tuples are useful for when you want to keep it unchanged forever.
21+
# Most of the time I'd recommend using tuples over lists, and only use lists when you specifically want to allow changes.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# -- Defining sets --
2+
3+
art_friends = {"Rolf", "Anne"}
4+
science_friends = {"Jen", "Charlie"}
5+
6+
# -- Adding to a set --
7+
8+
art_friends.add("Jen")
9+
10+
print(art_friends)
11+
12+
# -- No duplicate items --
13+
14+
art_friends.add("Jen")
15+
16+
print(art_friends) # Same as before, "Jen" was not added twice
17+
18+
# -- Removing from a set --
19+
20+
science_friends.remove("Charlie")
21+
22+
print(science_friends)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
art_friends = {"Rolf", "Anne", "Jen"}
2+
science_friends = {"Jen", "Charlie"}
3+
4+
# -- Difference --
5+
# Gives you members that are in one set but not the other.
6+
7+
art_but_not_science = art_friends.difference(science_friends)
8+
science_but_not_art = science_friends.difference(art_friends)
9+
10+
print(art_but_not_science)
11+
print(science_but_not_art)
12+
13+
# -- Symmetric difference --
14+
# Gives you those members that aren't in both sets
15+
# Order doesn't matter with symmetric_difference
16+
17+
not_in_both = art_friends.symmetric_difference(science_friends)
18+
19+
print(not_in_both)
20+
21+
# -- Intersection --
22+
# Gives you members of both sets
23+
24+
art_and_science = art_friends.intersection(science_friends)
25+
print(art_and_science)
26+
27+
# -- Union --
28+
# Gives you all members of all sets, but of course without duplicates
29+
30+
all_friends = art_friends.union(science_friends)
31+
print(all_friends)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
friend_ages = {"Rolf": 24, "Adam": 30, "Anne": 27}
2+
3+
print(friend_ages["Rolf"]) # 24
4+
# friend_ages["Bob"] ERROR
5+
6+
# -- Adding a new key to the dictionary --
7+
8+
friend_ages["Bob"] = 20
9+
print(friend_ages) # {'Rolf': 24, 'Adam': 30, 'Anne': 27, 'Bob': 20}
10+
11+
# -- Modifying existing keys --
12+
13+
friend_ages["Rolf"] = 25
14+
15+
print(friend_ages) # {'Rolf': 25, 'Adam': 30, 'Anne': 27, 'Bob': 20}
16+
17+
# -- Lists of dictionaries --
18+
# Imagine you have a program that stores information about your friends.
19+
# This is the perfect place to use a list of dictionaries.
20+
# That way you can store multiple pieces of data about each friend, all in a single variable.
21+
22+
friends = [
23+
{"name": "Rolf Smith", "age": 24},
24+
{"name": "Adam Wool", "age": 30},
25+
{"name": "Anne Pun", "age": 27},
26+
]
27+
28+
# You can turn a list of lists or tuples into a dictionary:
29+
30+
friends = [("Rolf", 24), ("Adam", 30), ("Anne", 27)]
31+
friend_ages = dict(friends)
32+
print(friend_ages)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Imagine you're wanting a variable that stores the grades attained by a student in their class.
2+
# Which of these is probably not going to be a good data structure?
3+
4+
grades = [80, 75, 90, 100]
5+
grades = (80, 75, 90, 100)
6+
grades = {80, 75, 90, 100} # This one, because of no duplicates
7+
8+
9+
total = sum(grades)
10+
length = len(grades)
11+
12+
average = total / length
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Imagine you've got all your friends in a list, and you want to print it out.
2+
friends = ["Rolf", "Anne", "Charlie"]
3+
print(f"My friends are {friends}.")
4+
5+
# Not the prettiest, so instead you can join your friends using a ",":
6+
friends = ["Rolf", "Anne", "Charlie"]
7+
comma_separated = ", ".join(friends)
8+
print(f"My friends are {comma_separated}.")
9+
10+
# Want the last one to say ", and" ?
11+
# You'll have to wait until we cover list slicing in the next section!

course_contents/1_intro/lectures/8_user_input/code.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# your_name = '?'
1212
your_name = input("Enter your name: ")
1313

14-
print(f"Hello {your_name}!")
14+
print(f"Hello {your_name}. My name is {my_name}.")
1515

1616
## Calculating months
1717

0 commit comments

Comments
 (0)
0