Python

Python Tuples

A tuple is a built-in Python data type used to store multiple values in a single variable.

A tuple is similar to a list, but there is one major difference:

A list is mutable, while a tuple is immutable.

This means that once a tuple is created, its elements cannot be changed, added, or removed.

Tuples are useful when you want to store a collection of values that should remain unchanged.

Example

fruits = ("apple", "mango", "papaya", "cherry")

1. Characteristics of Tuples

Python tuples have several important characteristics:

  • Ordered — elements maintain their order.
  • Immutable — elements cannot be changed after creation.
  • Indexed — elements can be accessed using index numbers.
  • Allow duplicates — the same value can appear more than once.
  • Can contain different data types.
  • Can be sliced using indexes.
  • Can be nested inside other tuples or data structures.

2. Creating a Tuple

A tuple is normally created using parentheses ().

Example

fruits = ("apple", "mango", "papaya", "cherry")

print(fruits)

Output

('apple', 'mango', 'papaya', 'cherry')

3. Tuple with Different Data Types

A tuple can contain different types of values.

Example

data = ("Ram", 25, 5.8, True)

print(data)

Output

('Ram', 25, 5.8, True)

A tuple can contain:

  • Strings
  • Integers
  • Floats
  • Boolean values
  • Lists
  • Other tuples
  • Dictionaries
  • Other Python objects

4. Creating a Tuple Without Parentheses

Python also allows you to create a tuple without explicitly using parentheses.

Example

fruits = "apple", "mango", "papaya"

print(fruits)

Output

('apple', 'mango', 'papaya')

Python recognizes this as a tuple because of the comma-separated values.

5. Creating a Tuple Using tuple()

Python provides the tuple() constructor for creating tuples.

Example

fruits = tuple(("apple", "mango", "papaya"))

print(fruits)

Output

('apple', 'mango', 'papaya')

You can also convert another iterable into a tuple.

Example

fruits = ["apple", "mango", "papaya"]

new_fruits = tuple(fruits)

print(new_fruits)

Output

('apple', 'mango', 'papaya')

6. Single-Element Tuple

There is an important rule when creating a tuple with only one element.

This is not a tuple:

x = ("apple")

Python considers it a string.

To create a single-element tuple, you need a comma.

Correct Example

x = ("apple",)

print(x)
print(type(x))

Output

('apple',)
<class 'tuple'>

The comma is what makes it a tuple.

7. Finding the Length of a Tuple

The len() function returns the number of elements in a tuple.

Example

fruits = ("apple", "mango", "papaya", "cherry")

print(len(fruits))

Output

4

8. Checking the Tuple Data Type

You can use type() to check whether a variable contains a tuple.

Example

fruits = ("apple", "mango", "papaya")

print(type(fruits))

Output

<class 'tuple'>

9. Accessing Tuple Elements

Tuples use zero-based indexing, just like lists.

The first element has index 0.

Example

fruits = ("apple", "mango", "papaya", "cherry")

print(fruits[0])
print(fruits[1])
print(fruits[2])

Output

apple
mango
papaya

The indexes are:

Index:   0        1        2        3
Value: apple    mango    papaya   cherry

10. Accessing Characters Inside a Tuple Element

If a tuple contains strings, you can use another index to access a character inside a string.

Example

fruits = ("apple", "mango", "papaya")

print(fruits[1])
print(fruits[1][0])
print(fruits[2][1])

Output

mango
m
a

Explanation

fruits[1]

returns:

mango

Then:

fruits[1][0]

returns the first character of "mango":

m

11. Negative Indexing

Tuples also support negative indexing.

The last element has an index of -1.

Example

fruits = ("apple", "mango", "papaya", "cherry", "grapes")

print(fruits[-1])
print(fruits[-2])

Output

grapes
cherry

The indexes are:

Positive:  0       1        2        3        4
Value:    apple   mango   papaya   cherry   grapes
Negative: -5      -4       -3       -2      -1

12. Tuple Slicing

Tuple slicing works in the same way as list slicing.

Syntax

tuple[start:end]

The starting index is included, while the ending index is excluded.

Example

fruits = ("apple", "mango", "papaya", "cherry", "grapes", "kiwi")

print(fruits[2:5])

Output

('papaya', 'cherry', 'grapes')

13. Leaving Out the End Index

print(fruits[2:])

This returns everything from index 2 to the end.

Output

('papaya', 'cherry', 'grapes', 'kiwi')

14. Leaving Out the Starting Index

print(fruits[:4])

This returns elements from the beginning up to, but not including, index 4.

Output

('apple', 'mango', 'papaya', 'cherry')

15. Negative Tuple Slicing

You can also use negative indexes for slicing.

Example

fruits = ("apple", "mango", "papaya", "cherry", "grapes", "kiwi")

print(fruits[-4:-1])

Output

('papaya', 'cherry', 'grapes')

16. Tuples Are Immutable

The most important difference between a list and a tuple is immutability.

Once a tuple is created, you cannot directly change its elements.

Example

fruits = ("apple", "mango", "papaya")

fruits[1] = "banana"

This produces an error:

TypeError: 'tuple' object does not support item assignment

This happens because tuples cannot be modified after creation.

17. List vs Tuple

List

fruits = ["apple", "mango", "papaya"]

fruits[1] = "banana"

print(fruits)

Output:

['apple', 'banana', 'papaya']

Tuple

fruits = ("apple", "mango", "papaya")

fruits[1] = "banana"

This produces an error because tuples are immutable.

Comparison

FeatureListTuple
Syntax[]()
OrderedYesYes
MutableYesNo
Allows duplicatesYesYes
IndexedYesYes
SlicingYesYes
Can contain different data typesYesYes

18. Adding Elements to a Tuple

You cannot directly use append() or insert() with a tuple.

For example:

fruits = ("apple", "mango", "papaya")

fruits.append("banana")

This produces an error because tuples do not have an append() method.

However, you can create a new tuple by combining tuples.

Example

fruits = ("apple", "mango", "papaya")
new_fruits = fruits + ("banana",)

print(new_fruits)

Output

('apple', 'mango', 'papaya', 'banana')

Notice the comma after "banana" because it is a single-element tuple.

19. Removing Elements from a Tuple

You cannot directly remove an element from a tuple.

For example:

fruits = ("apple", "mango", "papaya")

fruits.remove("mango")

This will produce an error because tuples do not have a remove() method.

If you need to modify the data, you can convert the tuple to a list.

Example

fruits = ("apple", "mango", "papaya")

temp = list(fruits)

temp.remove("mango")

fruits = tuple(temp)

print(fruits)

Output

('apple', 'papaya')

However, remember that after conversion, the original tuple itself was not modified. We created a new tuple.

20. Checking Whether an Element Exists

The in operator can be used to check whether a value exists in a tuple.

Example

fruits = ("apple", "mango", "papaya", "cherry")

print("mango" in fruits)
print("banana" in fruits)

Output

True
False

You can also use not in.

print("banana" not in fruits)

Output:

True

21. Counting Values Using count()

The count() method returns the number of times a particular value appears in a tuple.

Example

fruits = ("apple", "mango", "papaya", "mango", "cherry", "mango")

print(fruits.count("mango"))

Output

3

The value "mango" appears three times.

22. Finding an Index Using index()

The index() method returns the index of the first occurrence of a specified value.

Example

fruits = ("apple", "mango", "papaya", "cherry")

print(fruits.index("papaya"))

Output

2

The value "papaya" is located at index 2.

23. Tuple Methods

Because tuples are immutable, they have fewer methods than lists.

The two main tuple methods are:

MethodDescription
count()Counts how many times a value appears
index()Returns the index of the first matching value

Example

numbers = (10, 20, 10, 30, 10, 40)

print(numbers.count(10))
print(numbers.index(30))

Output

3
3

24. Unpacking a Tuple

Tuple unpacking allows you to assign tuple elements to separate variables.

Example

fruits = ("apple", "mango", "papaya")

a, b, c = fruits

print(a)
print(b)
print(c)

Output

apple
mango
papaya

Python assigns:

a → apple
b → mango
c → papaya

The number of variables should normally match the number of tuple elements.

25. Using * in Tuple Unpacking

The * operator can collect multiple remaining values into a list.

Example

fruits = ("apple", "mango", "papaya", "cherry", "kiwi")

a, *b, c = fruits

print(a)
print(b)
print(c)

Output

apple
['mango', 'papaya', 'cherry']
kiwi

Here:

  • a receives the first element.
  • c receives the last element.
  • b receives the remaining elements as a list.

26. Nested Tuples

A tuple can contain another tuple.

This is called a nested tuple.

Example

student = (
    "Ram",
    25,
    ("Python", "Django", "SQL")
)

print(student)

You can access the nested tuple using multiple indexes.

print(student[2][0])

Output

Python

27. Converting Between List and Tuple

You can convert a list into a tuple using tuple().

List to Tuple

fruits = ["apple", "mango", "papaya"]

fruits_tuple = tuple(fruits)

print(fruits_tuple)

Tuple to List

You can convert a tuple into a list using list().

fruits = ("apple", "mango", "papaya")

fruits_list = list(fruits)

print(fruits_list)

Output

['apple', 'mango', 'papaya']

This is useful when you need to temporarily modify tuple data.

28. When Should You Use a Tuple?

Use a tuple when:

  • The data should not be changed.
  • You want to represent a fixed collection of values.
  • You want to protect data from accidental modification.
  • You are working with fixed records.
  • You need to return multiple values from a function.
  • You want to use the collection as a dictionary key, when all contained elements are hashable.

Example

Coordinates are a good example:

location = (27.7172, 85.3240)

print(location)

The latitude and longitude represent a fixed pair of values, so a tuple is appropriate.

29. Complete Copy Code

# ==========================================
# Python Tuples
# ==========================================


# Creating a tuple
fruits = ("apple", "mango", "papaya", "cherry")

print(fruits)


# Tuple with different data types
data = ("Ram", 25, 5.8, True)

print(data)


# Tuple without parentheses
colors = "red", "green", "blue"

print(colors)


# Creating a tuple using tuple()
list_data = ["apple", "mango", "papaya"]

fruit_tuple = tuple(list_data)

print(fruit_tuple)


# Single-element tuple
single = ("apple",)

print(single)
print(type(single))


# Length
print(len(fruits))


# Type
print(type(fruits))


# Accessing elements
print(fruits[0])
print(fruits[1])


# Accessing character inside an element
print(fruits[1][0])
print(fruits[2][1])


# Slicing
print(fruits[1:3])
print(fruits[2:])
print(fruits[:3])


# Negative indexing
print(fruits[-1])
print(fruits[-2])


# Negative slicing
print(fruits[-3:-1])


# Checking whether a value exists
print("mango" in fruits)
print("banana" in fruits)


# count()
numbers = (10, 20, 10, 30, 10, 40)

print(numbers.count(10))


# index()
print(numbers.index(30))


# Tuple unpacking
a, b, c, d = fruits

print(a)
print(b)
print(c)
print(d)


# Combining tuples
tuple1 = ("apple", "mango")
tuple2 = ("papaya", "cherry")

combined = tuple1 + tuple2

print(combined)


# Repeating a tuple
numbers = (1, 2, 3)

repeated = numbers * 2

print(repeated)


# Converting tuple to list
fruits = ("apple", "mango", "papaya")

temp = list(fruits)

temp.append("banana")

fruits = tuple(temp)

print(fruits)

 

30. Expected Output

Original tuple: ('apple', 'mango', 'papaya', 'cherry', 'grapes')
Length: 5
Type: <class 'tuple'>
First element: apple
Last element: grapes
Slice: ('mango', 'papaya', 'cherry')
Is mango present? True
Is banana present? False
Count of mango: 1
Index of papaya: 2
After adding: ('apple', 'mango', 'papaya', 'cherry', 'grapes', 'banana')
Repeated tuple: (1, 2, 3, 1, 2, 3)
First: apple
Second: mango
Third: papaya
Fourth: cherry
Fifth: grapes

31. Practice Exercise

Create a tuple containing the names of five programming languages.

For example:

languages = ("Python", "JavaScript", "Java", "PHP", "C++")

Then perform the following:

  1. Print the complete tuple.
  2. Print the length of the tuple.
  3. Print the first language.
  4. Print the last language using negative indexing.
  5. Print the first three languages using slicing.
  6. Check whether "Python" exists in the tuple.
  7. Find the index of "JavaScript".
  8. Count how many times "Python" appears.
  9. Create another tuple containing two languages.
  10. Combine both tuples using +.
  11. Convert the tuple into a list.
  12. Add a new language to the list.
  13. Convert the list back into a tuple.

33. Challenge Exercise

Create a tuple containing student information:

student = ("Raj", 25, "Python", 85)

Perform the following:

  • Print the student's name.
  • Print the student's age.
  • Print the course name.
  • Print the student's marks.
  • Use tuple unpacking to assign all four values to separate variables.
  • Create a new tuple containing another student's information.
  • Combine the two tuples.
  • Convert the combined tuple into a list.
  • Add another student.
  • Convert it back into a tuple.

 

32. List vs Tuple — Quick Revision

FeatureListTuple
Syntax[]()
OrderedYesYes
MutableYesNo
DuplicatesAllowedAllowed
IndexingYesYes
Negative indexingYesYes
SlicingYesYes
append()YesNo
insert()YesNo
remove()YesNo
pop()YesNo
sort()YesNo
reverse()YesNo
count()YesYes
index()YesYes

 

  •  
Interactive Sandbox
Python