Topic 13: Tuple

Tuple in Python?

In Python, tuples are one of the most important data types used to store multiple items in a single variable. They are similar to lists, but tuples are immutable (अपरिवर्तनीय), meaning their elements cannot be changed once assigned.

Tuples are often used when you want to store a fixed collection of items that should not be modified during program execution.

What is a tuple in Python?

A tuple is an ordered, immutable, and heterogeneous  (भिन्न प्रकार) collection of elements.

You can store different data types such as integers, strings, floats, and even other tuples inside a tuple.

Syntax:

tuple_name = (element1, element2, element3, ...)

Example:

my_tuple = (10, 20, 30, "Python", 3.14)
print(my_tuple)

Output:

(10, 20, 30, 'Python', 3.14)

Characteristics of Tuples

Feature Description
Ordered Elements have a defined order and can be accessed by index.
Immutable Once created, elements cannot be changed or removed.
Heterogeneous Can store elements of different data types.
Allow Duplicates  Same values can appear multiple times.

Creating Tuples

Example 1: Creating a Tuple

tuple1 = (1, 2, 3)
print(tuple1)

Example 2: Tuple with Mixed Data Types

tuple2 = (101, "Alice", 78.9)
print(tuple2)

Example 3: Nested Tuple

tuple3 = (1, 2, (3, 4, 5))
print(tuple3)

 Example 4: Tuple without Parentheses

You can also create a tuple without parentheses using commas.

tuple4 = 10, 20, 30
print(tuple4)

Accessing Tuple Elements

You can access elements using their index number, starting from 0.

my_tuple = ("apple", "banana", "cherry")
print(my_tuple[0])  # Output: apple
print(my_tuple[2])  # Output: cherry

Negative Indexing

print(my_tuple[-1])  # Output: cherry
print(my_tuple[-2])  # Output: banana

Tuple Slicing

Slicing allows you to get a subset of elements from a tuple.

numbers = (10, 20, 30, 40, 50)
print(numbers[1:4])   # Output: (20, 30, 40)
print(numbers[:3])    # Output: (10, 20, 30)
print(numbers[-3:])   # Output: (30, 40, 50)

Tuple Operations

1️⃣ Concatenation(जोड़ना , संयोजन )

tuple1 = (1, 2)
tuple2 = (3, 4)
print(tuple1 + tuple2)   # Output: (1, 2, 3, 4)

2️⃣ Repetition (दोहराव या पुनरावृत्ति)

tuple1 = ("Hi",)
print(tuple1 * 3)   # Output: ('Hi', 'Hi', 'Hi')

3️⃣ Membership Test

colors = ("red", "green", "blue")
print("red" in colors)    # True
print("yellow" not in colors)  # True

Inbuilt Functions for Tuples

Function Description Example
len()     Returns number of elements     len((1,2,3)) → 3
max()     Returns the largest value     max((2,5,1)) → 5
min()     Returns the smallest value     min((2,5,1)) → 1
sum()     Returns sum of all numbers     sum((5,10,15)) → 30
tuple()     Converts another type into tuple     tuple([1,2,3]) → (1,2,3)
count()     Returns count of specific element     (1,2,1,3).count(1) → 2
index()     Returns index of first occurrence     10,20,30).index(20) → 1

Iterating Through a Tuple

fruits = ("apple", "banana", "cherry")
for fruit in fruits:
    print(fruit)

Output:

apple
banana
cherry

Deleting a Tuple

You cannot delete specific elements, but you can delete the entire tuple.

my_tuple = (1, 2, 3)
del my_tuple
# print(my_tuple)  # Error: NameError: name 'my_tuple' is not defined

Visual Diagram – Tuple Concept

       
Tuple adn Accessing of tuple

Advantages of Tuples

  •  Faster than lists (due to immutability).

  •  Can be used as dictionary keys.

  •  Memory efficient.

  •  Data safety (immutable structure).

Real-Life Example

student = ("Ravi", 21, "B.Tech", 8.9)
print("Name:", student[0])
print("CGPA:", student[3])

Output:

Name: Ravi
CGPA: 8.9

Conclusion

Tuples in Python provide an efficient and secure way to store fixed sets of values. They are faster and safer than lists when you don’t need to modify data. By understanding tuple operations and built-in functions, you can write clean, efficient, and reliable Python programs.


         👈 Previous                                   ADD                                     Qiuz( tuple) 👉

Comments

Popular posts from this blog

Topic1 :- What is Python Programming?

Topic2: -Why Python?

Topic7: What is Numpy?