2. Python: Data Types & Containers

2. Python: Data Types & Containers

šŸŽÆĀ Learning Goals

  • Navigate the Google Colab coding environment
  • Create and use variables in Python
  • Differentiate between basic Python data types and understand when and why they are used
  • Describe and compare the purposes of Python containers
  • Use Python methods and operators to manipulate data

šŸ“— Technical Vocabulary

  • Markdown
  • Python
  • Variable
  • String
  • Integer
  • Float
  • Boolean
  • List
  • Dictionary
Ā 
šŸ’”
Why learn Python?
Python is an extremely versatile programming language, with many use cases in web development and in machine learning. Its easy-to-read syntax, extensive libraries and frameworks, and robust visualization capabilities make Python the most suitable language for ML developers.

šŸ¤” What is Google Colab?

Colaboratory ("Colab" for short) is a data analysis and machine learning tool that allows you to combine executable Python code and rich text along with charts, images, and more into a single document stored in Google Drive. It is a free Jupyter notebook environment that requires no setup to use.

What's a Jupyter Notebook?

A Jupyter notebook is an interactive digital workspace where you can write and run code, take notes, and show the results all in one place.
In simpler terms, it is a Jupyter notebook with all the collaboration abilities of Google Docs. This means more than one person can work on the same code at the same time.

✨ Before We Start

Head to drive.google.com and create a folder to save your notebooks as we go. This will help you stay organized!

🌱 Let’s Start!

Navigate to Google Colab. Click ā€œSign In,ā€ and log in with your Google account.
notion image
notion image
Click ā€œNew Notebook.ā€
notion image
Give your Colab notebook a title.
notion image

AI Assistance

Before we get started with writing code, we're going to disable the built-in AI Assistance tools in Google Colab. Select Tools from the menu bar and then select Settings. Navigate to the AI Assistance panel and uncheck the boxes. You'll have the option to change the settings and use these tools later, but as you're getting started, it's helpful to rely on your own understanding first!
notion image
Ā 

šŸ‘©ā€šŸ’»Ā Using Google Colab

Colab has two types of cells: text and code.
notion image

šŸ’¬Ā Text Cells

Click ā€œTextā€ to add a text cell.
notion image
Ā 
Text cells are formatted using a simple markup language called Markdown. When you write text in the Markdown source (left), you can see what it will look like in the rendered version (right). Above the Markdown source, there is a toolbar to assist editing.
notion image
Ā 
If you click outside of the text cell, you will see only the rendered version. Double-click the text cell to see the Markdown source again.
notion image
Ā 
Headers are created using #. Use multiple ### for less emphasis.
Ā 
šŸ’”
Fun Fact: Can you guess the name of the # symbol? Find out here!
Ā 
Here is the size of text followed by one #.
notion image
Ā 
Here is the size of text followed by three ###.
notion image
Ā 
To make text bold surround it with **two asterisks**.
notion image
Ā 
To make text italic use a *single asterisk* or _underscore_.
notion image
āœļø
Try-It | Common Markdown Elements
Now that you've seen some of the basic Markdown elements in action, you can try it for yourself! Check out this resource before completing the steps below.
  1. Create an ordered list of your favorite foods.
  1. Write an unordered list of your friends' names.
  1. Insert a link to a coding resource you like.
  1. Create another Markdown element from the resource provided!

šŸ’»Ā Code Cells

Click ā€œCodeā€ to add a code cell.
notion image
Ā 
You can use code cells to write Python code. Let’s try it! Type print ("HelloĀ World!") into your code cell.
notion image
Ā 
Press the play button to run your code, or you can press ā€œShiftā€ then ā€œEnterā€ or "Return" on your keyboard.
notion image
Ā 
You did it! You wrote and ran a line of code in Python! šŸŽ‰
notion image
Ā 
šŸ’”
Tips & Tricks
Run (or ā€œexecuteā€) each cell in order. You can edit & re-run cells if you would like! Sometimes though, editing and re-executing could have unintended consequences. If you encounter this problem, reset your environment:
  • Click ā€œRuntime.ā€ Then click ā€œRestart runtimeā€ to reset the currently running code cell.
  • Click ā€œRuntime.ā€ Then click ā€œRestart and run allā€ to reset the Colab image and get you back to a 100% clean environment.
  • You can also clear all output in the Colab by clicking ā€œEditā€ and then ā€œClear all outputs.ā€
  • Comments: A hash signĀ #Ā in a code block begins a comment. All characters after the # and up to the end of the physical line are part of the comment and the Python interpreter ignores them. It’s like a note or reminder that programmers leave for themselves or others who read their code. It's written in plain English and doesn't affect how the program runs. Comments help explain what the code does, making it easier to understand and work with, so we’d highly recommend getting into the habit of writing comments!
    • #Ā ThisĀ isĀ howĀ youĀ writeĀ aĀ comment
      āœļø
      Try-It | Text & Code Cells
      Continue working in the same Colab notebook to complete the following tasks:
      1. Create a text cell with notes about things you want to remember from this lesson. Include text that is bold, italicized, different font sizes, or organized in lists!
      1. Create a code cell and print() an encouraging message to yourself. Run the code cell to see the output below the code!

šŸŒŽĀ What is Python?

Python is a programming language first released in 1991 by Guido van Rossum and is one of the most popular languages used in computing today. Python is most often used in web development to create applications. Some of the more popular websites and applications that run on Python (at least partially) include: Google, YouTube, Facebook, Instagram, Reddit, and Dropbox!
Python is also extremely popular in the AI community because it is a general purpose language which makes it easy to collaborate with all types of computer scientists through its simple syntax. People choose to use Python so that they can communicate easily with other people!
šŸ’»
Use this template to follow along with the Try-It and Practice exercises throughout this lesson!

šŸ¤” Why Python for AI/ML?

Why should I use Python for Artificial Intelligence (AI) and Machine Learning (ML)?
That’s a great question! Python is super popular for AI and ML because:
  1. It's Easy to Learn: Python's like speaking English compared to other programming languages. It's straightforward and simple, which makes it a breeze to pick up and start coding.
  1. Loads of Cool Tools: Python has a ton of libraries (think of them as ready-made tools) that make building AI and ML programs easier. Imagine having a giant toolbox full of gadgets that you can just grab and use whenever you need them, rather than building them from scratch.
  1. You Can Do Anything: Python isn't just for AI and ML; you can use it for all sorts of cool stuff, like making games, building websites, or even controlling robots! It's like a Swiss Army knife for coding.

šŸ Basic Python Concepts

Now let's delve into some fundamental Python concepts:

šŸ”¢Ā Data Types

Throughout this camp, you will learn about several different Python data types. A data type is a specific kind of information or data. Based on the type of data we write in our code, the program will interpret it differently. In this lesson, we will cover three data types: strings, numbers, and booleans.

🧶 Strings

Strings represent values that are characters, words, or any other type of text. You can think of a string as a series of characters (alpha, numeric, spaces, and special characters) between two quotation marks. An example is a person's first name or the title of your favorite book:
#Ā StringĀ Example "KarlieĀ Kloss" 'DataĀ Feminism'
In Python, strings can be either double-quoted (ā€œlike thisā€) or single-quoted (ā€˜like this’).

print()

If we want to run the cell and have it return what we wrote, we can use the print function.
#Ā PrintĀ outĀ theĀ followingĀ text print('Hi,Ā KodeĀ WithĀ Klossy')
You should see ā€œHi, Kode With Klossyā€ appear as the output of the code above. The output will appear below the code cell.
Ā 
āœļø
Try-ItĀ | Strings
  1. Create a new code cell. Inside the cell, typeĀ print("<your first name>"). Instead of the words "your first name", type your actual name! Be sure to include the quotes!
  1. TypeĀ print("<the last thing you ate>").
  1. TypeĀ print("<your full name>").
  1. Type anything you want inside of theĀ ()Ā afterĀ print. Try to use some characters from your keyboard that are not numbers or letters.
šŸŒ¶ļøĀ  Mild Challenge: Look up how to make your strings all lower or uppercase!Ā 
✨✨ Don't forget to run your code by clicking the play button. ✨✨

āœ‰ļøĀ Variables

Variables allow us to store information that can later be used in an expression. To define a variable in Python, we must first state the name we want our variable to have. To assign a value to that name, we must use the "Assignment Operator" which is denoted by the equal (=) symbol. Finally, on the right-hand side of the equal sign (=), we give a value we want our variable to hold!
šŸ’”
Note: You can use any word to name your variable. However, there are specific keywords that are reserved in Python that cannot be used as variable names. Check out thisĀ linkĀ for a full list of Python's reserved words. There are also specific rules for naming variables that can be found here.
# Variable example dog = "fluffy" print(dog)
# Variable example 2 favorite_food = "pizza" print(favorite_food)
In Python, when naming variables with more than one word, we use a convention called snake_case, where words are separated by underscores (e.g., favorite_food). While your code won’t break if you don’t use snake_case, other Python developers might find it unusual. Different programming languages have their own naming conventions—like camelCase (used in languages like JavaScript) and kebab-case (often used in URLs). But in Python, snake_case is the standard!
āœļø
Try-It | Strings and Variables
Complete the following in a new code cell. After you type the code, make sure to run it!
Declare a variable storing a string for each prompt below. Print each variable to see the output!
  • A variable that holds your favorite food (e.g.,Ā favorite_food = "tacos")
  • A variable calledĀ petĀ that holds the name of a pet
  • A variable calledĀ schoolĀ that holds the name of your high school
  • A variable calledĀ goalĀ that holds one of your goals. Remember, you can include spaces (and even special characters) in a string!
šŸŒ¶ļøĀ  Mild Challenge: Print one of these variables as part of a longer sentence. Hint: Look up string interpolation!

String Methods

String methods in Python are built-in functions that allow you to manipulate and work with text. By using these methods, you can clean up, format, and analyze text data efficiently, which is especially helpful when working with messy or inconsistent information. Three common string methods include:
  • lower()
  • strip()
  • replace()
There are many more Python string methods, but we’ll start with these for now. Before we explain how these work, let’s see them in action!
āœļø
Try-It | String Methods
Run the code snippet below and observe the output. Be prepared to share your responses to the following questions.
  • What does lower() seem to do?
  • How does strip() change the text?
  • What happens when you use replace()?
  • How do these methods work together in cleaned_text?
text1 = "HeLLo, WoRLd!" print(text1.lower()) text2 = " Python is fun! " print(text2.strip()) text3 = "Coding is soooo cool!" print(text3.replace("soooo", "so")) text4 = " WELCOME to Python!!! " cleaned_text = text4.strip().lower().replace("!!!", "") print(cleaned_text)
Click here to see an explanation of each method!
  • The lower() method converts all letters in a string to lowercase. This is useful when comparing text or when you want to standardize text.
  • The strip() method removes any spaces, tabs, or newlines from the beginning and end of a string. This is helpful when cleaning up user input to remove accidental spaces before or after names.
  • The replace() method swaps out parts of a string for something else. This is useful when standardizing text or removing special characters from text.
With these built-in Python string methods, you’re ready to handle messy text like a pro!
Raw text is often messy and inconsistent. Unlike humans, computers don’t naturally understand language—they depend on clean, well-structured data to identify patterns and make accurate predictions. Because of this, it's common to remove unnecessary words and punctuation when preparing text for AI models. This helps the model focus on the most important words in a sentence. Let’s test your new knowledge of Python string methods with another Try-It exercise!
āœļø
Try-It | Clean Up Messy Text
Use Python string methods to clean up this messy text. Remove extra spaces from the beginning and the end of the string, convert all letters to lowercase, and remove all punctuation!
messy_text = " HeLlO, wOrLd! "

šŸ”¢Ā Numbers

Python has several data types to represent numbers, including integers and floats.
  • Integers are used to represent whole numbers (ex. 50)
  • Floats are used to represent fractional or irrational numbers (ex. 3.14)
# Integer Example print(10) # Float Example print(5.5) # This is a string (not a number) because it has quotation marks. Python will treat this like a word, not a number. print("15")
If we want to convert a float to an integer, we can pass our value to theĀ int function. Similarly, if we want to convert an integer to a float, we can pass our value to theĀ float function.
# convert float to integer int(1.999) # convert integer to float float(4)
The code above will turn the float (1.999) into an integer (1). Similarly, the integer (4) will now include a value after the decimal (4.0).
If we want to convert a number into a string, we can pass our value into theĀ str function.
# convert float to string str(1.999)
The program will now treat 1.999 as a string instead of a number. In which case, you could no longer perform math operations with this value.

āž—Ā Python as a Calculator

Python is perfectly suited to do basic calculations. Apart from addition, subtraction, multiplication, and division, there is also support for more advanced operations.
notion image
āœļø
Try-It | Numbers
Run the code below to see the answers to the following calculations:
# Addition, subtraction print(5 + 5) print(5 - 5) # Multiplication, division, and exponentiation print(3 * 5) print(10 / 2) print(4 ** 2)
Python evaluates every mathematical expression by following the PEMDAS ruleset.
PEMDAS = parentheses (or brackets), exponents, multiplication, division, addition, subtraction
Now let's try to combine operators:
lucky_number = 2 * (45 - 2**2) - (12 / 2) + 1 #Do not remove brackets from the print function print(lucky_number)
Click here to see the solution!
This code snippet prints the number 77.0.
āœļø
Try-It | Numbers & Calculations
Complete the following in a new code cell. After you type the code, make sure to run the cell!
  • Create an expression that equals 100
  • Write an expression with two or more operators
  • Create an expression using a variable
šŸŒ¶ļø Spicy Challenge: How would Python evaluate the following expression?
magic_number = (20 - 3 * 5) ** 2 - (50 / 2) + (15 % 4)

šŸ‘Ā Booleans

Booleans (pronounced boo-lee-ehn-s) represent either aĀ TrueĀ orĀ FalseĀ value. We can use booleans to represent real-world dichotomies such as:
  • Yes or No
  • Correct or Incorrect
  • Left or Right
  • On or Off
is_member = True print(is_member)
This statement will return the boolean value True.
When you compare two values, the expression is evaluated usingĀ Boolean comparison operators, and Python returns a Boolean answer:
print(20 > 10) print(100 == 50) print(10 < 5)
These statements will return True, False, False.
A combination of boolean expressions can create more complex comparisons. To combineĀ boolean values we can use the "Boolean Operators"Ā and,Ā or, andĀ not. Let's imagine a gym offers its members four sessions with a personal trainer per month. The code snippet below demonstrates how to combine boolean expressions to determine if a user is still eligible for more personal training sessions this month.
is_member = True pt_sessions = 4 is_eligible = is_member and pt_sessions < 4 print(is_eligible)
The print statement will return False. Even though is_member is assigned to True, the and operator requires that both parts of the expression evaluate to True in order to return Truefor the entire expression. In this case, the comparison pt_sessions < 4 evaluates to False, so the return value is False.

šŸŽĀ Containers

A container is a data structure that holds multiple pieces of data in a single object. In this lesson, we will learn about two collection data types: lists and dictionaries.

šŸ“œĀ Lists

A list is one of the most versatile data types available in Python. It is a data type that can contain any number of items of any data type. To define a list, we use square bracketsĀ [], and each item must be separated by a comma. Each item in a list can be of a different type which means that a list is not constrained to only containing a collection of one data type.
empty_list = [] print(empty_list) # You can also use the list function to create a list. empty_list = list() print(empty_list)
More examples of lists:
# List containing only string types food = ['pizza', 'sushi'] # List containing only numbers numbers = [20,10,5] # List containing multiple types multitype = ['pasta', 100, 1.25]

Accessing Items in a List

The position or location of an item in a list is known as itsĀ index. It is important to know that the index of a list must be an integer value, and the first value in a list has an index ofĀ zero. For example, the first item lives at position zero, the second item lives at position one, and so on. To access an item, we can specify its index by calling the list name and denoting the index operatorĀ [].
# Creating a list of colors colors = ['blue', 'pink'] # printing item in list based on index print(colors[0]) print(colors[1])
The first print statement will return blue. The second print statement will return pink.

Slicing

Slicing a list gives us another list that is a subset of the original list, instead of just one single element. The basic syntax for slicing is array[start:stop:step]. The slice creates and returns a new list from the index start up to (but not including) index stop. The step is 1 by default, meaning every element is included, but can be modified to skip elements.
# create a list letters = ['a', 'b', 'c', 'd', 'e','f'] # slice out the first 2 elements (elements at index 0 and 1) print(letters[0:2]) # -> ['a', 'b'] # slice out the middle 2 elements print(letters[2:4]) # -> ['c', 'd'] # take the last 2 elements (starting at index 4 to the end of the list) print(letters[4:]) # -> ['e', 'f'] # can also use the negative indices for slicing print(letters[-2:]) # -> ['e', 'f'] # print every other element print(letters[::2]) # -> ['a', 'c', 'e']

Slicing and Indexing on Strings

Indexing and slicing work identically with strings. We can pull out certain characters from a string to create a substring!
my_string = "Karlie Kloss" # Pull out the first character of the string print(my_string[0]) # Pull out the last character of the string print(my_string[-1]) # Creating a substring of only some characters print(my_string[6:10]) # Creating a substring of only some characters print(my_string[-12:-7])
The output of the code above looks like this:
notion image
Ā 

List Operations

Lists can contain any number of items. Due to this fact, we do not want to create a new list every time we want to make a change, as this could be very expensive in computation time. To make updating lists easier, Python allows lists to be mutable, meaning that we can modify the original list at any given time.
To update a list, we can use different list methods, such asĀ appendĀ orĀ pop. TheĀ appendĀ method will add an item to the end of a list whileĀ popĀ will remove the item at a given index.
# Add an item my_list = [] my_list.append('Value') print(my_list) # Remove an item my_list.pop(0) print(my_list)
The output of the code above looks like this:
notion image
As you can see in the example above, we used two different list methods to update our list. Let's unpack the syntax behind our example. If we translate our code into English terms, it would read as followed:
AppendĀ the string "Value" to the list stored in the variable called "my_list"
To properly use a list method, we must specify the following:
  • The name of the list we intend to update
  • aĀ .Ā to show that we are using a method to update the list
  • The name of the method we want to use and the appropriate parameters:
    • If using theĀ appendĀ method, include what we are adding to the list in quotation marks.
    • If using theĀ popĀ method, include the index of the item we are removing.
āœļø
Try-ItĀ | Lists
  1. Create a new, empty list calledĀ scholars.
  1. Append your name to the list as a string.
  1. Append the following names as strings (in order):
      • Karlie
      • Serena
      • Miley
      • Ariana
      • Selena
  1. Sort the list, then print the list to view the newly sorted list.Ā Hint
  1. Reverse the list, then print to view the reversed list.Ā Hint
  1. Add an additional person to the list, then print the updated list.
Ā 

šŸ“šĀ Dictionaries

A dictionary is a data type that is similar to a list, but instead of storing data by position, it stores data by name. Storing data by name establishes the ability to have an extremely fast lookup time, as we do not have to iterate over the collection of data to search for the value we are looking for.
We define an empty dictionary using curly bracketsĀ {}
empty_dictionaryĀ =Ā {}
Items in dictionaries are in the form of key-value pairs. AĀ keyĀ is a name, and aĀ valueĀ is the content connected to that name. To define a dictionary we need to write something like:
my_dictionaryĀ =Ā {'key1':Ā 1,Ā 'key2':Ā [1,2,3]}
In the example above,Ā key1Ā andĀ key2Ā are the keys, andĀ 1Ā andĀ [1,2,3]Ā are the values.
The keys in a dictionary can be any immutable data type, but they must be unique. Keys are generally primitive types, such as strings or numbers. The values can be any data type, including lists, as shown above.
# Empty dictionary myempty_dictionary = {} # Equivalently myempty_dictionary = dict() print(dict())
The output of the code above is {}.

Accessing Dictionary Values

Instead of using an index number to access values in a dictionary, we use the key for the value we want to reference. By default, dictionaries do not structure their content in any particular order.
# Access Data in a Dictionary scholar_hometowns = {"Karlie": "Chicago", "Serena": "Los Angeles"} print(scholar_hometowns["Karlie"])
The output of the code above is Chicago.
type(scholar_hometowns)
The output of the code above is dict.
If we want to update our dictionary to store an additional value, we can use the assignment operatorĀ =Ā after we have indicated the specific key we want to add or update.
In our example above, let's say that there was a typo with Serena's hometown, and we want to update our dictionary to contain the proper value for Serena's hometown.
scholar_hometowns["Serena"] = "Saginaw" print(scholar_hometowns)
The output of the code above looks like this:
notion image
To update our hometown dictionary to have an additional key-value pair, we can do the same as above, except we index with a new key instead of using an existing key.
scholar_hometowns["Zendaya"] = "Oakland" print(scholar_hometowns)
The output of the code above looks like this:
notion image

🪺 Nested Data Structures

In programming, a nested data structure is a data structure that contains other data structures inside it. Think of it like a backpack that holds smaller pouches, each containing different supplies. Two common types of nested structures include:
  1. Lists of lists – a list where each element is also a list
  1. Dictionaries with nested structures – a dictionary where some values are also dictionaries (or lists)

Lists of Lists

Imagine you’re organizing your favorite playlists. Each playlist is a list of songs and all of your playlists are organized in a single list. Let’s take a look at an example!
playlists = [ ["Enchanted", "Cool", "Feather"], ["Shake It Off", "Physical", "Espresso"], ["Love Story", "New Rules", "Thumbs"] ]
The playlists list has three elements, each one a list of songs on a playlist! If you want to access a specific song from this list, you need to select playlist first and then the song. For example, to access ā€œEspressoā€, use playlists[1][2].

Dictionaries with Nested Structures

Now, let’s imagine a Spotify user profile that includes their username, saved playlists, favorite artists and listening stats.
user_profile = { "username": "MusicLover23", "email": "musiclover23@example.com", "subscription": "Premium", "playlists": { "Chill Vibes": ["Enchanted", "Cool", "Feather"], "Hype Mode": ["Shake It Off", "Physical", "Espresso"], "Throwbacks": ["Love Story", "New Rules", "Thumbs"] }, "top_artists": ["Taylor Swift", "Dua Lipa", "Sabrina Carpenter"], "listening_stats": { "minutes_listened": 34567, "most_played_song": "Espresso", "favorite_genre": "Pop" } }
Nested data structures are everywhere in the real world, helping apps and websites keep things organized behind the scenes. Think about your favorite music streaming app—it needs to track your profile, playlists, favorite artists, and listening history, all neatly stored in a way that makes sense. Social media works the same way, with each user having a profile, a list of friends, and posts that contain comments and likes. Even online shopping relies on nested data—your shopping cart isn’t just a list of items; each item has details like price, size, and color choices. By using nested structures, programmers can keep everything structured and easy to access, making apps faster, smarter, and more personalized for you!
āœļø
Try-It | Nested Data Structures
Using the user_profile dictionary above, figure out how to access the following values. Remember to use print() to see the output!
  1. The user’s subscription type.
  1. The total minutes the user has listened to music.
  1. The first song in the Throwbacks playlist.
  1. The second artist in their top_artists list.
Now that we’ve covered the basics of Python data types and data structures, let’s practice these new skills! If you finish the practice early, make sure you check out the Extensions below.
šŸ“
Practice | Data Types & Containers
Create a new code cell block to complete the exercises below.
  1. Create a dictionary calledĀ meĀ that contains the following key-value pairs:
      • "name": where the value is your name as aĀ string
      • "age": where the value is your age as aĀ integer
  1. Print your name from the dictionary.
  1. Add a new key-value pair to yourĀ meĀ dictionary, where the key is theĀ stringĀ "hobbies" and the value is an empty list.
  1. Append a hobby (or two!) as a string to your hobbies list.
šŸŒ¶ļøšŸŒ¶ļøšŸŒ¶ļø Spicy Challenge: Use your online sleuthing skills to learn about string interpolation. Print the statement "My name is [YOUR NAME]" by combining a string with your dictionary!
šŸ¤–Ā AI Connection
If you encounter an error message you don't understand, try copying it into an AI tool with the prompt: "I'm a beginner learning Python. Can you explain what this error message means and give me a hint about how to fix it without writing the code for me?" Understanding error messages is one of the most important skills a programmer can build, and AI is a great tool for translating them into plain language.

Extensions

šŸŒ¶ļøĀ  Mutability

All data types in Python are either mutable or immutable, meaning that theyĀ canĀ orĀ cannotĀ be modified after they have been created. The general rule is that simple data types are immutable while more complex data types are mutable. An example of an immutable type is aĀ stringĀ or aĀ number. Something that can be more complex in structure is aĀ list, making it a prime example of a mutable data type.
šŸ’”
It is important to note that mutability does not affect variables; it only affects values stored inside variables.

Immutable Types

When we store an immutable type, the value itself is stored in the variable. However, due to its immutability, we cannot change that value. We can only change the value of the variable by setting it to a new value.
my_str = 'abc' #let's say I want to change my_str to be 'adc' my_str[1] = 'd'
The output of the code above will be an error statement.
# I can only do that by redefining what my_str is my_str = 'abc' my_str = 'adc' print(my_str)
The output of the code above is adc.

Mutable Types

When we store a value in a variable, that value is given an address that references its location in Python memory. To access a mutable type, Python will use this address to locate our data where we can retrieve or update the current value. When modifying a mutable type, the change occurs directly on the value instead of creating a new value as immutable types do.
my_list = [1, 2, 3, 4, 5] print(my_list) # we can change what is stored in my_list my_list[1] = 'new item' print(my_list)
The output of the code above looks like this:
notion image
# we can also change the list by using a method my_list.append(3.14) print(my_list)
The output of the code above looks like this:
notion image

Tuples

Another container in Python is the tuple. This is an immutable version of the list, meaning we cannot change the tuple once it has been set. Tuples are defined with parenthesesĀ ().
my_tuple = (1,2,3,4,5,'a string',4.99) type(my_tuple)
The output of the code above is tuple.
my_tuple[1] = 9 my_tuple.append('new item')
Both code statements in the block above will return an error message. This is because tuples are immutable!
page icon
For a summary of this lesson, check out the 2. Python: Data Types & Containers One-Pager!
Ā