Learn with Yasir

Share Your Feedback

Python Tuples Tutorial for Beginners | Learn Tuple Basics with Examples


Explore the basics of Python tuples in this beginner-friendly tutorial. Learn tuple creation, properties, and usage with clear examples to enhance your Python skills.

Tuple in Python

  • In python, a tuple is an immutable sequence of elements. it is similar to a list, but the elements of a tuple cannot be modified once they are created.
  • Tuple is a collection data type in python. It is useful for storing multiple related values as a single unit.
  • Sequence types in python - list, tuple and range

Basic Operations

1. Creating Tuples

A tuple is created by enclosing elements within parentheses () and separating them with commas. While parentheses are technically optional, it’s generally considered best practice to use them for clarity and consistency.

Example 1

Some common ways to create tuples in Python include:

tup = (1,2,3)
print(tup) # Output: (1, 2, 3)
# check the type of variable
print(type(tup)) # Output: <class 'tuple'>

# another example to create tuple
tup1 = 4,5,6
print(tup1) # Output: (4, 5, 6)

# tuple with mixed datatypes
tup_mixed = (7, "String", 7.8)
print(tup_mixed)

πŸ“Ί Python Tutorial: Learn 5 Easy Ways to Create Tuples in Python

This video explains how to create tuples in Python.

2. Conversion

# List to tuple
list_data = [1, 2, 3]
tuple_data = tuple(list_data)  # (1, 2, 3)

# String to tuple
str_data = "hello"
tuple_chars = tuple(str_data)  # ('h', 'e', 'l', 'l', 'o')

A nested tuple is a tuple that contains one or more tuples as element.

Empty and single item tuple:

2. Accessing Elements

my_tuple = ('a', 'b', 'c', 'd', 'e')

# Indexing
print(my_tuple[0])    # 'a' (first element)
print(my_tuple[-1])   # 'e' (last element)

# Slicing
print(my_tuple[1:3])  # ('b', 'c')
print(my_tuple[:2])   # ('a', 'b')
print(my_tuple[2:])   # ('c', 'd', 'e')
print(my_tuple[::-1]) # ('e', 'd', 'c', 'b', 'a') (reverse)

for more details, see Python Tuple Slicing: A Complete Guide with Examples

Python tuple example 2: How to Access Tuple Items in Python

Tuple Operations

3. Concatenation (+)

tuple1 = (1, 2, 3)
tuple2 = (4, 5)
combined = tuple1 + tuple2  # (1, 2, 3, 4, 5)

4. Repetition (*)

repeated = ('hi',) * 3  # ('hi', 'hi', 'hi')

5. Membership Testing

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

6. Iteration

for item in ('a', 'b', 'c'):
    print(item)

7. Unpacking tuples

Tuple unpacking allows you to assign the values of a tuple to multiple variables in a single step. Each element of the tuple is assigned to a corresponding variable.

point = (10, 20)
x, y = point  # x=10, y=20

# Extended unpacking
values = (1, 2, 3, 4, 5)
a, b, *rest = values  # a=1, b=2, rest=[3, 4, 5]

Python tuple example 4: Unpacking a Tuple in Python

  • The number of variables on the left side must match the number of elements in the tuple, or you’ll get a ValueError.
# Correct unpacking - number of variables matches tuple elements
point = (3, 5)
x, y = point
print(f"x: {x}, y: {y}")  # Output: x: 3, y: 5

# Incorrect unpacking - will raise ValueError
colors = ('red', 'green', 'blue')
# color1, color2 = colors  # This would raise ValueError
# Correct way would be:
color1, color2, color3 = colors
print(color1, color2, color3)  # Output: red green blue

Tuples are immutable, meaning once created, their elements cannot be changed, added, or removed. However, they can store mutable objects (like lists or dictionaries), which can be modified.

for more details, see Mutable vs Immutable Data Types: Key Differences & Examples (2025 Guide)

Example:

my_tuple = (1, 2, [3, 4])  # Tuple with an immutable list inside

# my_tuple[0] = 10  ❌ Fails (can't modify tuple)
my_tuple[2].append(5)      # βœ… Works (modifying the inner list)

print(my_tuple)  # Output: (1, 2, [3, 4, 5])

Tasks

Task 1: Creating a Tuple

Write a Python program that:

  • Creates a tuple with three different items (e.g., a number, a word, and a decimal).
  • Prints the entire tuple.
  • Prints the first item in the tuple.

Example Output:

Tuple: (10, 'Apple', 3.5)
First item: 10

Task 2: Tuple Unpacking

Write a Python program that:

  • Creates a tuple with three values: name, age, and country.
  • Uses tuple unpacking to assign each value to separate variables.
  • Prints the extracted values.

Example Output:

Name: Alice
Age: 25
Country: USA

Task 3: Accessing Tuple Elements

Write a Python program that:

  • Creates a tuple with five numbers.
  • Prints the last number using negative indexing.

Example Output:

Numbers: (5, 10, 15, 20, 25)
Last number: 25

Task 4: Calculate the Sum of a Tuple Using a For Loop

Write a Python function called sum_tuple that takes one argument: a tuple of numbers. The function should return the sum of all the numbers in the tuple using a for loop (not the built-in sum() function).

Input:

A tuple containing numerical values (integers or floats). Example:

(1, 2, 3, 4)

Expected Output:

A single numerical value representing the sum of the numbers in the tuple. For the input above, the output would be:

10

Click here to see the solution ✨

Task 5: Printing Tuple Elements with a While Loop

Write a Python function called print_tuple_while that takes a tuple of strings as input. This function should iterate through the tuple using a while loop and print each element of the tuple on a separate line.

Input:

A tuple of strings, for example:

my_tuple = ("apple", "banana", "cherry")

Expected Output:

apple
banana
cherry

Here’s a starting point for the function:

def print_tuple_while(input_tuple):
    index = 0
    # Complete the while loop to print each element
    # of the input_tuple
    pass

# Example usage:
my_tuple = ("apple", "banana", "cherry")
print_tuple_while(my_tuple)

Click here to see the solution ✨

Task 6: How to Swap Variables in One Line of Code using Tuple Unpacking

Write a Python function called swap_variables that takes two variables, a and b, as input. The function should swap the values of these two variables using the one-line tuple unpacking technique and then return the new values of a and b as a tuple.

Input:

Two variables of any data type. For example:

x = 10
y = 5

Expected Output:

A tuple containing the swapped values. For the example input above:

(5, 10)

Here’s a starting point for the function:

def swap_variables(a, b):
    # Write your one-line swap using tuple unpacking here
    return a, b

# Example usage:
x = 10
y = 5
swapped_x, swapped_y = swap_variables(x, y)
print(f"Original x: {x}, y: {y}")
print(f"Swapped x: {swapped_x}, y: {swapped_y}")

Click here to see the solution ✨


🧠 Practice & Progress

Explore More Topics

Python Fundamentals

Flow Control Statements


Python Functions


Fundamentals more ...




🧠 Python Advanced

Object-Oriented Programming in Python (OOP)

More...

🧠 Modules