PREV NEXT
- Python tuple is a immutable object ,it is combination of homogeneous or heterogeneous data types
- To access the elements in tuple we can use index,which start from 0 and stop with n-1
- In tuple elements are stored in sequence manner
- In tuple every element is separated by comma
- We can create empty tuple also in python.
- In tuple elements are enclosed in paranthesis “()”
Different Types of Syntax:
Creating Empty tuple:
tuple=()
Note: It will create empty tuple
Create tuple with data:
tuple_name=(data1,data2,data3……….datan)
How print the complete tuple:
Example:
1 2 3 |
custom_tuple=(1,2,3,4,5) print custom_tuple |
Output:
1 |
(1, 2, 3, 4, 5) |
How to access the tuple elements:
To access the elements in tuple there two tw type of indexing is available.
They are
1.Forward Indexing
2.Backward Indexing
1.Forward Indexing:
The starting index ( mostly ‘0 ‘ ) is positive and ending index (n-1) is also positive then we can call it as Forward Indexing i.e almost it is in ascendiing order.i.e consit tuple having an elements are 1,2,3,4,5,6,7
my_tuple=(1,2,3,4,5,6,7)
In this case start index is ‘0’ and end index is ‘6’
my_tuple[0] is 1
my_tuple[1] is 2
my_tuple[2] is 3
my_tuple[3] is 4
my_tuple[4] is 5
my_tuple[5] is 6
my_tuple[6] is 7
Below is the example is illustrating accessing the tuple elements for different data types
Examples:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
tuple_nums = (10,20,30,40) tuple_flt_nums=(1.5,2.6,3.8,9.5) tuple_chars =('a','b','c','d') tuple_str=("fruilts","vegs","raw","drinks") tuple_all=(10,52.5,"fruits",'x') print "tuple numbers" print "*************************" for i in range(len(tuple_nums)): print "tuple_num[%d]:%d"%(i,tuple_nums[i]) print "*************************" print "tuple float numbers" print "*************************" for i in range(len(tuple_flt_nums)): print "tuple_flt_nums[%d]:%d"%(i,tuple_flt_nums[i]) print "*************************" print "tuple characters" print "*************************" for i in range(len(tuple_chars)): print "tuple_chars[%d]:%s"%(i,tuple_chars[i]) print "*************************" print "tuple strings" print "*************************" for i in range(len(tuple_str)): print "tuple_str[%d]:%s"%(i,tuple_str[i]) print "*************************" print "tuple all" print "*************************" for i in range(len(tuple_all)): print "tuple_all[",i,"]:",tuple_all[i] print "*************************" |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
tuple numbers ************************* tuple__num[0]:10 tuple__num[1]:20 tuple__num[2]:30 tuple__num[3]:40 ************************* tuple_ float numbers ************************* tuple__flt_nums[0]:1 tuple__flt_nums[1]:2 tuple__flt_nums[2]:3 tuple__flt_nums[3]:9 ************************* tuple_ characters ************************* tuple__chars[0]:a tuple__chars[1]:b tuple__chars[2]:c tuple__chars[3]:d ************************* tuple_ strings ************************* tuple__str[0]:fruilts tuple__str[1]:vegs tuple__str[2]:raw tuple__str[3]:drinks ************************* tuple_ all ************************* tuple__all[ 0 ]: 10 tuple__all[ 1 ]: 52.5 tuple__all[ 2 ]: fruits tuple__all[ 3 ]: x ************************* |
2.Backward Indexing:
The starting index ( mostly ‘-n’ ) is nagative number and ending index (-n+(n-1)) is also negative number then we can call it as Backward Indexing i.e consider tuple having an elements are 1,2,3,4,5,6,7 i.e here no.of elements are 7 so starting index (-n) means -7 and ending index means (-n+(n-1)) -1
my_tuple=[1,2,3,4,5,6,7]
In this case start index is ‘0’ and end index is ‘6’
my_tuple[-7] is 1
my_tuple[-6] is 2
my_tuple[-5] is 3
my_tuple[-4] is 4
my_tuple[-3] is 5
my_tuple[-2] is 6
my_tuple[-1] is 7
Below is the example is illustrating accessing the tuple elements for different data types
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
tuple__nums = [10,20,30,40] tuple__flt_nums=[1.5,2.6,3.8,9.5] tuple__chars =['a','b','c','d'] tuple__str=["fruits","vegs","raw","drinks"] tuple__all=[10,52.5,"fruits",'x'] print "tuple_ numbers" print "*************************" n=len(tuple__nums) for i in range(n): print "tuple__num[%d]:%d"%(i-n,tuple__nums[i]) print "*************************" n=len(tuple__flt_nums) print "tuple_ float numbers" print "*************************" for i in range(n): print "tuple__flt_nums[%d]:%d"%(i-n,tuple__flt_nums[i]) print "*************************" n=len(tuple__chars) print "tuple_ characters" print "*************************" for i in range(n): print "tuple__chars[%d]:%s"%(i-n,tuple__chars[i]) print "*************************" n=len(tuple__str) print "tuple_ strings" print "*************************" for i in range(n): print "tuple__str[%d]:%s"%(i-n,tuple__str[i]) print "*************************" n=len(tuple__all) print "tuple_ all" print "*************************" for i in range(n): print "tuple__all[",i-n,"]:",tuple__all[i] print "*************************" |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
tuple_ numbers ************************* tuple__num[-4]:10 tuple__num[-3]:20 tuple__num[-2]:30 tuple__num[-1]:40 ************************* tuple_ float numbers ************************* tuple__flt_nums[-4]:1 tuple__flt_nums[-3]:2 tuple__flt_nums[-2]:3 tuple__flt_nums[-1]:9 ************************* tuple_ characters ************************* tuple__chars[-4]:a tuple__chars[-3]:b tuple__chars[-2]:c tuple__chars[-1]:d ************************* tuple_ strings ************************* tuple__str[-4]:fruits tuple__str[-3]:vegs tuple__str[-2]:raw tuple__str[-1]:drinks ************************* tuple_ all ************************* tuple__all[ -4 ]: 10 tuple__all[ -3 ]: 52.5 tuple__all[ -2 ]: fruits tuple__all[ -1 ]: x ************************* |
Note: If we observe above both examples ,here we can consider “tuple__nums “ tuple_ explanation
tuple__nums [0]=tuple__num[-4]=10
tuple__nums [1]=tuple__num[-3]=20
tuple__nums [2]=tuple__num[-2]=30
tuple__nums [3]=tuple__num[-1]=40
How to slice the element in tuple:
tuple slice can be used slice particular part of the tuple and as well as to modify the particular part of the tuple we can use the slice mechanism in tuple
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
tuple1=(9,6,5,8,1,3,7,4) print tuple1 #printing the complete tuple print tuple1[:-5] #This will slice the elements from -5 to backward index print tuple1[-5:] #This will slice the elements from -5 to forward index print tuple1[:5] #This will slice the elements from 5 to backward index print tuple1[5:] #This will slice the elements from 5 to forward index print tuple1[2:5] #This willslice the elements in between 2:5 |
Output:
1 2 3 4 5 6 7 8 9 10 11 |
(9, 6, 5, 8, 1, 3, 7, 4) (9, 6, 5) (8, 1, 3, 7, 4) (9, 6, 5, 8, 1) (3, 7, 4) (5, 8, 1) |
How to replicate the python tuple:
Here Replicate means adding the same tuple multiple time wit all elements, we can achieve this mechanism by using ateristic symbol (‘*’)
Example:
1 2 3 4 5 6 7 8 9 |
tuple =(1,2,3) print tuple*1 #No replication print tuple*2 #one time replication print tuple*3 #two time replication print tuple*4 #three time replication |
Output:
1 2 3 4 5 6 7 |
(1, 2, 3) (1, 2, 3, 1, 2, 3) (1, 2, 3, 1, 2, 3, 1, 2, 3) (1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3) |
How to add tuple,delete the tuple:
Adding Two tuple:
Example:
1 2 3 4 5 6 7 |
tuple1=(1,2,3) tuple2=(4,5,6) tuple=tuple1+tuple2 print tuple |
Output:
1 |
(1, 2, 3, 4, 5, 6) |
Update Tuple :
We should not update the tuple because of it is immutable,it means it is having the constant values
1.del method
1.del method:
We delete the complete tuple,But we can’t delete the specific or individual elements in tuple by usong delete method.
Syntax:
del(index) Note: Here index might be a single value ,slice of tuple also we can use
Example:
1 2 3 4 5 6 7 |
tuple = ("xyz", 420, 99.90)# tuple with the name number and marks print "tuple before delete operator:", tuple del tuple print "tupes is not avilable:", tuple |
Output:
1 2 3 4 5 6 7 8 9 10 11 |
tuple before delete operator: ('xyz', 420, 9960599605L) tupes is not avilable: Traceback (most recent call last): File "del_tuple.py", line 4, in <module> print "tupes is not avilable:", tuple NameError: name 'tuple' is not defined |
Max,Min,Count,Len and Sum Methods:
Max Method:
This Method is used to find the biggest element in the tuple.
Min Method:
This Method is used to find the smallest element in the tuple.
Count Method:
This Method is used to count the occurance of specific element in the tuple.
Len Method:
This Method is used to count the totla elements in the tuple.
Sum Method:
This Method is used find the sum of the elements in the tuple.
Example:
1 2 3 4 5 6 7 8 9 10 11 |
tuple=(100,269,32,269,1000,21) print "Biggest element in tuple:",max(tuple) print "Smallest element in tuple:",min(tuple) print "sum of elements in tuple:",sum(tuple) print "count the number of elements in tuple:",len(tuple) print "count the element occurance no.of times:",tuple.count(269) |
Output:
1 2 3 4 5 6 7 8 9 |
Biggest element in tuple: 1000 Smallest element in tuple: 21 sum of elements in tuple: 1691 count the number of elements in tuple: 6 count the element occurance no.of times: 2 |