In Python, a tuple is a collection of ordered, immutable elements. Tuples are similar to lists in Python, but the main difference is that tuples cannot be modified once they are created. Tuples are often used to store related data together and can be indexed and iterated like lists.
Tuples are often used to return multiple values from a function, since they allow for easy grouping of related data. However, since tuples are immutable, elements cannot be modified once the tuple is created.
A tuple is created using parentheses (), and elements are separated by commas. Here's an example of a tuple:
Tuples are one of the 4 built-in data types in Python used to store connections of data. These are used to store multiple items of the same or different data types in a single variable. Tuples are ordered, immutable, and allow duplicate values.
tuple1 = ("Delhi", "Mumbai", "Chennai", 9)
Tuples are ordered, hence tuple items can be indexed:
print (tuple1[1])
# Output = Mumbai
We can use len() function to find the length of the tuple:
print(len(tuple1))
# Output = 4
Since tuples are immutable, we cannot add, insert, update, or delete items from a tuple.
To create a type with only one item, we need to add a comma after the item; otherwise, it won't be recognized as a tuple:
tuple2 = ("Hello",) # This is a tuple
tuple3 = ("Hello") # This is a string
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
In Python, a tuple is a collection of ordered, immutable elements. Tuples are similar to lists in Python, but the main difference is that tuples cannot be modified once they are created. Tuples are often used to store related data together and can be indexed and iterated like lists.
Tuples are often used to return multiple values from a function, since they allow for easy grouping of related data. However, since tuples are immutable, elements cannot be modified once the tuple is created.
A tuple is created using parentheses (), and elements are separated by commas. Here's an example of a tuple:
Tuples are one of the 4 built-in data types in Python used to store connections of data. These are used to store multiple items of the same or different data types in a single variable.
Tuples are ordered, immutable, and allow duplicate values.
Tuples are ordered, hence tuple items can be indexed:
We can use len() function to find the length of the tuple:
Since tuples are immutable, we cannot add, insert, update, or delete items from a tuple.
To create a type with only one item, we need to add a comma after the item; otherwise, it won't be recognized as a tuple: