Search the courses, subjects, tracks, games and features, or jump straight to a page.
Learn it your way
A short class with Sarah and her students. The other ways are under More.
Sign up free to listen. Transcripts are free to read.
Today, we're going to learn about lists in Python. They are mutable, meaning you can change them after they're created. Can anyone give me an example of a list?
How about a list of my favorite foods?
Or we could do a list of animals!
Exactly! We can define a list like this: fruits = ["apple", "banana", "cherry"]. You can also add more items with append. Can someone demonstrate this?
Sure! If I add fruits.append("orange"), it will be added to the list.
Great job! Remember that lists are defined with square brackets and can be modified. When you think of lists, think 'Mutable!'
Now let's discuss tuples. Unlike lists, tuples are immutable. Can anyone tell me what that means?
It means we can't change them after they've been created!
Exactly! A tuple can be created like this: colors = ("red", "green", "blue"). So, if I wanted to access the first element, I’d write colors[0] and it will give me 'red'. Why do you think we'd want to use tuples instead of lists?
Maybe for data we don't want to change, so it stays the same?
Correct! Tuples are perfect for data integrity because you can't alter them. Think 'Immutable!' when you think of tuples.
Let's compare lists and tuples. If I wanted a collection of items that I'd use a lot and change frequently, which would I choose?
A list, because you can change it!
And if I just needed to store data without changing it, I'd use a tuple?
Exactly! Lists are great for collections that change, while tuples are better for fixed collections. A good mnemonic to remember is 'List - Mutable; Tuple - Immutable.' Why does this distinction matter?
It helps us choose the right data structure for our needs!
Awesome! Remember this whenever you're coding.
Lists can be changed, just like a game; tuples are set, they stay the same.
Imagine two friends, Listy and Tuppy. Listy loves to change his plans, while Tuppy sticks to his. That's how lists and tuples work!
To remember: 'List - Mutable,' 'Tuple - Immutable.' Just think 'L-M, T-I'.
LIT: Lists are 'L'iquids and flexible, while Tuples are 'I'nvariable.
Term
What is a list?
Click to flip
Term
What is a tuple?
Click to flip
Term
How do you add an item to a list?
Click to flip
Term
Are tuples mutable?
Click to flip
A mutable collection of items in Python, defined by square brackets.
An immutable collection of items in Python, defined by parentheses.
An attribute indicating that an object can be changed after it is created.
An attribute indicating that an object cannot be changed after it is created.
Lists are mutable collections of items, while tuples are immutable collections.
This section introduces two fundamental data structures in Python: lists and tuples. It highlights their characteristics, such as mutability in lists and immutability in tuples, providing examples to illustrate their use in programming.
In Python, lists and tuples are essential data structures that allow you to store collections of items.
Lists are defined using square brackets and can be modified, meaning you can change, add, or remove items after their creation. For example, you might create a list of fruits fruits = ["apple", "banana", "cherry"] and then add another fruit, such as fruits.append("orange"). Lists are great for handling data when modification is frequent.
On the other hand, tuples are defined using parentheses and are immutable, meaning once they are created, their content cannot be changed. For instance, a tuple of colors could be defined as colors = ("red", "green", "blue"). The immutability of tuples makes them useful for fixed collections of items where data integrity is important. In this section, you'll understand the core differences between these two structures and when to use each to effectively manage data in your Python programs.