TUPLE IN PYTHON

machine learning from scratch

Similar to Lists, Tuple is an ordered sequence of items but are immutable i.e items of tuple can not be changed. The tuple which is pronounced either like too-pul or tup-el is used as a way of defining a combination of one or more similar values in mathematics. Tuples are used for those data which are write-protected and can not be changed later. Tuples are written within parentheses () and separated by commas. Python recognizes it as tuple even if the parentheses is missing.

 >>> tuple_1 = (2,"sawid",4)
>>> print (type(tuple_1))
>>> a = 2, 'sunil','bae'     #missing parentheses
>>> print(type(a))

OUTPUT:
<class 'tuple'>
<class 'tuple'>
  • immutable – can’t be changed once created (separates from list)
  • ordered – The elements in a tuple are in a specific order, and remain in that order
  • Sequence – The objects in the tuple are accessible in sequence-i.e. first, second, third etc.

Although tuples support most of the same operations as lists (such as indexing, slicing,and concatenation), the contents of a tuple cannot be modified after creation (that is,you cannot replace, delete, or append new elements to an existing tuple). We can access the elements of tuple using indexing by providing the index value inside square bracket. Indexing starts from 0 like as in other data types. We can use the slicing operator [] to extract items but we cannot change its value.

Since we can’t change values in tuples, so iteration in tuples is faster than list & the speed of execution is faster in tuple.

Count ( )

count ( ) is a method which is used to count the total number of occurrence of data item in tuple.

>>> a = ( 3, 5, 7, 3, 5, 3, 3)
>>> b = a.count(3)
>>> print (b)

Output: 4
Index ( )

The index () method in the tuple searches an element and return its index. Simply, index() method checks the given element in a tuple and returns its location. However, the first / smallest position is returned if the same element is present more than once.

>>> a = ( 5, 7, 3, 5, 3, 3)
>>> print (a.index(3))

output:  2
Slicing

Slicing operator [] can be used with tuple. Tuple stores sequences indexed by integers, starting at zero. To extract a single item, use the indexing operator s[i] like this: Similar to List, square brackets can be used to access elements of the tuple, we can access the range of items using slicing. To obtain certain part of items we specify starting and ending index separated by colon ‘:’.

>>> a = ('sunil', 'python', 'sawid',3)
>>> print (a[2])
>>> print(a [1:3])

Output :
       sawid
       ('python', 'sawid')

BUT !! DO YOU KNOW ??

Tuples are immutable: Once a tuple is created, its value can not be changed. But there is a way, we can convert tuple into a list, change the list elements, and convert the list back into a tuple. Lets give it a try by changing “python” into sush.

This page is contributed by Diwas & Sunil . If you like AIHUB and would like to contribute, you can also write an article & mail your article to  itsaihub@gmail.com . See your articles appearing on AI HUB platform and help other AI Enthusiast.

About Diwas

🚀 I'm Diwas Pandey, a Computer Engineer with an unyielding passion for Artificial Intelligence, currently pursuing a Master's in Computer Science at Washington State University, USA. As a dedicated blogger at AIHUBPROJECTS.COM, I share insights into the cutting-edge developments in AI, and as a Freelancer, I leverage my technical expertise to craft innovative solutions. Join me in bridging the gap between technology and healthcare as we shape a brighter future together! 🌍🤖🔬

View all posts by Diwas →

One Comment on “TUPLE IN PYTHON”

Leave a Reply

Your email address will not be published. Required fields are marked *