Download our e-book of Introduction To Python
Pallavi Dhotre
3 years ago
# python tuple example
demo=() #empty tuple
demo=(1,) #tuple with single element
demo=(1,2,3,4) #tuple with integer values
demo=('Ram','Sham','Siya') #tuple of strings
# python tuple example
demo_tuple=('Ram','Sham','Siya') #tuple of strings
print(demo_tuple[0]) #this will access the first element
print(demo_tuple[1]) #this will access the second element
print(demo_tuple[2]) #this will access the third element
Ram
Sham
Siya
#example of tuple in python
demo_tuple=('Ram','Sham','Siya') #tuple of strings
print(demo_tuple) #this will display the python tuple before deleting
del demo_tuple #deleting entire tuple
print(demo_tuple)
('Ram', 'Sham', 'Siya')
Traceback (most recent call last):
File "C:/Users/Pallavi/PycharmProjects/demo/demo1.py", line 4, in <module>
print(demo_tuple)
NameError: name 'demo_tuple' is not defined
a=(10,20,55,44,34,9,6) #first tuple
print(len(a))#display the length of the list
7
a=(12,3,4,44,2,34,6) #tuple
print(max(a)) #display max element from the tuple
44
a=(12,3,4,44,2,34,6) #tuple
print(min(a)) #display min element from the tuple
2
a=(12,3,4,44,2,34,6) #tuple
print(sum(a)) #display sum of the elements in the tuple
105
a=(12,3,4,44,2,34,6) #tuple
print(sorted(a)) #display sorted tuple
[2, 3, 4, 6, 12, 34, 44]
#example of tuple in python
a=(10,20,55,44,34,9,6) #first tuple
b=(1,2,3) # second tuple
print(a+b) #python tuples concatenation
print(b*4) #python tuples replication
(10, 20, 55, 44, 34, 9, 6, 1, 2, 3)
(1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3)
listOfTuple=[(1,2,4),('ram','sham')]
print(listOfTuple)
[(1, 2, 4), ('ram', 'sham')]
#write a python program to find the repeated items of a tuple
demo_tuple=(1,2,3,1,4,3,4,5,2,1,) #tuple
print(demo_tuple) #display tuple
c=demo_tuple.count(1) #count the repeted item
print(c) #display number of occurances
(1, 2, 3, 1, 4, 3, 4, 5, 2, 1)
3