×

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

Difference between For Loop and While Loop in Python

What is the “For” loop? The “For” Loop is a commonly used control structure in programming that allows us to repeat a set of actions multiple times. The “For” loop is...

3 minutes read.

Difference between Python 2 and Python 3

In this tutorial, we will learn the differences between two versions of python, that is, python version 2 and python version 3. Some basic differences include- Python 2 is the older version...

3 minutes read.

Python open() function

Python open() function The open() function in Python opens a file and returns a corresponding file object. If the file cannot be opened, an OSError is raised. Syntax open(file, mode='r', buffering=1, encoding=None, errors=None, newline=None, closefd=True, opener=None) Parameter file It represents the path and name of the file mode...

2 minutes read.

Working with CSV files in Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

7 minutes read.

Python Pascal Triangle

Python Pascal Triangle Pascal triangle A pascal triangle is a number pattern of triangular array of the binomial coefficients. For designing a pascal triangle, we write a function in the program which...

5 minutes read.

Python MySQL Client

MySQL client is a project that is the subdivision of the MySQLdb package. This package provides an interface that runs the Python database API. Installing mysqlclient You can easily install mysqlclient with...

5 minutes read.

Python Maurer Rose and Rhodonea Curves

In this tutorial, in Python we will make a Maurer Rose example and Rhodonea Curve example. Before we continue to see what precisely is maurer rose or a rhodonea bend...

10 minutes read.

StandardScaler in Sklearn

When and How Should You Use StandardScaler? StandardScaler comes into play whenever the properties of the provided dataset change significantly within their ranges or are recorded in different measurement units. Since using...

4 minutes read.

Word frequency Python

Word frequency Python In this tutorial, we will write the Python program to count the occurrence of a word (word frequency) in a given sentence. We will learn all the approaches...

5 minutes read.

Sort Dictionary in Python

Dictionaries In Python, a dictionary is an unordered collection or set of data types that enable of store data in an unordered key-value/pair. The key is stored alongside with value. Dictionary contains key:...

4 minutes read.

Wikipedia module in Python

Wikipedia module in Python: The internet has become an essential part of all of our life which is the largest source of all the information. Therefore, it becomes very important...

7 minutes read.

Data Distribution in python

Before discussing data distribution, we will discuss each word that is data and Distribution. What is meant by data? A collection of raw facts and figures is called data. The word "raw"...

18 minutes read.

Composition in Python

Composition in Python Composition is one of the important concepts of Object-oriented programming (OOPs). Composition basically enables us for creating complex types objects by combining other types of objects in the...

6 minutes read.

How to check version of Python

How to check version of python The versions of Python come with different kinds of features and functionalities. It is not a herculean task to keep track of the updates these...

3 minutes read.

Python Modules

Python Modules The Python module is a collection of definition and statements. It can contain functions, classes, and variables.  Combining the related code into a module makes the code easier to understand and use....

5 minutes read.

Python String Variable

Python programming language: 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...

4 minutes read.

Python filter() function

Python filter() function The filter() function constructs an iterator from those elements of iterable for which the parameter ‘function’ returns a Boolean value true. Syntax filter(function, iterable) Parameter function: This parameter represents a function to be run for each item in the...

1 minute read.

Python String title() method

Python String title() method The string.title() method in Python returns a string where the first character in every word is upper case. If the word contains a number or a symbol,...

1 minute read.

Python Interface

When creating an application, it is important to continuously keep track of its changes. As an application grows, sometimes it gets hard to manage its updates and changes. Often, you...

4 minutes read.

Python K-Means Clustering

K-Means Clustering is a basic yet incredible calculation in information scienceThere are a plenty of true utilizations of K-Means clustering (a couple of which we will cover here)This far reaching...

25 minutes read.