×

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

Related Topics

Python Array

Python Array In the programming language or computer science, an array is defined as the form of a data structure which consists of or store collection of various types of elements...

10 minutes read.

How to Declare a Variable in Python?

The concept of constants and variables is something that we are studying right from our primary classes. We know that constants are the fixed values whereas variables are those whose...

4 minutes read.

Python slice()

Python slice() class The slice() class return a slice object representing the set of indices specified by range(start, stop, step).  Syntax class slice(stop)          or class slice(start, stop[, step]) Parameter Start: This parameter represents an integer number specifying at...

1 minute read.

Multiple Linear Regression using Python

Linear Regression: Linear regression is a method that models the relationship between a dependent variable and one or more independent variables; in other terms, that models the relationship between a target...

6 minutes read.

Python Printf Style Formating

String objects have one special implicit activity: the % administrator (modulo). This is otherwise called the string designing or interjection administrator. Given design % values (where configuration is a string),...

3 minutes read.

Python sklearn train_test_split

Sklearn: One of the most beneficial open-source libraries for learning algorithms in Python is, without a doubt, Sklearn or Scikit-Learn. The most effective tools for statistical modeling and machine learning are all included in...

6 minutes read.

Kite Python

Kite in Python: The Kite is a package provided by the python programming language; it works with the help of artificial intelligence and helps us write code inside the visual studio....

3 minutes read.

Covariance in Python

Covariance is defined as the estimate of the difference of change between two variables or more variables. It defines the changes of two variables together. In Python, The covariance can...

2 minutes read.

Python Time Module

Python contains many files that can be imported into a python code and used whenever we want. One of that modules is the time module. It is a good practice...

6 minutes read.

Checking whether a String Contains a Set of Characters in python

In this tutorial, we will learn how to examine or check whether a string contains any set of characters or a substring and if it contains, we will learn how...

6 minutes read.

Python Trigonometric Functions

Before jumping right into the functions and how to use them we need to know what do trigonometric functions mean and how many functions does python has. What are Trigonometric functions? Trigonometric...

3 minutes read.

Python Set union() method

Python Set union() method The set.union() method in Python returns a set that contains all items from the original set and all items from the specified sets. Syntax set.union(set1, set2...) Parameter set1- This parameter represents the first set...

2 minutes read.

Python Struct

Python Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Python IDE names

Python is one of the most renowned programming languages. It has different execution conditions and a great many compilers to execute the python programs, e.g., PyCharm, PyDev, Jupyter Notebook, Visual...

6 minutes read.

Python Random Module

The tutorial for the Python random module demonstrates how to produce pseudo-random integers in Python. Random Number Generator (RNG) The RNG (random number generator) generates a series of values with no discernible...

8 minutes read.

Python set()

Python set() Class The set() class in Python returns a new set object, optionally with elements taken from iterable.  Syntax class set([iterable]) Parameter iterable : This parameter represents a sequence, collection or an iterator object Return This function returns a...

1 minute read.

How to Add Elements in List in Python?

How to Add Elements in List in Python We all are aware of the wide spectrum of applications where the data structures of Python are used. Be it lists, tuples, or...

4 minutes read.

Anytree Python

Python's anytree module is frequently used within data science-related software. Anytree has a Tolerant License, a Construct File that is public, no errors, no risks, and excellent support. One may...

3 minutes read.

How to Compare two Lists in Python?

How to Compare two Lists in Python The list is a data structure in Python that can hold values of different data types. The values are enclosed in square brackets [...

4 minutes read.

Python List append() Method

Python List append() Method The list.append() method in Python adds or appends an item to the end of the list. Syntax list.append(x) Parameter x: It is a required parameter that represents an element of any type (string, number,...

1 minute read.