Day 13: Python Tuples
Understanding Tuples in Python
Tuples in Python are used to store a collection of items. They are similar to lists, but they are immutable, meaning that their items cannot be modified once they are created. To create a tuple in Python, enclose a comma-separated list of items in parentheses. You can access and unpack the items in a tuple using their index or using multiple assignment. Tuples are often used to return multiple values from a function, and they can also be used as keys in dictionaries since they are immutable.
Creating Tuples in Python
In the above example, we have created three tuples: numbers
, words
, and mixed
.
Accessing and Unpacking Tuples in Python
numbers = (1, 2, 3, 4, 5)
print(numbers[0]) # Output: 1
print(numbers[2]) # Output: 3
x, y, z, _, _ = numbers
print(x, y, z)
# Output: 1 2 3
In this example, we access items in the numbers
tuple using their index. We also unpack the numbers
tuple using multiple assignment.
By understanding how to create, access, and unpack tuples in Python, you can effectively manage collections of immutable items in your code. Stay tuned for more Python-related content.
Learn More
Want to learn more about Python for Machine Learning? Check out the full course HERE.
Need help mastering Machine Learning?
Don't just follow along — join me! Get exclusive access to me, your instructor, who can help answer any of your questions. Additionally, get access to a private learning group where you can learn together and support each other on your AI journey.