8000 Add updated sections 1 and 2. · DORELAS/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
  • 14_advanced_set_operations
  • 15_dictionaries
  • 16_length_and_sum
  • 17_joining_a_list
  • 8_user_input
  • 2_intro_to_python/lectures
  • 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+
    < 1CF5 code>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

    Lines changed: 37 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -0,0 +1,37 @@
    1+
    numbers = [0, 1, 2, 3, 4]
    2+
    doubled_numbers = []
    3+
    4+
    for num in numbers:
    5+
    doubled_numbers.append(num * 2)
    6+
    7+
    print(doubled_numbers)
    8+
    9+
    # -- List comprehension --
    10+
    11+
    numbers = [0, 1, 2, 3, 4] # list(range(5)) is better
    12+
    doubled_numbers = [num * 2 for num in numbers]
    13+
    # [num * 2 for num in range(5)] would be even better.
    14+
    15+
    print(doubled_numbers)
    16+
    17+
    # -- You can add anything to the new list --
    18+
    19+
    friend_ages = [22, 31, 35, 37]
    20+
    age_strings = [f"My friend is {age} years old." for age in friend_ages]
    21+
    22+
    print(age_strings)
    23+
    24+
    25+
    # -- This includes things like --
    26+
    names = ["Rolf", "Bob", "Jen"]
    27+
    lower = [name.lower() for name in names]
    28+
    29+
    # That is particularly useful for working with user input.
    30+
    # By turning everything to lowercase, it's less likely we'll miss a match.
    31+
    32+
    friend = input("Enter your friend name: ")
    33+
    friends = ["Rolf", "Bob", "Jen", "Charlie", "Anne"]
    34+
    friends_lower = [name.lower() for name in friends]
    35+
    36+
    if friend.lower() in friends_lower:
    37+
    print(f"I know {friend}!")
    Lines changed: 24 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -0,0 +1,24 @@
    1+
    ages = [22, 35, 27, 21, 20]
    2+
    odds = [n for n in ages if n % 2 == 1]
    3+
    4+
    # -- with strings --
    5+
    6+
    friends = ["Rolf", "ruth", "charlie", "Jen"]
    7+
    guests = ["jose", "Bob", "Rolf", "Charlie", "michael"]
    8+
    9+
    friends_lower = [f.lower() for f in friends]
    10+
    11+
    present_friends = [
    12+
    name.capitalize() for name in guests if name.lower() in friends_lower
    13+
    ]
    14+
    15+
    # -- nested list comprehensions --
    16+
    # Don't do this, because it's almost completely unreadable.
    17+
    # Splitting things out into variables is better.
    18+
    19+
    friends = ["Rolf", "ruth", "charlie", "Jen"]
    20+
    guests = ["jose", "Bob", "Rolf", "Charlie", "michael"]
    21+
    22+
    present_friends = [
    23+
    name.capitalize() for name in guests if name.lower() in [f.lower() for f in friends]
    24+
    ]
    Lines changed: 27 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -0,0 +1,27 @@
    1+
    friends = ["Rolf", "ruth", "charlie", "Jen"]
    2+
    guests = ["jose", "Bob", "Rolf", "Charlie", "michael"]
    3+
    4+
    friends_lower = {n.lower() for n in friends}
    5+
    guests_lower = {n.lower() for n in guests}
    6+
    7+
    present_friends = friends_lower.intersection(guests_lower)
    8+
    present_friends = {name.capitalize() for name in friends_lower & guests_lower}
    9+
    10+
    print(present_friends)
    11+
    12+
    # Transforming data for easier consumption and processing is a very common task.
    13+
    # Working with homogeneous data is really nice, but often you can't (e.g. when working with user input!).
    14+
    15+
    # -- Dictionary comprehension --
    16+
    # Works just like set comprehension, but you need to do key-value pairs.
    17+
    18+
    friends = ["Rolf", "Bob", "Jen", "Anne"]
    19+
    time_since_seen = [3, 7, 15, 11]
    20+
    21+
    long_timers = {
    22+
    friends[i]: time_since_seen[i]
    23+
    for i in range(len(friends))
    24+
    if time_since_seen[i] > 5
    25+
    }
    26+
    27+
    print(long_timers)
    Lines changed: 26 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -0,0 +1,26 @@
    1+
    """
    2+
    friends = ["Rolf", "Bob", "Jen", "Anne"]
    3+
    time_since_seen = [3, 7, 15, 11]
    4+
    5+
    long_timers = {
    6+
    friends[i]: time_since_seen[i]
    7+
    for i in range(len(friends))
    8+
    if time_since_seen[i] > 5
    9+
    }
    10+
    11+
    print(long_timers)
    12+
    """
    13+
    14+
    # While that is extremely useful when we have conditionals, sometimes we
    15+
    # just want to create a dictionary out of two lists or tuples.
    16+
    # That's when `zip` comes in handy!
    17+
    18+
    friends = ["Rolf", "Bob", "Jen", "Anne"]
    19+
    time_since_seen = [3, 7, 15, 11]
    20+
    21+
    # Remember how we can turn a list of lists or tuples into a dictionary?
    22+
    # `zip(friends, time_since_seen)` returns something like [("Rolf", 3), ("Bob", 7)...]
    23+
    # We then use `dict()` on that to get a dictionary.
    24+
    25+
    friends_last_seen = dict(zip(friends, time_since_seen))
    26+
    print(friends_last_seen)
    Lines changed: 26 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -0,0 +1,26 @@
    1+
    # So far we've been using functions such as `print`, `len`, and `zip`.
    2+
    # But we haven't learned how to create our own functions, or even how they really work.
    3+
    4+
    # Let's create our own function. The building blocks are:
    5+
    # def
    6+
    # the name
    7+
    # brackets
    8+
    # colon
    9+
    # any code you want, but it must be indented if you want it to run as part of the function.
    10+
    11+
    12+
    def greet():
    13+
    name = input("Enter your name: ")
    14+
    print(f"Hello, {name}!")
    15+
    16+
    17+
    # Running this does nothing, because although we have defined a function, we haven't executed it.
    18+
    # We must execute the function in order for its contents to run.
    19+
    20+
    greet()
    21+
    22+
    # You can put as much or as little code as you want inside a function, but prefer shorter functions over longer ones.
    23+
    # You'll usually be putting code that you want to reuse inside functions.
    24+
    25+
    # Any variables declared inside the function are not accessible outside it.
    26+
    print(name) # ERROR!

    0 commit comments

    Comments
     (0)
    0