Python Tutorial
Python Flow Control
Python Functions
Python Data Types
Python Date and Time
Python Files
Python String
Python List
Python Dictionary
Python Variable
Python Input/Output
Python Exceptions
Python Advanced
Tuples are an immutable sequence data type in Python, similar to lists but enclosed in parentheses ()
. Once a tuple is created, you cannot modify its elements or change its size. In this tutorial, we'll cover creating tuples, accessing elements, basic tuple operations, and common tuple methods.
To create a tuple, enclose the elements in parentheses ()
and separate them with commas ,
.
empty_tuple = () single_element_tuple = (1,) # Note the trailing comma for single-element tuples example_tuple = (1, 2, 3, 4, 5) mixed_type_tuple = (1, "Hello", 3.14, [1, 2, 3])
You can access individual elements in a tuple using indexing and slicing.
[]
with the index of the element you want to access.numbers = (1, 2, 3, 4, 5) first_number = numbers[0] print(first_number) # Output: 1
[]
with a start and end index separated by a colon :
to access a sub-tuple.numbers = (1, 2, 3, 4, 5) subtuple = numbers[0:3] print(subtuple) # Output: (1, 2, 3)
+
operator to join two tuples.tuple1 = (1, 2, 3) tuple2 = (4, 5, 6) result = tuple1 + tuple2 print(result) # Output: (1, 2, 3, 4, 5, 6)
*
operator to repeat a tuple a specified number of times.example_tuple = (1, 2, 3) result = example_tuple * 3 print(result) # Output: (1, 2, 3, 1, 2, 3, 1, 2, 3)
len()
function to find the length of a tuple.numbers = (1, 2, 3, 4, 5) length = len(numbers) print(length) # Output: 5
count(value)
: Returns the number of occurrences of the specified value in the tuple.example_tuple = (1, 2, 3, 2, 1, 2, 1) count = example_tuple.count(1) print(count) # Output: 3
index(value)
: Returns the index of the first occurrence of the specified value in the tuple. Raises a ValueError
if the value is not found.example_tuple = (1, 2, 3, 4, 5) index = example_tuple.index(3) print(index) # Output: 2
This tutorial provides an introduction to tuples in Python. While there are many more methods and features available, these basics will help you get started working with tuples in your Python programs. Remember that tuples are immutable, so if you need a mutable sequence data type, use a list instead.
Creating and initializing tuples in Python:
my_tuple = (1, 2, 'three', 4.0)
Accessing elements in a Python tuple:
my_tuple = (1, 2, 'three', 4.0) third_element = my_tuple[2]
Tuple packing and unpacking in Python:
packed_tuple = 1, 'two', 3.0 a, b, c = packed_tuple
Modifying and updating tuples in Python:
original_tuple = (1, 2, 3) modified_tuple = original_tuple + (4,)
Nested tuples in Python:
nested_tuple = (1, (2, 3), ('four', 5))
Tuple vs list in Python:
my_tuple = (1, 2, 3) my_list = [1, 2, 3]
Converting lists to tuples in Python:
tuple()
constructor to convert a list to a tuple.my_list = [1, 2, 3] my_tuple = tuple(my_list)
Tuple slicing and indexing in Python:
my_tuple = (1, 2, 'three', 4.0) sliced_tuple = my_tuple[1:3]
Common operations with Python tuples:
my_tuple = (1, 2, 'three', 4.0) is_in_tuple = 'three' in my_tuple tuple_length = len(my_tuple)