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.
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.
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)
This video explains how to create tuples in Python.
# 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:
A tuple with one item is constructed by following a value with a comma (it is not sufficient to enclose a single value in parentheses).
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
tuple1 = (1, 2, 3)
tuple2 = (4, 5)
combined = tuple1 + tuple2 # (1, 2, 3, 4, 5)
repeated = ('hi',) * 3 # ('hi', 'hi', 'hi')
colors = ('red', 'green', 'blue')
print('green' in colors) # True
print('yellow' not in colors) # True
for item in ('a', 'b', 'c'):
print(item)
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]
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)
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])
Write a Python program that:
Example Output:
Tuple: (10, 'Apple', 3.5)
First item: 10
Write a Python program that:
Example Output:
Name: Alice
Age: 25
Country: USA
Write a Python program that:
Example Output:
Numbers: (5, 10, 15, 20, 25)
Last number: 25
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 β¨
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 β¨
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 β¨