Logo
Unit 5 – Python Data Structures: Lists and Tuples

Python Data Structures: Lists and Tuples

Duration: 5 minutes

Python’s beauty is in its powerful yet simple data structures. Lists and tuples are two built-in data structures for storing collections of objects. Let’s learn about them and how they work!

Lists

A list in Python is a mutable and ordered collection of items, enclosed within square brackets []. Creating a List:

fruits = ["apple""banana""cherry"]
print(fruits)  # Output: ['apple', 'banana', 'cherry']

Accessing List Items:

print(fruits[1])  # Output: 'banana'

Modifying List Items:

fruits[2] = "mango"
print(fruits)  # Output: ['apple', 'banana', 'mango']

Tuples

In Python, a tuple is comparable to a list, but it is immutable, which means that once constructed, the things within it cannot be modified. Within parenthesis **(), **tuples are defined.

Making a Tuple:

colors = ("red""green""blue")
print(colors)  # Output: ('red', 'green', 'blue')

Accessing Tuple Items:

print(colors[2])  # Output: 'blue'

Note: Since tuples are immutable, we can’t modify their items like we can with lists.#### AssignmentIt’s time to explore lists and tuples. Make a list and tuple each of your favorite movies. Use your list and tuple to access various elements. Try modifying a list element and then observe what happens when you try the same thing with a tuple.

Conclusion

Lists and tuples are two essential Python data structures for storing and manipulating collections of objects. Understanding these structures is crucial because they serve as the foundation for many complicated data operations. Remember, the more you practice, the more comfortable you’ll grow with Python data structures!

Next Tutorial: Python Data Structures: Dictionaries and Sets

5 minutes Minutes

Continue

Code on the Go with our Mobile App!

Unleash your coding potential anytime, anywhere!

Download Now!