B.Tech Students: Apply for Live Programming Internship C, C++, Java, Python ,Web page Designing, PHP Python Tuples Tutorial with Examples | LiveCodeProgramming
LiveCodeProgramming

Python Tuples

A tuple is a collection in Python which is ordered and immutable. Tuples are written using parentheses ().

Tuple Python का एक collection है जो ordered और immutable होता है। इसे () में लिखा जाता है।

1. Creating a Tuple
students = ("Abhishek", "Chaitanya", "Naincy", "Shrutika", "Jayati", "Aryan")
print(students)
Output:
('Abhishek', 'Chaitanya', 'Naincy', 'Shrutika', 'Jayati', 'Aryan')
2. Accessing Tuple Items
print(students[1])
Output:
Chaitanya
3. Loop Through Tuple
for name in students:
    print(name)
Output:
Abhishek
Chaitanya
Naincy
Shrutika
Jayati
Aryan
4. Check if Item Exists
if "Aryan" in students:
    print("Yes, Aryan is in the tuple")
Output:
Yes, Aryan is in the tuple
5. Tuple Length
print(len(students))
Output:
6
6. Tuple with One Item
single = ("Aryan",)
print(type(single))
Output:
<class 'tuple'>
7. Tuple Concatenation
group1 = ("Abhishek", "Jayati")
group2 = ("Naincy", "Shrutika")
combined = group1 + group2
print(combined)
Output:
('Abhishek', 'Jayati', 'Naincy', 'Shrutika')
8. Slice a Tuple
print(students[1:4])
Output:
('Chaitanya', 'Naincy', 'Shrutika')