Table of Contents
Python tuple is a collection of objects and is separated by commas. It will work similarly to the Python list regarding indexing, nested objects, and repetition. Read this article to learn more about Python-Tuples.
What is Python Tuples?
Python tuple will allow you to store multiple items into a single variable. Whereas, the tuple is considered one of the 4 built-in data types in Python. It is a collection that is mainly ordered and unchangeable. So, it will be written with the round brackets.
What are Tuple Items?
Tuple items will be ordered, unchangeable, and can have duplicate value. These items are indexed the first item has an index of [0] and the second item will have an index of [1] etc.
What is an Ordered Tuple?
An ordered Tuple is referred to as an item that should be in a defined order and that order won’t be changed. Tuples are also unchangeable, where they can’t be changed, added, or removed items after the tuple is created.
Example – To show that duple allows duplicates
from collections import Counter
def check_duplicates(lst):
count = Counter(lst)
for item, freq in count.items():
if freq > 1:
print(f"Element '{item}' appears {freq} times.")
# Example list with duplicates
my_list = [1, 2, 3, 1, 4, 2, 5, 6, 3, 2]
# Check for duplicates
check_duplicates(my_list)
Output
Element '1' appears 2 times.
Element '2' appears 3 times.
Element '3' appears 2 times.
Example-To determine the Tuple Length
def tuple_length(tup):
return len(tup)
# Example tuple
my_tuple = (1, 2, 3, 4, 5)
# Find the length of the tuple
length = tuple_length(my_tuple)
# Print the length of the tuple
print("Length of the tuple:", length)
Output
Length of the tuple: 5
Example- To create the Tuple With One Item
It is required to add the comma after the item to create the Tuple or else, it won’t be considered as the Tuple.
# Tuple with a single item
my_tuple = (42,) # Note the trailing comma after the single item
# Output
print("Tuple with a single item:", my_tuple)
Output
Tuple with a single item: (42,)
Example – to create the Data Types for Tuple items
Tuple items will be of any data type:
# Define a tuple with items of different data types
my_tuple = ("apple", 42, 3.14, True, [1, 2, 3])
# Output the data types of tuple items
for item in my_tuple:
print(f"Item: {item}, Data Type: {type(item)}")
Output
Item: apple, Data Type: <class 'str'>
Item: 42, Data Type: <class 'int'>
Item: 3.14, Data Type: <class 'float'>
Item: True, Data Type: <class 'bool'>
Item: [1, 2, 3], Data Type: <class 'list'>
What is type()
Tuple will be referred to as the objects with the data type in Python.
<class 'tuple'>
What is a Tuple() Constructor?
The Tuple constructor will help you to make the tuple.
# Using tuple() method to create a tuple
my_list = [1, 2, 3, 4, 5]
my_tuple = tuple(my_list)
# Output
print("Tuple created from list:", my_tuple)
Output
Tuple created from list: (1, 2, 3, 4, 5)
What are the Python Collection Data Types?
Python consists of four collection data types in Python Programming language:
List: It is referred to as the collection that is ordered and changeable. Hence, it will allow duplicate members.
Tuple: It is one among the data types that are ordered and unchangeable and will offer duplicate members.
Set: It is a collection that is unordered, unchangeable, and unindexed and has no duplicate members.
Dictionary: It refers to the collection that is ordered is changeable and has no duplicate members.
Python – Tuples- FAQs
Q1. What is the purpose of tuples in Python?
Ans. Tuple will store the multiple items in the single variable.
Q2. How are tuples stored in Python?
Ans. Tuples will be stored in a single block of memory.
Q3. What are the two functions of a tuple?
Ans. Tuple Functions includes len(), max(), min(), tuple().
Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together