Learn Python
- Python basic
- Introduction to File Handling
- Basics of List Comprehension
- Introduction to Matplotlib
- Classes and Objects
- Introduction to Functions
- Python Numbers
- Creating Basic Plots
- Opening and closing files
- Function parameters and arguments
- Advanced Techniques
- Attributes and Methods
- Python Strings
- Scope and lifetime of variables
- Advanced Plotting
- Reading from files
- Performance and Limitations
- Encapsulation
- Python List
- Specialized Plots
- Writing to files
- Return statement and output
- Inheritance
- Python Tuple
- Advanced Customization
- Working with different file formats
- Lambda Functions
- Polymorphism
- Practical Applications
- Higher-Order Functions
Python Tuple
Introduction to Tuples
Tuples in Python are used to store collections of elements, similar to lists. However, there's a crucial difference: tuples are immutable. This means once you create a tuple, you cannot change the elements inside it.
Elements in a tuple maintain a specific order, just like in lists. You can access elements by their position (index).
Unlike lists, you cannot modify elements after creating a tuple. You can't add, remove, or change the values within the tuple.
Unlike lists, you cannot modify elements after creating a tuple. You can't add, remove, or change the values within the tuple.
Think of tuples like a shopping cart: You can add items (elements) to your cart, and they have a specific order (first item added, second item added, etc.). However, once you check out (create the tuple), you can't take items out or change them.
Here's an example to illustrate the difference between tuples and lists:
Creating a list:
my_list = ["apple", "banana", "cherry"] # Modifying the list (possible) my_list[1] = "orange" # Change "banana" to "orange" print(my_list) # Output: ["apple", "orange", "cherry"]
Creating a tuple:
my_tuple = ("apple", "banana", "cherry") # Trying to modify the tuple (not possible) # my_tuple[1] = "orange" # This will cause an error print(my_tuple) # Output: ("apple", "banana", "cherry")
As you can see, modifying the list element was successful, while attempting to modify the tuple element resulted in an error.
Why use tuples?
Even though they're immutable, tuples offer several advantages:
- Since tuples are fixed in size, Python can optimize them for faster performance compared to mutable lists.
- Immutability ensures data remains unchanged after creation, making tuples reliable for storing fixed data sets.
- Using tuples clearly communicates that the data should not be modified, improving code readability.
It's time to take a quiz!
Test your knowledge and see what you've just learned.
What is the main difference between a list and a tuple in Python?
CTuples are always ordered; lists are not.
DLists can only store numbers; tuples can store any type.
ALists are mutable; tuples are immutable.
BTuples can have duplicate elements; lists cannot.
Check Answer
Can you change an element in a tuple after it is created?
AYes, you can change elements.
BOnly the first element can be changed.
COnly the last element can be changed.
DNo, tuples are immutable.
Check Answer
What is one advantage of using tuples over lists?
CTuples allow for faster modification of elements.
DTuples can contain more elements than lists.
ATuples have a fixed size and can be optimized for performance.
BTuples can store different types of data.
Check Answer
How do you access the second element in a tuple named "my_tuple"?
Amy_tuple[1]
Bmy_tuple(1)
Cmy_tuple{1}
Dmy_tuple{2}
Check Answer