Tuple in Python
What is Tuple ?
Tuple is similar to the List.
Only one difference between them is Lists are mutable and Tuples are immutable object.
Also, Tuples are hashable whereas lists are not.
How to create Tuple ?
Tuple can be created by placing sequence of values separated by 'comma' ( , ) with or without the use of parentheses. But the standard syntax says we should use parentheses ( ) , to define a tuple, it increases the readability.
# empty tuple my_tup = ( ) # tuple without use of parentheses my_tup = 1, 2, 3 # tuple of integers my_tup = (1, 2, 3) # tuple with mixed datatypes my_tup = (1, "Hello", 1.2)
Creating a tuple with single element :
Creating a tuple with one element is a bit tricky.Having one element in the parentheses is not sufficient, there must be a trailing ‘comma’ to make it a tuple.
my_tuple = ("hello") print(type(my_tuple)) # < class 'str'> # Creating a tuple having one element my_tuple = ("hello",) print(type(my_tuple)) # < class 'tuple'> # Parentheses is optional my_tuple = "hello", print(type(my_tuple)) # < class 'tuple'>
NOTE : Creation of Python tuple without the use of parentheses is known as Tuple Packing.
Access Tuple Elements :
We can use the index operator [ ] to access an element in a tuple.Tuple elements are also index based. It also support positive and negative index as string and list supports.
my_tup = (10,20,"hello",30,40,50,3.2)
<---- +Ve indexes --->
<---- -Ve indexes --->
0 1 2 3 4 5 6 10 20 'hello' 30 40 50 3.2 -7 -6 -5 -4 -3 -2 -1
Access the Element of Tuple :
We can access the element of list using index( +ve or -ve)
my_tup = (10,20,"hello",30,40,50,3.2) print(my_tup) [OUTPUT] 10 print(my_tup) [OUTPUT] 'hello' print(my_tup[-1]) [OUTPUT] 3.2 print(my_tup) remember [OUTPUT] Traceback (most recent call last): File "
", line 1, in print(my_List) IndexError: list index out of range
Tuple slicing :
We can slice a tuple or get a sub-tuple by using slicing operator [ : ].
For more details like syntax refer to String section.
Revision tuple[a:b] means tuple[a], tuple[a+1], …, tuple[b-1] # all tuple elements with indexes from a to b; # including a and excluding b -------------------------- my_tup = (10,20,30,40,50) x = my_tup[1:4] print(x) [OUTPUT] (20,30,40) print(my_tup[ : ]) similar to my_tup[0:len(my_tup)] [OUTPUT] (10,20,30,40,50) print(my_tup[::-1]) reverse [OUTPUT] (50, 40, 30, 20, 10)
Tuple Operations :
a = (1, 2, 3) b = (4, 5, 6) c = a + b use + operator print(c) [OUTPUT] (1, 2, 3, 4 ,5,6)
a = (1, 2, 3) print(a*3) use * operator [OUTPUT] (1, 2, 3, 1, 2, 3, 1, 2,3)
Since, tuples are immutable, we can not update / modify it.
my_tuple = (10,20,30,40,50) my_tuple = 100 update index 1 value [OUTPUT] Traceback (most recent call last): File "
", line 1, in < module> my_tuple = 100 TypeError: 'tuple' object does not support item assignment
As we know, we can not change the tuple because tuples are immutable objects. Using the del operator, we can't delete the elements of tuple but entirely tuple deletion is possible.
names = ('adam', 'carol', 'henry', 'margot', 'phil') del names delete the value at index 3 print(names) [OUTPUT] Traceback (most recent call last): File "
", line 1, in < module> del names TypeError: 'tuple' object does not support item deletion Deleting entire tuple del names print(names) [OUTPUT] NameError: name 'names' is not defined
List Methods :
|count(x)||Returns the number of items x.|
|index(x)||Returns the index of the first item that is equal to x.|
|all()||Returns true if all element are true or if tuple is empty.|
|any()||return true if any element of the tuple is true. if tuple is empty, return false.|
|len()||Returns length of the tuple or size of the tuple.|
|enumerate()||Returns enumerate object of tuple.|
|max()||return maximum element of given tuple.|
|min()||return minimum element of given tuple.|
|sum()||Sums up the numbers in the tuple.|
|sorted()||input elements in the tuple and return a new sorted list.|
|tuple()||Convert an iterable to a tuple.|
Program to understand the functions :
>>> my_tup = (10,20,30,40,10,10) >>> print(my_tup.index(10)) 0 >>> print(my_tup.count(10)) 3 >>> print(all(my_tup)) True >>> print(any(my_tup)) True >>> print(len(my_tup)) 6 >>> print(max(my_tup)) 40 >>> print(min(my_tup)) 10 >>> print(sum(my_tup)) 120 >>> print(sorted(my_tup)) [10, 10, 10, 20, 30, 40]
Tuple Iteration :
for loop we can iterate each item in a tuple.
cities = ("Delhi","Mumbai","Kolkata","Chennai","Bangalore") >>> for item in cities: print(item) [OUTPUT] Delhi Mumbai Kolkata Chennai Bangalore
Next chapter is python Set
Training For College Campus
We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.
Please mail your requirement at email@example.com
Projects For Students
Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.
Students can mail requirement at firstname.lastname@example.org