- Tuple in python can be a set of values like a List in python
- What is a tuple?: It is a Collection of objects.
- The tuple is immutable.
- Tuple data structure: Duplicates are allowed and Insertion order is preserved.
- Python tuple contains heterogeneous element.
- How to create tuple in python?
You can create tuple using () brackets.
Tuple = (Values)
Tuple = (10,20,30)
- Create empty tuple
You can create an empty tuple by simply writing brackets.
Tuple syntax in python
Tuple = ()
Create empty tuple python
TuplePython123print("Empty Tuple")Tuple=()print("Tuple = ", Tuple)
It will create an empty tuple.
- One element tuple
You can create a tuple with only one element
Tuple = (argument,)
Python create tuple
TuplePython123print("One element Tuple")Tuple=(10,)print("Tuple = ", Tuple)
The comma is important for one element otherwise it is treated as an int.
- Multi-element tuple
You can create a tuple with any number of an element.
Tuple = (argument)
TuplePython123print("Tuple")Tuple=(10,20,30)print("Tuple = ", Tuple)
- Note:- To create tuple we use () bracket.
- Accessing element of the tuple
You can access elements of the tuple by using index or slice operator.
Access tuple python
- How to create dynamic Tuple?
You can create the dynamic Tuple by taking the input as a tuple using the eval function.
Take dynamic Tuple input
Python Tuple data structurePython123Tuple = eval(input("Enter Tuple = "))print("Tuple = ",Tuple)print(type(Tuple))
- Traversing elements of the Tuple
You can traverse the Tuple using its index
- What is traversing? The sequential accessing each element of the Tuple is traversing.
- You can traverse elements of the Tuple by using looping statements in python: for loop or while loop.
- Traverse Tuple using while loop
Using while loop you can access each and every element of the tuple.
python tuplePython123456T = (10, 20,30)I = 0print("Tuple Itesms: ")while I < len(T):print(T[I])I = I +1
- Traverse tuple using for loop
Another way to traverse tuple we can use is for loop.
python TuplePython1234T = (10, 20,30)print("Tuple Itesms: ")for x in T:print(x)
- You can use + and * operator for the tuple.
Tuple function in python
- Python provides various inbuilt functions for the tuple.
- Python tuple methods are as follows:
To find How many elements are there in tuple we use len() function.
Length function in python: If you pass the tuple to len() function it gives the length of the tuple. Len is not a method of any class, it’s a function provided by python
Length of tuple python
Tuple lengthPython12T = (10, 20,30)print("Tuple Legth = ",len(T))
- Count function
To check the number of occurrences of argument in the tuple
Count function in python: This function gives the number of occurrences of the variable.
Tuple countPython12T = (10, 20,30,10)print("Count for 10 in tuple = ",T.count(10))
To know the index of the variable
How to use the index function in python
python IndexPython12T = (10, 20,30,10)print("Index of 20 in tuple = ",T.index(20))
If more occurrences of the variable are present then index() function gives the first occurrence index of the variable.python indexPython12T = (10, 20,30,10)print("Index of 10 in tuple = ",T.index(10))
If the specified element not available you will get an error.indexPython12T = (10, 20,30,10)print("Index of 10 in tuple = ",T.index(50))
So, before using the index() function make sure that you first read the element of the a tuple then use index function.
To check element present in the tuple or not use membership operator.
Membership operator: in and not in
We can now write the the index functionpython IndexPython12345678T = (10, 20,30,10)print("Tuple = ",T)ele = int(input ("Enter value to check in tuple = "))print(ele)if ele in T:print("Index of ",ele,"in tuple = ",T.index(ele))else:print("Element not in tuple")
To perform sorting for the tuple.
To sort element based on natural sorting order.
It is python inbuilt function
Natural sorting order
For number sorting done in ascending order
For string done in alphabetical order.
Sort according to natural sortingSorted tuplePython123T=(10,2,1,4)print("Tuple = ",T)print("Sorted Tuple = ",sorted(T))
You cannot use sort() it will give you an error.sortPython1234T=(10,2,1,4)print("Tuple = ",T)T.sort()print("Sorted Tuple = ",T)
Because a tuple is immutable, once we created tuple we cannot modify it or recreate it
So for this, you need to move tuple to list.
Why sort() is not available for tuple
Because a tuple is immutable
Sorted () else gone return a list, so you need to convert it into the tuple.
Sort according to the reverse of sorted order (descending).
ExampleTuplePython1234T=(10,2,1,4)print("Tuple = ",T)T1 = tuple(sorted(T,reverse=True))print("Sorted Tuple = ",T1)
- Find min and max value
You can find min and max value in the tuple
Python provides min() and max () you can apply min and max for only homogenous values.
Syntax for min()
Examplepyhon minPython123T=(10,2,10,4)print("Tuple = ",T)print("Min value in Tuple = ",min(T))
Syntax for max()
Examplepython maxPython123T=(10,2,10,4)print("Tuple = ",T)print("Max value in Tuple = ",max(T))
Comparison in tuple
- Cmp() function
Cmp() function is available in python 2 not in python 3.
To compare tuple: – it compares elements of a tuple
If both are same, return 0
If first tuple < tuple 2 returns -1
If first tuple > tuple 2 returns 1
- The other comparison operator such as <, <=, > , >= , == is applicable for the tuple.
Examplepython tuple comparisonPython1234T1=(10,2,10,4)T2=(5,20,10,4)print(T1< T2)print(T1>T2)
It gone check first element is it is false then it will not check rest element.
Tuple packing and unpacking
- Suppose you have a group of values and you want to pack these group of values into tuple then you can do this.
- And you have tuple and you want to make it as individual element then you can also so this ( unpacking)
- Packing means grouping into single.
Tuple packingPython12345678A = 10B=20T= (A, B)print("A = ",A)print("B = ",B)print("Tuple packing")print("Tuple = ",T)
Group elements into single tuple are tuple packing.
- Tuple unpacking
Example.Tupple unpackPython1234567T=(10,20,30)print("Tuple = ",T)A,B,C = Tprint("Tuple unpacking")print("A = ",A)print("B = ",B)print("C = ",C)
Unpack tuple element to individual element is tuple unpacking.
- Tuple comprehension is not supported it gone run but you will get generator object
TuplePython12T=(x*x for x in range(1, 11))print(T)