Tuple Operations
Posted: Mon May 27, 2024 9:33 am
Tuples:
A tuple is a built-in data structure in Python similar to lists but also has some significant differences. The difference between the two is that once the elements are assigned to the tuple can’t be changed. Tuples are used to store an ordered series of elements and are defined by parentheses (). The most common associated method of tuples are tuple(), count(), index() etc. Tuples allow duplicate values:
Examples of Tuple: thistuple = ("apple", "banana", "cherry", "apple", "cherry")
A tuple is a built-in data structure in Python similar to lists but also has some significant differences. The difference between the two is that once the elements are assigned to the tuple can’t be changed. Tuples are used to store an ordered series of elements and are defined by parentheses (). The most common associated method of tuples are tuple(), count(), index() etc. Tuples allow duplicate values:
Examples of Tuple: thistuple = ("apple", "banana", "cherry", "apple", "cherry")
Code: Select all
# Creating a tuple of integers
my_tuple = (1, 2, 3, 4, 5)
# Creating a tuple of mixed types
mixed_tuple = (1, 'hello', 3.14, True)
# Creating an empty tuple
empty_tuple = ()
# Creating a singleton tuple (a tuple with a single element)
singleton_tuple = (42,)
print(my_tuple)