The Python Basics Notebook

Contents

The Python Basics Notebook#

# /// script
# requires-python = ">=3.10"
# ///

# Standard library imports (no need to declare in dependencies)

Overview#

Welcome to your next step in learning Python!
This notebook is written like a small interactive book to complement the lecture.

This notebook covers the following core building blocks:

Chapter

Concept

Why it matters

0

Commenting & Printing

Learn how to annotate your code to make it more readable

1

Data Types

Understand the different types of values in Python

2

Variables

Store and label values

3

Operators

Learn how to perform operations on variables & values

4

Data Structures

Organizing values in Python

5

Data Structures: Lists

An ordered container of values

6

Data Structures: Tuples

An ordered, immutable container of values

7

Data Structures: Dictionaries

A key-value container

8

Data Structures: Sets

An unordered, unique set of values

9

For Loops

Learn how to automate repetitive tasks

10

If Statements

Conduct conditional tasks

11

Functions

Package code into reusable, testable actions

Each chapter has:

  1. Narrative explanation – read this like a textbook.

  2. 📓 Examples – run and play.

  3. ✍️ Exercises – your turn & guess the output!


0. Commenting & Printing#

Concept.
Comments are lines of text in your code that are ignored by the computer. They are written in plain language and are meant to explain what your code is doing. Comments help make your code easier to understand–for both future you and other people reading it. Comments can also be used to prevent running lines of code.

How to make a comment in Python#

To write a comment, start the line with a hashtag #. Anything after the # is ignored by Python.

# This is a comment

📓 Example: Making a comment#

# Monday, July 14, 2025

Where to place comments in Python#

You can place comments as their own line or at the end of a line of code.

# This is a comment
print("Hello, World!")  # This is also a comment

Printing#

In Python, print() is a built-in function that displays whatever you put inside it on the screen. It is a very useful tool for writing and testing code, as we will see throughout this lesson!

📓 Example: Printing#

Run the code block below to use print() to print “Hello, World!”

print("Hello, World!")
Hello, World!

✍️ Exercise: Printing Practice#

Use print() to print your name!

print("Eva")
Eva

Use comments to prevent Python from running code#

You can add a # at the beginning of a line of code to stop Python from running it. This is called “commenting out” code.

📓 Example: Commenting out lines of code#

# print("Hello, World!")
print("Hasta la vista, baby!")
Hasta la vista, baby!

Commenting keyboard shortcuts#

Since commenting is so useful in Python, there are keyboard shortcuts to quickly comment or uncomment lines of code.

Operating System

Shortcut

Mac OS X

CMD + /

Windows

CTRL + /

To use these shortcuts, put your cursor on any 1 line of code or highlight multiple lines of code to toggle comments on or off with the shortcut.

Commenting Best Practices#

Writing clear comments helps others and your future self understand what your code does without extra explanation. As you work through this lesson, we will highlight best practices for writing helpful comments and keeping your code easy to read.

✍️ Exercise: Commenting Practice#

  1. Make a line comment that says: “This is a comment”

  2. Run the code in the cell

  3. Then add the following code in a separate line: print("This shouldn't be here")

  4. Run the code in the cell

  5. Comment out the print("This shouldn't be here") code line

  6. Run the code in the cell again

# This is a comment
print("This shouldn't be here")
This shouldn't be here

1. Data Types#

Concept. Let’s now discuss different types of values commonly used in Python! In Python, the following data types are commonly used:

Data Type

Description

Example

int

integer numbers; pronounced “int”

-1, 0, 42

float

decimal numbers; pronounced “float”

3.14, -0.001, 26.2

str

a sequence of characters bounded by single or double quotes; pronounced “string”

“hello”, ‘world’, “123”, “ca$hmoney3000”, “^.^”

bool

2 truth values; pronounced “bool” or “boolean”

True or False

None

nothing - represents absence of a value; pronounced “none”

None

How to tell what data type is assigned to a value#

You can use the built-in function type() to check a value’s data type.

📓 Example: Using type()#

print(type("Hi"))
<class 'str'>

Casting a value to a different data type#

You also cast, or change, a value’s data type with the following built-in functions:

Function

Description

int()

converts a value to an int

float()

converts a value to a float

str()

converts a value to a str

📓 Example: Converting a float to an int#

print(type(2.3))
print(int(2.3))  # converts float 2.3 to int 2
print(type(int(2.3)))
<class 'float'>
2
<class 'int'>

📓 Example: Converting an int to a str#

print(type(17))
print(str(17))  # converts int 17 to str 17
print(type(str(17)))
<class 'int'>
17
<class 'str'>

✍️ Exercise: Guess the output before running the code!#

print(type(3.14))
<class 'float'>

✍️ Exercise: Guess the output before running the code!#

print(type(str(2025)))
<class 'str'>

✍️ Exercise: Guess the output before running the code!#

print(float(1))
1.0

2. Variables#

Concept#

A variable is a name assigned to a value. In Python, creating a new variable is called “declaring a variable.”

How to declare a new variable in Python#

To declare a new variable, type a variable name, then use the = sign to assign it a value. Here’s a few examples of declaring variables:

welcome = 'Hello World'
x = 3
y = 5.2  

How to name your variables#

You can name your variables almost anything, but there are some rules and best practices.

Rules

  • Variable names must not start with a number or special character

  • No spaces are allowed in variable names

Best Practices

  • Use only letters, numbers, and underscores _

  • Use lowercase consistently. Python treats uppercase and lowercase letters as different. For example: price and Price are two different variables.

  • Use underscores when you would have wanted to use spaces (snake_case)

  • Be descriptive with names. For example: temperature_c or temp_c is better than t

  • Add a comment if the variable’s purpose is not obvious

How to change your variable’s value#

You can reassign a variable at any time by assigning it a new value. Here’s an example:

price = 19.99
price = "expensive"

The variable price is now assigned to “expensive” .

✍️ Exercise: Variable Practice#

Create two variables:

  • first_name = your first name

  • bobiac_year = your BoBiAC cohort year

Bonus points for commenting what your variables are!

Then, tell Python to print the values assigned to these variables by writing:

print(first_name)
print(bobiac_year)

When you run your code, you should see:
your first name
bobiac_year

first_name = "Eva"  # your first name
bobiac_year = 2025  # your BoBiAC cohort year
print(first_name)
print(bobiac_year)
Eva
2025

3. Operators#

Concept#

Now that we have learned about variables and what can be assigned to them, let’s explore what we can do with variables. In Python, operators are used to perform various operations on variables and values, including arithmetic or comparison operations.

Arithmetic operators#

Arithmetic operators in Python include basic mathematical calculations that you would normally do on a calculator!

Operator

Description

Example

+

Addition

variable_1 + variable_2

-

Subtraction

variable_1 - variable_2

*

Multiplication

variable_1 * variable_2

/

Division

variable_1 / variable_2

**

Exponent

variable_1**variable_2

Using arithmetic operators with variables will return the resulting value.

Let’s see some examples!

📓 Example: Adding two int values#

v1 = 1
v2 = 2
print(v1 + v2)
3

📓 Example: Adding two str values#

v1 = "BoBiAC"
v2 = "2025"
print(v1 + v2)
BoBiAC2025

Notice that we can add 2 str values!

Note: You might be wondering, can we apply other arithmetic operators to str values? The answer is yes, but implementing operators beyond + does not have use for the applications we are covering in this course.

Comparison operators#

Comparison operators compare values assigned to two different variables.

Operator

Description

Example

==

Equal in value

variable_1 == variable_2

!=

Not equal in value

variable_1 != variable_2

>

Greater than

variable_1 > variable_2

<

Less than

variable_1 < variable_2

>=

Greater than or equal to

variable_1 >= variable_2

<=

Less than or equal to

variable_1 <= variable_2

Caution: Don't confuse == with = in Python. A single = is used to declare a variable. A double == is used to evaluate whether two values are equal.

Unlike arithmetic operators, comparison operators generally return a bool, which is either True or False. Let’s see an example.

📓 Example: Comparison operators#

variable_1 = 1
variable_2 = 2
print(variable_1 < variable_2)  # returns True
True

✍️ Exercise: Guess the output before running the code!#

variable_1 = 5
variable_2 = 10
print(variable_1 + variable_2)
15

✍️ Exercise: Guess the output before running the code!#

variable_1 = 5
variable_2 = 10
print(variable_1 == variable_2)
False

✍️ Exercise: Guess the output before running the code!#

variable_1 = "Hi"
variable_2 = "hello"
print(variable_1 + variable_2)
Hihello

✍️ Exercise: Guess the output before running the code!#

variable_1 = 2
variable_2 = 1.3
print(variable_1 + variable_2)
3.3

4. Data Structures#

Concept#

When working with many values, each with its own data type, it is helpful to organize them using data structures. A data structure is a container to organize values. Python has 4 different types of built-in data structures: lists, tuples, dictionaries, and sets.

Python’s Built-In Python Data Structures When thinking about how to organize values into a data structure, there are a few key questions that we can consider:

  • Do the values need appear in an order?

  • Can the values be changed after they have been put in a data structure?

  • Can there be duplicate values?

The following table summarizes the answers to these 4 questions for each Python data structure:

Characteristic

list

tuple

dict

set

Ordered?

Yes

Yes

Yes

No

Allows changing items?

Yes

No

Yes

Yes

Allows duplicate items?

Yes

Yes

No duplicate keys

No

In the next chapters, we will learn more about each of these data structures and how we can work with them.


5. Data Structures: Lists#

Concept#

A list is a built-in Python data structure. It is a way to store a sequence of values.

Creating a list#

We create a list by placing items inside square brackets [ ], separated by commas:

mylist = [1, 2, 3]

We can put any data type we’d like in a list, not just int! We can even mix different data types. Here’s an example:

mylist = [1, 'code', 1.1, 'breathe', 2, 'repeat', True]

Lists can also have duplicate items:

mylist = [1, 'code', 'code', 1.1, 'breathe', 2, 'repeat', True, True]

Lists can even contain lists as items!

mylist = [['code', 'code', 'code'], ['breathe', 'breathe'], ['repeat']]

We can also print() variables associated with a whole list to see the entire list.

📓 Example: Printing a List#

mylist = ["a", "b", "c"]
print(mylist)
['a', 'b', 'c']

List length#

You can find a list’s length using len().

📓 Example: Getting a list’s length#

mylist = [1, 2, 3]
print(len(mylist))
3

Now you give it a try! Get the length of this list:

mylist = ['Bo', 'Bi', 'A', 'C', 2025]

✍️ Exercise: Printing a list’s length#

mylist = ["Bo", "Bi", "A", "C", 2025]
print(len(mylist))
5

Indexing#

The order of items in a list is tracked using an index. An item’s index tells you its position in the list. The first item in a list has an index [0], the second item has an index of [1], and so on. Notice how our index counting scheme in Python starts from 0 instead of 1. Starting from 0 is a general theme in Python that spans through many different concepts we will cover in this course.

Accessing list items#

We can access individual list items by using their indices! For example, let’s consider the following list mylist:

mylist = ['a', 'b', 'c']

If we want to print only 'a', then we can use its index to specifically print it. Since 'a' is first in the list, it has an index of [0].

print(mylist[0])
> a

✍️ Exercise: Printing Items in a List#

Print the following sentence by accessing and printing items in the list mylist

  • Sentence:

    I

    love

    to

    code

  • mylist = ['I', 'hate', 'love', 'to', 'code']

mylist = ["I", "hate", "love", "to", "code"]
print(mylist[0])
print(mylist[2])
print(mylist[3])
print(mylist[4])
I
love
to
code

Remember, indexing in Python starts at zero!

Let’s see another example of accessing a list items. Consider the following list mylist:

mylist = [['a', 'b', 'c'], ['d', 'e', 'f']]

If we want to print only 'd', then we can use the first index to specify which item in the overall list, then a second index to specify the value within that item.

📓 Example: Accessing an item in a list of lists#

mylist = [["a", "b", "c"], ["d", "e", "f"]]
print(mylist[1][0])  # prints d
d

Negative Indexing#

When a list has many items, it is often easier to count from the end rather than the beginning of the list. Python lets you do this using negative indexing.

For example, let’s consider the following list mylist:

mylist = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

If we want to print 'x', it’s much easier to say it’s 3rd from the end of the list, rather than counting through the items preceding it. We can use negative indexing to start our index count from the end of the list and work backwards. With negative indexing, [-1] is the last item of the list, [-2] is the second to last item of the list, and so on. Therefore, to print 'x' from mylist, we would use [-3] index.

📓 Example: Using negative indexing#

mylist = [
    "a",
    "b",
    "c",
    "d",
    "e",
    "f",
    "g",
    "h",
    "i",
    "j",
    "k",
    "l",
    "m",
    "n",
    "o",
    "p",
    "q",
    "r",
    "s",
    "t",
    "u",
    "v",
    "w",
    "x",
    "y",
    "z",
]
print(mylist[-3])
x

Accessing a Range of Items#

Sometimes, you want to access a range of items from a list at once. We can do this by specifying a range of indices. Let’s again consider this list mylist:

mylist = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

If we want to print 'a' 'b' 'c', the first 3 items in the list, then we can specify the range of their indices using a colon :. Since indexing starts from 0, that range will be [0:3]. Notice how the last number in that index range, 3, is excluded.

📓 Example: Accessing a range of items#

mylist = [
    "a",
    "b",
    "c",
    "d",
    "e",
    "f",
    "g",
    "h",
    "i",
    "j",
    "k",
    "l",
    "m",
    "n",
    "o",
    "p",
    "q",
    "r",
    "s",
    "t",
    "u",
    "v",
    "w",
    "x",
    "y",
    "z",
]
print(mylist[0:2])  # wrong indexing - the last item is EXCLUDED!!!
print(mylist[0:3])  # correct indexing to print abc
['a', 'b']
['a', 'b', 'c']

✍️ Exercise: Guess the output before running the code!#

mylist = ["I", "hate", "love", "to", "code"]
print(mylist[2:5])
['love', 'to', 'code']

Changing an item in a list#

Lists are mutable, which means you can change their contents after creating them. You can update an item by using its index and assigning it to a new value. For example, consider the list mylist:

mylist = ["dapi", "fitc", "cy5"]

Let’s say we want to change the first item of mylist to be “mcherry”, instead of “dapi”. We would do that as follows:

mylist[0] = "mcherry"

📓 Example: Changing an item in a list#

mylist = ["dapi", "fitc", "cy5"]
print(mylist)
mylist[0] = "mcherry"
print(mylist)
['dapi', 'fitc', 'cy5']
['mcherry', 'fitc', 'cy5']

Adding items to a list#

After a list is created, we can add new items to it at any time.

Add item to the end of a list#

You can add an item to the end of a list with append(). For example:

mylist = [] # empty list
mylist.append('something')
print(mylist)
> ['something']

📓 Example: Using append() to add an item to the end of a list#

mylist = []
a = ["hi", "hello"]
b = ["bye", "goodbye"]
mylist.append(a)
mylist.append(b)
print(mylist)
[['hi', 'hello'], ['bye', 'goodbye']]

Notice how append() simply adds the specified item to the end of mylist. In the example above, each item is a list. If you append a list, the entire list is added as a single item, not as individual elements.

Add an item to a specific list index#

You can add an item to a specified index with insert(). For example, let’s say we want to insert 'b' in mylist at index [1]:

mylist = ['a', 'c']
mylist.insert(1, 'b')
print(mylist)
> ['a', 'b', 'c']

✍️ Exercise: Adding Items to a List#

Given mylist:

mylist = ['a', 'b', 'c']

Add 'easy as' to the beginning of mylist. Print the updated mylist to confirm it’s correct.

a = "easy as"
mylist = ["a", "b", "c"]
mylist.insert(0, a)
print(mylist)
['easy as', 'a', 'b', 'c']

✍️ Exercise: Adding Items to a List#

Given mylist:

mylist = ['a', 'b', 'c']

Add 'alphabet' to the end of mylist. Print the updated mylist to confirm it’s correct.

a = "alphabet"
mylist = ["a", "b", "c"]
mylist.append(a)
print(mylist)
['a', 'b', 'c', 'alphabet']

Removing list items#

We can also remove items from a list! There are many ways to do this, but we will focus on 3 common methods.

Removing list items with remove()#

Let’s say you have a specific value you would like to remove from a list. You can use remove() to remove that value from the list. For example, consider the following list mylist:

mylist = [1, 2, 3]

Let’s say we want to remove the int 2 from mylist. We could do that using remove():

mylist = [1, 2, 3]
mylist.remove(2)
print(mylist)
> [1, 3]

remove() is a nice way to remove list items when you have a specific value you want to target to remove.

Note: remove() only removes the **first** matching value it finds. If there are duplicate values in a list, remove() will **not** remove all of them.

Removing list items with pop()#

Let’s say you know the index of a specific item that you want to remove from a list. You can use pop() to remove that an item at that specific index from the list. For example, consider the following list mylist:

mylist = ['a', 'b', 'c']

Let’s say we want to remove the second value in mylist, which would have an index of [1]. We could do that using pop():

mylist = ['a', 'b', 'c']
mylist.pop(1) # remove the second value from mylist
print(mylist)
> ['a', 'c']

Caution: Notice that `pop()` removes a specified **index**, while `remove()` removes a specified **value**. The two approaches are not the same!

✍️ Exercise: Removing Items from a List#

Given letters:

letters = ['d', 'm', 'x']

Remove 'm'. Print the updated letters to confirm it’s correct.

# using pop()
letters = ["d", "m", "x"]
letters.pop(1)
print(letters)

# using remove()
letters = ["d", "m", "x"]
letters.remove("m")
print(letters)
['d', 'x']
['d', 'x']

Emptying a List with clear()#

Sometimes, you want to completely empty a list and start fresh. You can do that using clear(). Let’s say that you have already made a list mylist:

mylist = ['x', 'y', 'z']

Now you want to remove all items in mylist. Here’s how you can use clear() to do that:

mylist = ['x', 'y', 'z']
mylist.clear()
print(mylist)
> []

6. Data Structures: Tuples#

Concept#

Another built-in data structure in Python is a tuple. Just as with a list, a tuple stores a sequence of values that are ordered and potentially appear in duplicate. However, a tuple is unchangeable, or immutable, meaning that we cannot change items in a tuple after they are specified.

The chart below summarizes a comparison between a list and a tuple:

Characteristic

list

tuple

Ordered?

Yes

Yes

Allows changing items?

Yes

No

Allows duplicate items?

Yes

Yes

Creating a tuple#

In Python, we can create a tuple by using parentheses () and commas to separate items:

mytuple = (1, 2, 3)

Note: Notice how similar a tuple and a list look in definition. Remember that lists are defined with [] and commas separating items, while tuples are defined with () and commas separating items.

Definition

list

[]

tuple

()

Just like lists, tuples can store a mix of data types. For example:

mytuple = ('a', 1, 'b', 2.5)

Tuple length#

Just as with lists, we can get a tuple’s length using len():

mytuple = ('a', 'b', 'c')
print(len(mytuple))
> 3

Accessing items in a tuple#

Since tuples are ordered, we can access their items using indices! The syntax to do this is the same as with lists. Let’s say we want to access 'b' in mytuple tuple:

mytuple = ('a', 'b', 'c')
print(mytuple[1])
> b

Tuples are immutable#

Unlike lists, we cannot change items in a tuple. Let’s learn this the hard way. Try running the block of code below. What happens?

📓 Example: Changing a tuple’s item#

a = (1, 2, 3)
a[1] = 5
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[34], line 2
      1 a = (1, 2, 3)
----> 2 a[1] = 5

TypeError: 'tuple' object does not support item assignment

This is Python reminding us that tuples are immutable! We cannot modify their contents after they are specified.

✍️ Exercise: Tuple Practice#

Create a tuple containing each letter of your first name as individual items and print it.

name_tuple = ("e", "v", "a")
print(name_tuple)

✍️ Exercise: Tuple Practice#

Access the first item in the tuple you created and print it.

print(name_tuple[0])

7. Data Structures: Dictionaries#

Concept#

Another built-in data structure in Python is a dictionary, or dict. A dict allows you to to associate, or map, a key to a value. Therefore, a dict is organized similarly to a phonebook, where a person’s name (the key) maps to their phone number (the value).

A dict is mutable (changeable). It does not allow duplicate keys, but can contain duplicate values.

The following chart summaries the differences between a list, tuple, and a dict:

Characteristic

list

tuple

dict

Ordered?

Yes

Yes

Yes

Allows changing items?

Yes

No

Yes

Allows duplicate items?

Yes

Yes

No duplicate keys

Creating a dict#

We can create a dictionary using curly brackets {}, with colons : to map keys to values, and commas to separate each mapping:

mydict = {
        1: "Start", 
        2: "Middle", 
        3: "Finish"
        }

In this example:

  • Keys: 1, 2, 3

  • Values: “Start”, “Middle”, “Finish”

Keys can be any immutable type, which includes int, float, str, bool, None, and tuple. Values can be any data type or data structure.

Here’s an example where a dict contains keys mapped to values that are lists:

mydict = {
        1: "Start of course",
        2: ["Lectures", "Labs", "Office Hours", "End"],
        3: ["Course dinner", 2025, "End"],
        }

While we can have duplicate values, we cannot have duplicate keys.

Accessing a key’s values#

To access the value associated with a specific key in a dict, we can use square brackets [] and the key:

mydict = {
        'beginning': "Start of course",
        'middle': ["Lectures", "Labs", "Office Hours"],
        'end': ["Course dinner", 2025],
        }
print(mydict['middle'])
> ["Lectures", "Labs", "Office Hours"]

Note: Accessing values in a dict works differently than in a list or tuple. Lists and tuples use numerical indices based on the order of items, while dictionaries use keys to access values directly, regardless of the order the keys appear in.

✍️ Exercise: Dictionary Practice#

Create a dictionary containing the following artists as keys and their songs as values:

  • Hey Jude by the Beatles

  • Don’t Stop Believin by Journey

  • Hit Me with Your Best Shot by Pat Benatar

  • With a Little Help from My Friends by the Beatles

  • Yellow Submarine by the Beatles

Print the dictionary.

song_dict = {
    "The Beatles": [
        "Hey Jude",
        "With a Little Help from My Friends",
        "Yellow Submarine",
    ],
    "Journey": "Don't Stop Believin",
    "Pat Benatar": "Hit Me with Your Best Shot",
}

print(song_dict)

✍️ Exercise: Dictionary Practice#

From your dictionary, access the Journey song and print it.

print(song_dict[1])

8. Data Structures: Sets#

Concept#

The final built-in data structure in Python is a set. A set is made up of unique values (no duplicates!) that do not have a specific order and can be changed.

The following chart summaries the differences between a list, tuple, dict, and a set:

Characteristic

list

tuple

dict

set

Ordered?

Yes

Yes

Yes

No

Allows changing items?

Yes

No

Yes

Yes

Allows duplicate items?

Yes

Yes

No duplicate keys

No

Creating a set#

We can create a set by using curly brackets {} with commas to separate items:

myset = {1, 2, 3}

Note: Notice how both a set and a dictionary use curly brackets {} to start their definition. Remember that sets are defined with {} and commas separating items, while dictionaries are defined with {} and commas separating keys that are mapped to values with colons.

Definition

set

{“item1”, “item2”}

dict

{“key1”: “value”, “key2”: “value”}

Just as with the other 3 data structures, we can put any data type we’d like in a set. For example:

myset = {'a', 1, 'b', 2.5}

Set length#

We can get a set’s length using len():

myset = {'a', 'b', 'c'}
print(len(myset))
> 3

Sets do not allow duplicates#

Sets automatically remove duplicate items. If your ry to create a set with duplicates, Python will not raise an error, it will just ignore the duplicates. Let’s check it out!

📓 Example: Duplicate items in a set#

myset = {"chocolate", "vanilla", "strawberry", "chocolate"}
print(myset)

"chocolate" does not appear in duplicate when we print myset. This is because sets automatically remove duplicates.

✍️ Exercise: Set Practice#

Create a set containing your favorite ice cream flavors, then print the set.

icecream_flavors = {"chocolate", "pistachio", "hazelnut"}
print(icecream_flavors)

✍️ Exercise: Set Practice#

Now print the length of your set.

print(len(icecream_flavors))

Accessing items in a set#

Since sets contain unordered items, we cannot use indices or keys to access them.


9. For Loops#

Concept#

In Python, for loops let us repeat tasks for each item in a container, such as a list, tuple, dict, or set. They are helpful when you have a block of code that you want to repeat a on each item in one of these data structures.

Let’s consider a countdown timer. We want to print numbers counting down from 5 to 1. In Python, we can do this by specifying this counting sequence as a list count_sequence and printing each item by using its index:

count_sequence = [5, 4, 3, 2, 1] # sequence of counting organized as a list
print(count_sequence[0]) # print 5
print(count_sequence[1]) # print 4
print(count_sequence[2]) # print 3
print(count_sequence[3]) # print 2
print(count_sequence[4]) # print 1

But wow, that’s a lot to type out! Not only is it tedious to type, more typing means more chances to make a mistake. Instead, let’s simplify this code by using a for loop!

Setting up a for loop with a list#

Instead of repeating the same print() line with different list indices, let’s use a for loop to iterate through the list items.

Here is an example of how a for loop can be structured to iterate through list items:

mylist = [1, 2, 3]
for item in mylist:
    #do something to each item

In this structure, Python is smart enough to know that the variable item refers to each item in mylist. The loop works like this:

  1. It starts with the first item, 1, and runs all indented lines below the for line.

  2. Then it moves to the second item, 2, and runs those same indented lines again.

  3. Finally, it processes the third and last item, 3. Once every item in the list has been used, the for loop ends.

If we use this for loop structure to simplify our countdown code above, we would get:

count_sequence = [5, 4, 3, 2, 1]
for item in count_sequence: 
    print(item)
> 5
> 4
> 3
> 2
> 1

Wow! We went from 6 lines of code to 3 lines of code! Yay for loops!

✍️ Exercise: For loop practice with lists#

You’re playing hide and seek and want to use Python to count to 10. Write Python code that will print out your count up from 1 to 10 by using the following list for the count sequence:

count = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
count = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]  # counting sequence as a list
for item in count:
    print(item)

Setting up a for loop with a tuple#

for loops also can iterate over tuples. Here’s an example of the same hide and seek code, but instead of organizing the count in a list, we’ll organize it in a tuple.

count = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) # counting sequence as a tuple
for item in count:
    print(item)
> 1
> 2
> 3
> 4
> 5
> 6
> 7
> 8
> 9
> 10

The loop works the exact same way as it did with a list: Python takes each item from the tuple in order and runs the indented code for each one.

Setting up a for loop with a dict#

for loops can iterate over dictionaries too. Here’s an example of using a for loop to iterate over mydict dictionary’s keys:

mydict = {
        1: "Start", 
        2: "Middle", 
        3: "Finish"
        }
for key in mydict:
    print(key)
> 1
> 2
> 3

In this loop, Python goes through each key in the dict and runs the indented code for each one.

Setting up a for loop with a set#

You can also use a for loop to iterate over a set. However, remember that sets are not ordered, so the items may not appear in the order you defined them.

Consider the following set myset:

myset = {"apple", "banana", "coconut"}

We can use a for loop to print each item in myset:

for item in myset:
    print(item)
> "banana"
> "apple"
> "coconut"

Notice that the order of the printed items may not match the order in which they were defined. This is because sets are not ordered.


10. If Statements#

Concept#

Let’s consider a situation where you are using a for loop to automate a series of tasks, but at a certain step you want to do something extra or skip something. In Python, if statements are used to conditionally conduct lines of code. if statements allow your code to make decisions and branch into different paths depending on whether conditions are met.

if statement keywords

  • if: runs the indented code only if the condition is True

  • elif: (short for “else if”) — Checks another condition if the previous one was False

  • else: A fallback — runs only if all above conditions are False

How to structure an if statement in Python#

if condition:
    # do something

For example, let’s say in our hide and seek count up, we want to warn everyone to hurry up once we count to 7. We can add an if statement inside our for loop to do this.

📓 Example: If statement#

count_sequence = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for item in count_sequence:
    print(item)
    if item == 7:  # if we're on the count of 7,
        print("Hurry up!")  # then print "Hurry up!"

In this code, “Hurry up!” will only print when we are on item 7 in count_sequence. For other items, Python skipped the if block because the condition item==7 was not True.

Multiple if statements in Python#

Sometimes we will want to check more than one condition. Python allows you do do this using a series of if, elif, and else blocks:

 if condition_1: 
    # do this if condition_1 is True
 elif condition_2: 
    # do this if condition_1 is False and condition_2 is True
 elif condition_3:
    # do this if all above are False and condition_3 is True
 else:
    # do this if none of the above conditions are True

Python will ready these conditions in order from top to bottom. As soon as it finds a True condition, it runs that block and skips the rest. The else block is optional, but it is a useful fallback when no conditions are met.

📓 Example: Multiple if statements#

mylist = [1, 2, 3]
for item in mylist:
    if item == 2:
        print("Python rocks!")
    elif item > 1:
        ...
    else:
        print("*")

Logical operators#

When constructing if statements, it is often useful to combine multiple conditions using logical operators. Logical operators enable us to check for more than one condition at once.

Operator

Description

Example

and

Returns True if both statements are true

variable_1>1 and variable_2<5

or

Returns True if one of the statements is true

variable_1>1 or variable_2<5>

Logical operators usually return a bool, which is either True or False.

📓 Example: Logical operators#

variable_1 = 1
variable_2 = 2
print(variable_1 < 3 and variable_2 < 5)  # prints True

Let’s see an example! Suppose you bought two different lottery tickets and want to use Python to check if either ticket wins a grand prize. You can do this using an if statement with the or operator:

winning_number_1 = 314159
winning_number_2 = 628318
lottery_tickets = [628318, 271828]
for number in lottery_tickets: 
    if number==winning_number_1 or number==winning_number_2:
        print("Congratulations! You won!")

Since no items in lottery_tickets match winning_number_1, the first portion of the if statement is always going to be False. However, the first item in lottery_tickets is the same as winning_number_2, so in the first loop number==winning_number_2 will be True. Therefore, in the first loop we will have False or True, which will yield True. We will consequently see the following printed in the first loop:

> "Congratulations! You won!"

The second loop has False or False, which will yield False. Therefore, nothing will be printed in the second loop. Let’s confirm!

📓 Example: Logical operators in if statements#

winning_number_1 = 314159
winning_number_2 = 628318
lottery_tickets = [628318, 271828]
for number in lottery_tickets:
    if number == winning_number_1 or number == winning_number_2:
        print("Congratulations! You won!")

✍️ Exercise: Guess the output before running the code!#

mylist = [1, 2, 3]
for item in mylist:
    if item > 0 or item < 5:
        print("if statements are cool")
    else:
        print("if statements aren't very cool")

Breaking a loop#

Sometimes, you may want to stop a for loop early, for example, once a condition is met. You can use break to exit the loop.

Consider that hide and seek counting code again. We want to count normally, remind everyone to “Hurry up!” at the count of 7, and stop counting after 9, since everyone is hidden. Here is how we can do this:

count_sequence = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for item in count_sequence:
    print(item)
    if item==7: # if we're on the count of 7,
        print("Hurry up!") # then print "Hurry up!"
    if item==10:
        break # end the entire for loop

Check it out for yourself! Run the code below.

📓 Example: Using break in a loop#

count_sequence = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for item in count_sequence:
    print(item)
    if item == 7:  # if we're on the count of 7,
        print("Hurry up!")  # then print "Hurry up!"
    if item == 10:
        break  # end the entire for loop

Continuing a loop#

What if we don’t want to completely exit a for loop, but instead want to skip the rest of the code for a particular iteration? We can use continue.

Consider the hide and seek counting code again:

count_sequence = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for item in count_sequence:
    print(item)
    if item==7: # if we're on the count of 7,
        continue # skips anything else below when item==7
        print("Hurry up!") # then print "Hurry up!"

If we use continue above our “Hurry up!” print line, then the count will continue to the next iteration without printing “Hurry up!”

Check it out for yourself! Run the code below. Does “Hurry up!” print?

📓 Example: Using continue in a loop#

count_sequence = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for item in count_sequence:
    print(item)
    if item == 7:  # if we're on the count of 7,
        continue  # skips anything else below when item==7
        print("Hurry up!")  # then print "Hurry up!"

11. Functions#

Concept#

In many applications, Python code can become long and complex, which makes it harder to read, debug, and reuse. To organize code by tasks, we use functions. A function is a block of code that performs a specific task. It is organized to have a name, inputs (parameters), and outputs (return values).

How to write a function#

We structure functions as follows:

def function_name(parameters): 
    # do task here
    return value

In this function:

  • function_name is the name of the function

  • parameters are inputs the function uses

  • return specifies the output value the function gives back

We can use the function by calling it. That can be done as follows:

function_name(arguments) # calling the function with actual inputs called arguments

Note that parameters in the function_name definition is a placeholder for what you will actually want to input into the function. In the example above where we call the function function_name, arguments is the actual input passed into the function to use to generate a value to return.

Best Practices for Labeling Functions#

When creating functions, it is good practice to add additional information about what the function does and what inputs and outputs it expects. This makes your code easier to understand for others and future you.

Adding a docstring#

We can add a """docstring""", which is a string enclosed in triple quotes “”” “””, to describe what the function does. Python ignores the content of the docstring when running the code.

def function_name(parameters): 
    """A brief description of the function.

    Parameters
    ----------
    parameters: what this is

    Return
    ------
    what the function returns

    """
    # do task here
    return value

Adding type labels#

We can also add type labels to specify the expected data types for the function’s inputs and outputs. These labels will make it easier use the function correctly.

def function_name(parameters:type) -> type: 
    """A brief description of the function.

    Parameters
    ----------
    parameters: type
    what this is

    Return
    ------
    type: what the function returns
    
    """
    # do task here
    return value

Functions with multiple inputs#

Functions can take multiple inputs by listing parameters separated by commas. Here’s an example:

def my_function(x:int, y:float) -> int: 
    """A brief description of the function.

    Parameters
    ----------
    x: int
    what x is
    y: float
    what y is

    Return
    ------
    int: what the function returns
    
    """
    # do task here
    return value

✍️ Exercise: Writing a function#

Write a function that adds two inputted values and returns the result.

def add_values(x: int, y: int) -> int:
    """A simple addition function.

    Parameters
    ----------
    x: int
        The first number.
    y: int
        The second number.

    Return
    ------
    int: the sum value
    """
    return x + y


sum_calc = add_values(1, 2)
print(sum_calc)

✍️ Exercise: More function writing practice#

Let’s go back to our previous example of hide and seek counting. Our code was:

count = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # counting sequence as a list
for item in count:
        print(item)

Now, let’s edit this code to write a function for the task of counting and run it to see if our counter still works!

def counter_printer(count_sequence: list) -> None:
    """Function that inputs a count_sequence list and prints a countdown

    Parameters
    ------------
    count_sequence: list
    a list of a counting sequence from 1 to 10

    Return
    ------------
    None: prints each item from the list count_sequence

    """
    for item in count_sequence:
        print(item)
    return None


count = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]  # counting sequence as a list
counter_printer(count)  # call the function counter

Accessing Function Documentation#

It may seem like a lot of extra work to write out a full docstring on your functions, but in Python there is incentive to do so. Running the following for any given function, not just the ones you write, will return the docstring on the function:

function_name?

Docstrings, therefore, are very helpful to quickly remind yourself about relevant function info in your own code, other people’s code, and built-in Python functions.

✍️ Exercise: Access your function’s docstring#

Use ? to access the docstrings for the functions you wrote above.

add_values?
counter_printer?

Next Steps#

Congratulations for making it to the end of this lesson! You have now been introduced to relevant Python fundamentals needed for the rest of the course. However, there is always more to learn when it comes to mastering a programming language and mindset. Below are three resources we recommend to review concepts and build upon your new-found knowledge.

Reviewing Python Fundamentals: Code in Place#

Code in Place is a self-paced beginner course that covers many of the core topics introduced in this lesson.

Extra Practice on Python Fundamentals: Real Python#

Real Python offers beginner-friendly practice problems and tutorials to strengthen your Python skills.

Reviewing Python Syntax & Rules: Python Docs#

Python Docs are the official source for Python syntax, rules, and concepts—perfect for deeper reference and learning.