Tuples in Python

Python tutorial · PySpark.in

Tuple 

A tuple is a collection of items similar to a list, but with one major difference: Tuples are immutable.You cannot change, add, or remove items after creation.Tuples are commonly used when you want to store data that should remain constant.

Syntax

PLAINTEXT
1my_tuple = (item1, item2, item3)
▶ Output will appear here.

1. Creating a Tuple

A tuple is an ordered, immutable collection of items. It can store different data types.

PYTHON
1numbers = (10, 20, 30, 40)
2fruits = ("apple", "banana", "mango")
3mixed = ("Megha", 22, 5.5, True)
4print(numbers)
5print(fruits)
6print(mixed)
▶ Output will appear here.

2. Accessing Tuple Items

Retrieving elements from a tuple using index positions starting from 0.

PYTHON
1fruits = ("apple", "banana", "mango")
2print(fruits[0]) # apple
3print(fruits[2]) # mango
▶ Output will appear here.

3. Negative Indexing

Accessing tuple elements from the end using negative index values.

PYTHON
1fruits = ("apple", "banana", "mango")
2print(fruits[-1]) # mango
3print(fruits[-2]) # banana
▶ Output will appear here.

4. Tuple Slicing

Extracting a portion of a tuple using a range of indexes.

PYTHON
1numbers = (10, 20, 30, 40, 50)
2print(numbers[1:4]) # (20, 30, 40)
3print(numbers[:3]) # (10, 20, 30)
4print(numbers[2:]) # (30, 40, 50)
▶ Output will appear here.

5. Tuples are Immutable

Tuple items cannot be added, modified, or removed after creation.

PYTHON
1t = (1, 2, 3)
2t[1] = 100
3print(t) # ERROR: tuples cannot be modified
▶ Output will appear here.

6. You Can Convert Tuple → List → Tuple

A tuple can be temporarily converted to a list for modification and then back to a tuple.

PYTHON
1t = (10, 20, 30)
2temp = list(t)
3temp[1] = 200 # modify list
4t = tuple(temp)
5print(t) # (10, 200, 30)
▶ Output will appear here.

7. Looping Through a Tuple

Using a loop for to access each element in the tuple one by one.

PYTHON
1colors = ("red", "blue", "green")
2for c in colors:
3 print(c)
▶ Output will appear here.

8. Tuple Methods

Tuples have only two built-in methods:

count(): Counts occurrences of a value

PYTHON
1t = (1, 2, 2, 3, 2)
2print(t.count(2)) # 3
▶ Output will appear here.

index(): Return first index of a value

PYTHON
1t = ("a", "b", "c", "b")
2print(t.index("b")) # 1
▶ Output will appear here.

9. Nested Tuple

A tuple that contains other tuples inside it.

PYTHON
1nested = (("a", 1), ("b", 2))
2print(nested[1][0]) # b
▶ Output will appear here.

10. Tuple Packing & Unpacking

Tuple Packing :

Storing multiple values together into a single tuple.

Tuple Unpacking :

Assigning tuple items directly into separate variables.

PYTHON
1person = ("Rohan", 20, "BCA")
2print(person)
▶ Output will appear here.

11. Single Item Tuple

A tuple with one element must include a trailing comma to be recognized as a tuple.

PLAINTEXT
1t1 = (10) # Not a tuple
2t2 = (10,) # Tuple
▶ Output will appear here.

More Python tutorials

All tutorials · Try the free PySpark compiler · Practice challenges