{.separator}

Intro to Intermediate Python

Presenter Notes

Learning Objectives

After this lesson, you will be able to:

  • Confidently recap the previous units.
  • Describe key components of the upcoming unit.

Presenter Notes

Leveling Up

You're leveling up!

You have the proper foundation. Now, let's check how you're doing.

Presenter Notes

Let's Review: Lists

  • A collection of items stored in a single variable.
  • Created with square brackets ([]).
  • Begin counting at 0.

```python my_queens = ["Cersei", "Daenerys", "Arwen", "Elsa", "Guinevere"] step_counts_this_week = [8744, 5256, 7453, 3097, 4122, 2908, 6720]

We can also mix types.

weird_list = [1, "weird", ["nested list"], "eh?"] ```

Challenge: Can you recall how to slice a section of the list? For example, items 2 through 5 of step_counts_this_week?

Presenter Notes

Answer: Lists Challenge

  • Python uses a : to represent a range of indices.
  • Beware of off-by-one errors!

python step_counts_this_week = [8744, 5256, 7453, 3097, 4122, 2908, 6720] days_2_thru_5 = step_counts_this_week[2:6] # Items 2, 3, 4, and 5

Pro tip: It's 6 instead of 5 because the range is exclusive.

Presenter Notes

Let's Review: Loops and Iteration

What about looping a list?

```python my_queens = ["Cersei", "Daenerys", "Arwen", "Elsa", "Guinevere"]

for queen in my_queens: print(queen, "is the most powerful queen!") ```

Challenge: What if I want to loop from 1 to 10 and print out the numbers? How do I do this without a data structure to loop over?

Presenter Notes

Answer: Loops Challenge

To loop 1–10 without a data structure:

```python

Remember, "i" is a common name for a counter/index in programming!

for i in range(1, 11): print(i) ```

  • Why do you think we put 11 in the code?
  • What values does this print?

Presenter Notes

Let's Review: Sets

  • Lists that don't have duplicates.
  • Created with curly braces ({}) or from lists with the set() function.
  • Aren't indexed — elements are in any order!
  • Handy for storing emails, user names, and other unique elements.

```python email_set = {'my_email@gmail.com', 'second_email@yahoo.com', "third_email@hotmail.com"}

Or from a list:

my_list = ["red", "yellow", "green", "red", "green"] my_set = set(my_list)

=> {"red", "yellow", "green"}

```

Presenter Notes

Let's Review: Tuples

  • Lists that can't be changed!
  • Created with parentheses (()).
  • Can't add, pop, remove, or otherwise change elements after creation.

python rainbow_colors_tuple = ("red", "orange", "yellow", "green", "blue", "indigo", "violet")

Presenter Notes

Let's Review: Dictionaries

  • A collection of key-value pairs.
  • Created with curly braces ({key: value, key: value}).
  • Values can be anything!

python my_puppy = { "name": "Fido", "breed": "Corgi", "age": 3, "vaccinated": True, "fave toy": ["chew sticks", "big sticks", "any sticks"] }

Challenge: Can you recall how to iterate (loop) over each key of my_puppy and print out both the key and the corresponding value?

Presenter Notes

Answer: Dictionaries Challenge

Iterating a dictionary is similar to a list:

python for key in my_puppy: print(key, "-", my_puppy[key])

Outputs:

name - Fido breed - Corgi age - 3 vaccinated - True fave toy - chew sticks

Presenter Notes

Let's Review: Functions

  • Bits of code that can be used repeatedly.
  • Enable DRY — Don't Repeat Yourself.
  • Declared with def, (), and :.
  • Declare the function above the function call!

```python

Function definition:

def say_hello(): print("hello!")

Run the function three times.

say_hello() say_hello() say_hello() ```

Presenter Notes

Let's Review: Function Parameters

Parameters are in the function definition.

  • Arguments are in the function call.
  • Useful for very similar code with only minor variations.

Challenge: Rewrite the code below to use a single function with one parameter.

Presenter Notes

Function Parameters: Solution

Presenter Notes

Let's Review: Return Statements

  • Bring data out of a function.
  • Cause the function to exit.
  • Aren't a print statement!

```python def multiply(x, y): return x * y

result = multiply(3, 4) # Result is now equal to 12. ```

Presenter Notes

Let's Review: Classes

  • Templates (aka, blueprints) for objects.
  • Can contain methods and/or variables.
  • self is a reference to the created object.

```python class Animal(): def init(self): self.energy = 50

def get_status(self):
    if self.energy < 20:
        print("I'm hungry!")
    elif self.energy > 100:
        print("I'm stuffed!")
    else:
        print("I'm doing well!")

```

Challenge: How do you declare a new Animal?

Presenter Notes

Answer: Classes

Declaring a new Animal from the class:

python my_animal = Animal() # Creates a new Animal instance. my_animal.get_status() # Prints "I'm doing well!" my_animal.energy += 100 # We can access properties! my_animal.get_status() # Prints "I'm stuffed!"

Presenter Notes

Let's Review: Inheritance

A class can inherit properties and methods from another class.

You Do: Create a new class, Dog, which inherits from Animal.

  • Dog has an extra function, bark(), that prints "bark".
  • Dog has an extra property, breed.

Presenter Notes