Hero Vired Logo
Programs
BlogsReviews

More

Vired Library

Complimentary 8-week Gen AI Course with Select Programs.

Request a callback

or Chat with us on

Home
Blogs
Tuple in Python: Function with Example

The Python programming language consists of several unique data types, such as lists, dictionaries, sets, etc. Today, several programmers who check and learn these sets or lists in Python also explore the internet to learn about Python Tuple.

Tuple in Python is just another well-liked and widely accessed collection data type. This article covers an in-depth overview of Python Tuple, its features, benefits, and examples. So, let’s start.

What Is a Python Tuple?

A Python Tuple refers to a group of objects that are encased in parentheses and divided by commas. Programmers use the unchangeable, immutable objects – Tuples in Python to demonstrate fixed groupings of elements.
Moreover, a Python Tuple can be made out of other elements, such as tuples and lists, like demonstrated in the following example:

a tuple = (2, [3, 4, 5], (6, 7 8), and 9.0)

A float number, a tuple, a list, or an integer are all contained in the Python tuple produced by the code above. The following code returns the full Python tuple in addition to its data type.

print (a tuple)

print(type(a tuple)

(2, [3, 4, 5], (6, 7, 8), 9.0)

Features of Python Tuple

Some common features of a Python tuple are ordered, immutable, and allowing duplicate values. Several things are indexed: while the second item has an index [1], the first item has an index [0].

  • Ordered: When a tuple in Python is ordered, it means that the elements are in a specific sequence that won’t change.
  • Unchangeable: Since tuples in Python are immutable, we cannot alter, include, or eliminate things once they have been added to the tuple.
  • Permit Duplicates: Due to their indexing, Python tuples are capable of containing items with identical values:

Accessing Items in A Python Tuple

You can access a tuple in Python just like the way you use indexes to find things in a list. If the index value is specified, the object stored at the given index value shall be returned.

Indexing

This data structure technique can efficiently retrieve information from a data structure. Several data types in Python, including lists and strings, support indexing.

You can also learn about other types of data structures in detail to upgrade your skills as a Python programmer.

Let’s take a simple tuple in Python with five natural numbers. As a result, indexing will begin at value 0 where the value 1 will be stored and continue until the end of the tuple, which is at value 5, where the index value will be 4.

Creating a Tuple

All the components (elements) must be enclosed in parenthesis (), each one divided by a comma, to form a tuple. It is a good habit to utilize the parenthesis, even though they are not required.

Any number of items, which could be of various types, could be included in a tuple in Python (string, list, float, integer, etc.

Slicing

This method extracts a set of elements from a data type that allows element indexing, like Tuple in Python, using the slicing operator “:” Usually, the index value right before the slicing operator represents the Python Tuple starting index.

The starting index in the Python Tuple example above represents the index value before the slicing operator. The index value following the slicing operator represents the value that may not be incorporated in the output.

The output is limited to the value before the ending index. Even the negative index values can be used in conjunction with the slicing operator to extract the range of values from the Python tuple.

Python Tuple Methods

Tuples in Python
When busy with a tuple in Python, you can use the following Python tuple methods:

Index: It returns the specified item’s index

Count: Returns the items’ count

a = (1,2,1,4,1,6,1,8,1,6)
print(a.count(1))

Output: 5

Advantages of Tuple over List in Python

Tuples and lists are employed in similar contexts because of their similarities. Yet, there are several benefits to using a tuple rather than a list:

  • For unique (heterogeneous) data types, programmers typically use tuples, whereas, for homogeneous (similar) data types, they typically use lists.
  • Tuple iteration is quicker than list iteration because tuples are immutable. There is a modest performance improvement.
  • Tuples with immutable components can function as the key for a dictionary. This is not feasible with lists.
  • Implementing data as a tuple will ensure that it stays write-protected if it never changes.

Difference Between Tuples and Lists in Python

Python Tuple Python List
Typically accessed for heterogeneous data types Typically accessed for homogenous data types
Immutable for faster iteration Mutable
Immutable elements serve the purpose of a key to a dictionary No immutable elements available at all
Executing a Python tuple with unchangeable data assures that it’s write-protected No data write-protection assurance available

Conclusion

With this comprehensive guide, you now know how to create a tuple in Python. The guide also covers topics related to Python tuples, including indexing, slicing, and more.

Lastly, the article covered the benefits, features, and examples of Python tuples too. If you want to learn more about Python, start off by grasping all information on Python Libraries.

If you want to upgrade your career and excel in the field of Python and coding, you can enroll in the AI & ML course from Hero Vired. Take a step towards enhanced learning and a better future today!

FAQ's

A single variable can hold several things thanks to the use of tuples. One of Python’s four built-in data types for storing data collections is the tuple; the other three are dictionary, set, and list, each with a unique set of features and applications. A tuple is an unchanging, ordered collection.
Here is a list of the Python tuple example to know about: 
  • (2.0, 8.8, 11) – a Python tuple comprising three numeric objects
  • (201, 10, True) – a tuple in Python comprising an integer, a string, and a Boolean object
  • (‘Jack’, ‘Smith’, ‘Rose’, ‘Maria’) – a tuple in Python comprising four string objects.
The main distinction between a list and a tuple in Python is that tuples cannot be changed, whereas lists may. A list can therefore be changed, but a tuple cannot. Python Tuple is immutable, meaning that its contents cannot be altered once they’ve been constructed.
All the components (elements) must be enclosed in parenthesis () and divided by commas to form a tuple. Although using parenthesis is not required, it is a great exercise to do so. Any number of objects may be of various types and may be included in a tuple (string, list, float, integer, etc.).
An array having a predetermined length and predefined types at each index point is referred to as a tuple type. There is no requirement that the types in this array match; they can be mixed. In a typical array, the number of items might range from zero to an undetermined number, and the order is unimportant.

High-growth programs

Choose the relevant program for yourself and kickstart your career

You may also like

Carefully gathered content to add value to and expand your knowledge horizons

Hero Vired logo
Hero Vired is a premium LearnTech company offering industry-relevant programs in partnership with world-class institutions to create the change-makers of tomorrow. Part of the rich legacy of the Hero Group, we aim to transform the skilling landscape in India by creating programs delivered by leading industry practitioners that help professionals and students enhance their skills and employability.
Privacy Policy And Terms Of Use
©2024 Hero Vired. All Rights Reserved.
DISCLAIMER
  • *
    These figures are indicative in nature and subject to inter alia a learner's strict adherence to the terms and conditions of the program. The figures mentioned here shall not constitute any warranty or representation in any manner whatsoever.