Length of Tuple in Python
What is Tuple?
Python is a data structure in a python programming language; it is the collection of the objects in a sequence. The tuples are immutable; that is, we cannot change the tuples once they are created. If we change any data in the tuple, a new tuple will be created without changing the original tuple.
The objects in the tuple are enclosed inside the curly brackets ( ), and the commas separate these objects. The tuples are similar to the lists, but the elements in the lists are mutable, but the elements in the tuple are immutable.We can also store the lists and the tuples inside the tuple. Generally, the tuples are created when we don’t want to change the data inside the tuple once they are created.
Example:
my_tuple = (“Nihaas”, “Rohit”, “Yaswanth”, “Deeraj”, “Nikitha”)
py_tuple = (“print”, “values”, “hello”, “teams”, “function”)
new_tuple = (“shirt”, “program”, “style”, “box”, “pant”)
Tuple Creation
The elements in the tuple are immutable and well-ordered, and the commas separate the elements enclosed in curly brackets. We can directly input the tuple into the program and include dissimilar data types into the python tuple. That is, we can consist of the floating values, integers, tuples, lists and dictionaries inside the tuple, but in arrays, we need to include only similar data types.
Example:
#Creating a python in the python programming language
my_tuple = (4, “string”, 123.22, (“lists”, “variables”, “data”, 67), [“fun”, “hello”, 87])
print(my_tuple )
int_tuple = (34,55,67,8,89,78)
print(int_tuple)
Output:
(4, “string”, 123.22, (“lists”, “variables”, “data”, 67), [“fun”, “hello”, 87])
(34,55,67,8,89,78)
Here we can observe that the first tuple created consists of the integer data type elements and the second tuple created consists of the different data types; we can also store the tuple inside a list inside the tuple.
Accessing the Elements Inside the Tuple
We can access the elements from the tuple with the help of the indexing method; the index of the first element in the tuple is zero, and the index of the final element is the size of the element minus one. If we know the element's size, we can print the final element in the tuple.
Syntax:
tuple_name [index values]
Parameters:
- tuple_name: Name of the tuple which we created
- index values: These are the integer values for accessing the elements inside the tuple.
Example:
#Creating the tuple
#Creating the tuple
my_tuple = (“Nihaas”, “Rohit”, “Yaswanth”, “Dheeraj”, “Nikitha”)
#displaying the elements inside the tuple using the indexing method
print(my_tuple[0] )
print(my_tuple[3] )
Output:
Nicholas
Dheeraj
We can also access the elements inside the tuple using the negative indexing method; that is, by keeping the index value as -1, we can print the last element in the tuple; in this way, we can also access the elements inside the tuple by using the negative indexing method.
Example:
#Creating the tuple
my_tuple = (“Nihaas”, “Rohit”, “Yaswanth”, “Dheeraj”, “Nikitha”)
#displaying the elements inside the tuple using the indexing method
print(my_tuple[-1] )
print(my_tuple[-2] )
Output:
Nikitha
Dheeraj
Slicing Operation in the Tuple
We can also perform the slicing operation in python with the help of the index. When we perform the slicing operation, it will create the new tuple, but it does not change the already existing tuple; it will create the new tuple as the tuple is immutable.
Example:
#Creating the tuple
my_tuple = (“Nihaas”, “Rohit”, “Yaswanth”, “Dheeraj”, “Nikitha”)
#Slicing the tuple and displaying the elements
print(my_tuple[2:4])
#slicing the tuple and displaying the elements
print(my_tuple[1: ])
Output:
“Yaswanth”, “Dheeraj”
“Rohit”, “Yaswanth”, “Dheeraj”, “Nikitha”
Finding the Length of the Tuple
We can find the length of the tuple that will display the total number of elements present in the tuple. We can see the length of the tuple in python with the help of the built-in function, the len( ) function.
Syntax:
len(my_tuple )
Parameters:
- my_tuple: Name of the tuple where we need to find the length.
- Return type: This function will print the total number of elements inside the tuple.
Example:
# Creating the tuple
my_tuple = (12, 56, ‘who’)
my_tuple_2 = (87,990, ‘tuple’, 135.99)
#Displaying the length of the tuple
print( “Length of the tuple:”, len(my_tuple))
print(“Length of the tuple:”, len(my_tuple_2))
Output:
Length of the tuple: 3
Length of the tuple: 4