Rupaalife 👈
Tuple in Python
1. What is a Tuple?
A Tuple in Python is a built-in data type used to store multiple values in a single variable,
similar to a list.
The key difference is:
Tuples are immutable, which means their values cannot be changed after creation.
2. Why Do We Need a Tuple?
Tuples are used when:
Data should not be modified accidentally
Data is fixed and permanent
Safety and data integrity are important
Example:
Days of the week
Months in a year
Coordinates (latitude, longitude)
3. How to Create a Tuple
A tuple is created using round brackets ( ).
Syntax:
tuple_name = (value1, value2, value3)
Example:
numbers = (10, 20, 30)
4. Tuple with Different Data Types
A tuple can store multiple data types.
Example:
employee = ("Ravi", 28, 45000.75, True)
Here:
Name → string
Age → integer
Salary → float
Active → boolean
5. Real-Time Example
Bank Account Details
Bank account number and IFSC code should not change.
Python representation:
bank_details = ("1234567890", "SBIN0001234")
Since these values must remain fixed, tuple is the best choice.
6. Accessing Tuple Elements (Indexing)
Tuple elements are accessed using index numbers, starting from 0.
Example:
colors = ("Red", "Blue", "Green")
print(colors[1])
Output:
Blue
7. Tuple is Immutable (Important Concept)
Once a tuple is created, we cannot change its values.
Example:
colors = ("Red", "Blue", "Green")
colors[1] = "Yellow"
Output:
TypeError: 'tuple' object does not support item assignment
This proves tuples are immutable.
8. Tuple with One Element (Important)
To create a tuple with one element, a comma is mandatory.
Correct way:
single_value = (10,)
Wrong way:
single_value = (10)
Without a comma, Python treats it as an integer, not a tuple.
9. Tuple Operations
Length of Tuple
numbers = (10, 20, 30)
print(len(numbers))
Tuple Concatenation
a = (1, 2)
b = (3, 4)
print(a + b)
Output:
(1, 2, 3, 4)
10. Converting List to Tuple
Example:
marks = [85, 90, 88]
marks_tuple = tuple(marks)
11. Difference Between List and Tuple
Feature List Tuple
Brackets [ ] ()
Mutable Yes No
Speed SlowerFaster
Safety Less More
Use Case Changeable data Fixed data
12. One-Line Definition (Exam Oriented)
A tuple is a built-in data type in Python used to store multiple values in an ordered and
immutable collection.
13. When to Use Tuple
Use tuple when:
Data should remain constant
Performance is important
Data should not be modified
14. Conclusion
Tuples are similar to lists but provide data safety and faster performance.
They are best used for fixed collections of data that should not be changed.