
Intro to Intermediate Python
Learning Objectives
After this lesson, you will be able to:
- Confidently recap the previous units.
- Describe key components of the upcoming unit.
Leveling Up
You’re leveling up!
You have the proper foundation. Now, let’s check how you’re doing.
Let’s Review: Lists
- A collection of items stored in a single variable.
- Created with square brackets (
[]).
- Begin counting at
0.
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?
Answer: Lists Challenge
- Python uses a
: to represent a range of indices.
- Beware of off-by-one errors!
Pro tip: It’s 6 instead of 5 because the range is exclusive.
Let’s Review: Loops and Iteration
What about looping a list?
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?
Answer: Loops Challenge
To loop 1–10 without a data structure:
- Why do you think we put
11 in the code?
- What values does this print?
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.
Let’s Review: Tuples
- Lists that can’t be changed!
- Created with parentheses (
()).
- Can’t add, pop, remove, or otherwise change elements after creation.
Let’s Review: Dictionaries
- A collection of key-value pairs.
- Created with curly braces (
{key: value, key: value}).
- Values can be anything!
Challenge: Can you recall how to iterate (loop) over each key of my_puppy and print out both the key and the corresponding value?
Answer: Dictionaries Challenge
Iterating a dictionary is similar to a list:
Outputs:
name - Fido
breed - Corgi
age - 3
vaccinated - True
fave toy - chew sticks
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!
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.
Function Parameters: Solution
Let’s Review: Return Statements
- Bring data out of a function.
- Cause the function to exit.
- Aren’t a
print statement!
Let’s Review: Classes
- Templates (aka, blueprints) for objects.
- Can contain methods and/or variables.
self is a reference to the created object.
Challenge: How do you declare a new Animal?
Answer: Classes
Declaring a new Animal from the class:
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.
Knowledge Check
We’re about to move on to the next unit: Intermediate Python.
Any questions?
Don’t be shy! If you have a question, so do others!
Switching Gears: Preview
The next unit covers many topics, including:
- User input
- File I/O
- Abstraction
- Modules and libraries
- APIs
You don’t need to memorize them now! This is just an overview.
Abstraction
Python has built-in functions for performing common tasks.
You’ve seen things like my_list.len(), which tells you the length of a list.
But Python has more specialized built-in functions, like chaining lists together:
This helps you get complex things done more quickly.
We’ll learn several of these.
Modules and Libraries
We mentioned these in the pre-work!
Modules and libraries are:
- Code that others have written.
- Free to use!
- Useful extensions of the Python language (e.g., a fancy date and time formatter).
This one tells us when Mother’s Day is for a given year:
What Is an API?
Not only can we use code other people have written; we can also use data that they’ve made available to us.
We can incorporate stocks, movie ratings, or GIFs from the internet into your program!
This API lists Star Wars characters.
Summary and Q&A
We reviewed topics from earlier lessons:
- Lists, sets, tuples, and dictionaries.
- Loops and iteration.
- Functions, parameters, and return statements.
- Classes and inheritance.
We brushed the surface on some upcoming topics:
- User input and file I/O.
- Abstraction.
- Modules and libraries.
- APIs.
Let’s jump in to it!
Additional Reading and Resources
Now that you have an understanding of basic programming, here are some cool people to read about:
- Ada Lovelace: Regarded as the first programmer.
- Alan Turing: Considered the father of theoretical computer and artificial intelligence; helped crack the enigma code during World War II.
- Linus Torvalds: Creator of Linux OS and Git.