Data Science with Python Certification Traini ...
- 94k Enrolled Learners
- Live Class
Python programming language has various data types including lists, sets, dictionaries, etc. Python also comes with a collections package that has specialized data structures. Tuple in python is also one of the collection data types that is popular. In this article, we will learn about tuples in detail with examples. Following are the topics covered in this blog:
A tuple is an immutable data type in python, almost similar to a list in python in terms of indexing and having duplicate members. It is a collection data type that stores python objects separated by commas. Following is an example of how we can create or declare a tuple in python.
#creating a tuple a = ('python', 'edureka') #another approach b = 'python' , 'edureka' print(a) print(b)
Output: ('python' , 'edureka') ('python' , 'edureka')
Accessing items in a tuple works similar to a list, we can access elements in a list using indexes. We can specify the index value and it will return the item stored at that particular index value.
For example, let’s just say we have a tuple with 5 natural numbers as members. So the indexing will start with the value 0 where 1 will be stored and it will go until the end of the tuple i.e 5 and the index value at 5 will be 4.
Take a look at the example below to understand how we can access elements in a tuple using indexing
a = ('edureka', 'python' , 'data structure' , 'collections') print(a) print(a)
Output: python collections
As you can see in the above example, we are able to get the elements stored at the index values 1 and 3. Similarly, we can access any value inside a tuple using index values.
In python, we can use negative indexing as well to access elements in a tuple or any other data type that supports indexing.
a = (1,2,3,4,5,6,7,8,9,10) print(a[-4]) print(a[-1])
Output: 7 10
It is a technique in which we use the slicing operator ‘:’ to get a range of elements from a tuple or any other data type that supports indexing for accessing elements.
a = (1,2,3,4,5,6,7,8,9,10) print(a[1:8]) print(a[1:]) print(a[:5])
Output: (2,3,4,5,6,7,8) (2,3,4,5,6,7,8,9,10) (1,2,3,4,5)
In the above example, the index value before the slicing operator is the starting index and the index value after the slicing operator is the value that will not be included in the output.
Only until the value before the ending index will be included in the output. We can even use the negative index values with the slicing operator to get the range of values from the tuple.
a = (1,2,3,4,5,6,7,8,9,10) print(a[-8:])
Even though tuples in python are immutable in nature, a nested object in a tuple can be changed. Or in general, a tuple in python can be reassigned with a different value.
a = (1,2,3,[4,5]) a = 14 print(a) #reassigning the value a = ('edureka', 'python') print(a)
Output: (1,2,3,[14,5]) ('edureka', 'python')
Joining two tuples is a very easy task. You just to assign the addition of the two tuples to another variable and it will return the concatenated tuple with the values of both the tuples. Consider the example below to understand this.
a = (1,2,3,4,5) b = (6,7,8,9,10) c = a + b print(c)
As you can see in the example, the concatenated tuple contains the values of both the tuples a and b.
Being an immutable data type, a tuple in python does not allow any changes and you cannot even remove an element from a tuple after the declaration. But there is a keyword ‘del’ which will delete the tuple altogether.
a = (1,2,3,4,5) del a print(a)
You will get a Name error if you run the above program because there is no tuple named as present since we have deleted it.
Following are the tuple methods that we can use while working with a tuple in python.
a = (1,2,1,3,1,3,1,2,1,4,1,5,1,5) print(a.count(1)) print(a.index(5))
Output: 7 11
Used for homogenous data types
Generally used for heterogeneous data types
Mutable in nature
Immutable in nature, which helps in faster iteration
Does not have immutable elements
Immutable elements can be used as a key for a dictionary
No guarantee that the data is write-protected
Implementing a tuple with data that does not change guarantees that it is write-protected
Using a for loop we can iterate through a tuple in python. The following example shows how we can iterate through a tuple using a for loop.
a = ("edureka", "for data science", "for Artificial Intelligence") for i in a: print("python", i)
Output: python edureka python for data science python for artificial intelligence
It is possible to create a tuple using a tuple() constructor as well. We can even use the tuple constructor to change a list to a tuple.
a = [1,2,3,4,5] b = tuple(a) print(b) c = tuple(('edureka', 'python')) print(c)
Output: (1,2,3,4,5) ('edureka', 'python')
Using the membership operator ‘in’ in python we can check whether an element is present in a tuple or not. The following example shows how we can check if an element is present in a tuple or not.
a = (1,2,3,4,5,6,7,8,9,10) print(6 in a) print(15 in a )
Output: True False
This brings us to the end of this article where we have learned how we can use tuple in python and how we can access the elements in a tuple using indexes with various other examples. I hope you are clear with all that has been shared with you in this tutorial.
If you found this article on “Tuple In Python” relevant, check out the Edureka Python Certification Training, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.
We are here to help you with every step on your journey and come up with a curriculum that is designed for students and professionals who want to be a Python developer. The course is designed to give you a head start into Python programming and train you for both core and advanced Python concepts along with various Python frameworks like Django.
If you come across any questions, feel free to ask all your questions in the comments section of “Tuple In Python” and our team will be glad to answer.