In Python, a tuple is a collection of ordered items, similar to a list. Tuples are enclosed by parentheses '()' with comma-separated items. Tuples can contain elements of different data types, including integers, floats, strings, booleans, and even other tuples or complex objects.
Key properties of Python tuples are:
1. Ordered: Like lists, tuples in Python are ordered collections, meaning that the elements have a specific order that is maintained unless explicitly changed by the programmer. The order in which elements are added to the tuple is the order in which they are stored and retrieved, and this order cannot be altered as tuples are immutable.
2. Allows Duplicate Members: Tuples in Python can contain duplicate elements. That is, the same value can appear more than once in a tuple.
3. Immutable: Unlike lists, tuples are immutable, which means their elements cannot be modified after creation.
Creating Tuples:
To create a tuple in Python, you simply enclose the elements you want to include within parentheses '()', separated by commas.
# Examples of creating tuples
numbers = (1, 2, 3, 4, 5) # A tuple of integers
names = ("Alice", "Bob", "Charlie") # A tuple of strings
mixed = (1, "Alice", True) # A tuple with mixed data types
nested = (1, (2, 3), 4) # A tuple with another tuple as an element
empty_tuple = () # An empty tuple
In Python, creating a tuple with only one item, also known as a singleton tuple, has a unique syntax. The trailing comma `,` is what defines a one-item tuple, not the parentheses `()`. Without the trailing comma, Python treats the `()` as parentheses for a function or grouping, not a tuple.
Here is an example of creating a tuple with one item:
single_item_tuple = ("apple", )
print(single_item_tuple) # Outputs: ('apple', )
If you leave out the trailing comma, you don't get a tuple. So, always remember to include a trailing comma when you want to create a tuple with a single item.
Using tuple() Constructor:
You can create a tuple in Python using the 'tuple()' constructor. This can be useful when you want to create a tuple from an existing iterable object such as a string, list, set, or another tuple. Here's how you do it:
# Using tuple() constructor with a string
str_tuple = tuple("Hello")
print(str_tuple) # Outputs: ('H', 'e', 'l', 'l', 'o')
# Using tuple() constructor with a list
list_tuple = tuple([1, 2, 3, 4])
print(list_tuple) # Outputs: (1, 2, 3, 4)
Also, you can create an empty tuple by using the 'tuple()' constructor without any arguments:
empty_tuple = tuple()
print(empty_tuple) # Outputs: ()
Tuple Length:
In Python, the `len()` function is used to get the number of items (length) of a tuple.
Here is an example:
numbers = (1, 2, 3, 4, 5)
length = len(numbers)
print(length) # Outputs: 5
Access Items:
Tuple Indexing:
fruits = ("apple", "banana", "cherry", "date", "elderberry")
print(fruits[0]) # Outputs : "apple"
print(fruits[2]) # Outputs : "cherry"
fruits = ("apple", "banana", "cherry", "date", "elderberry")
print(fruits[-1]) # Outputs : "elderberry"
print(fruits[-3]) # Outputs : "cherry"
Range of Indexes:
fruits = ("apple", "banana", "cherry", "date", "elderberry", "fig", "grape")
# Get elements from index 2 to 4
print(fruits[2:5]) # Outputs: ("cherry", "date", "elderberry")
# Get all elements from the beginning to index 3 (exclusive)
print(fruits[:3]) # Outputs: ("apple", "banana", "cherry")
# Get all elements from index 4 to the end of the tuple
print(fruits[4:]) # Outputs: ("elderberry", "fig", "grape")
# Get the last 3 elements of the tuple
print(fruits[-3:]) # Outputs: ("elderberry", "fig", "grape")
# Get every other element in the tuple
print(fruits[::2]) # Outputs: ("apple", "cherry", "elderberry", "grape")
Check Items:
fruits = ("apple", "banana", "cherry", "date", "elderberry")
if "banana" in fruits :
print("Yes, 'banana' is in the fruits tuple") # This will be printed
if "mango" in fruits :
print("Yes, 'mango' is in the fruits tuple") # This will not be printed
Unpacking a Tuple:
fruits = ("apple", "banana", "cherry")
# Unpacking the tuple
(fruit1, fruit2, fruit3) = fruits
print(fruit1) # Outputs: "apple"
print(fruit2) # Outputs : "banana"
print(fruit3) # Outputs : "cherry"
fruits = ("apple", "banana", "cherry", "date", "elderberry")
# Unpacking the tuple
(first_fruit, *middle_fruits, last_fruit) = fruits
print(first_fruit) # Outputs: "apple"
print(middle_fruits) # Outputs : ["banana", "cherry", "date"]
print(last_fruit) # Outputs : "elderberry"
Join Tuples:
tuple1 = ("A", "B", "C")
tuple2 = (1, 2, 3)
# Joining the tuples
tuple3 = tuple1 + tuple2
print(tuple3) # Outputs: ('A', 'B', 'C', 1, 2, 3)
Multiply Tuples:
tuple1 = ("A", "B", "C")
# Multiplying the tuple
tuple2 = tuple1 * 3
print(tuple2) # Outputs: ('A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'C')
Delete Tuple:
fruits = ("apple", "banana", "cherry")
# Deleting the entire tuple
del fruits
# The following line will raise a NameError because 'fruits' no longer exists
print(fruits)