Saturday, April 14, 2018

Sample Program – Python Tuple

Posted by AnithaKamatchi on Saturday, April 14, 2018 in | No comments


pythonTuple.py

custInfo = ('CusId','CusName','City','Mobile')
custData = (1001,'Anitha','Pondicherry',876712349)

print("*****Cust Info*****")
print(custInfo[0],':',custData[0])
print(custInfo[1],':',custData[1])
print(custInfo[2],':',custData[2])

tuple_1,tuple_2 = (1,2,3,4),(1,7,8)
empty_tuple = ()

print('\nTuple 1: ',tuple_1,'\nTuple 1 count: ',len(tuple_1),'\nMax Value: ',max(tuple_1),'\nMin Value: ',min(tuple_1))
print('Tuple 2: ',tuple_2 )

#print empty tuple
print("Empty Tuple: ",empty_tuple)

#Assign value to empty tuple
empty_tuple = tuple_1 + tuple_2
print("Empty Tuple after assigning values: ",empty_tuple)

#converting list to a tuple
list_1 = ['Grapes','Apple','Orange']
list2Tuple = tuple(list_1)
print("List after converting it to tuple: ",list2Tuple)

#displaying a range from a tuple
print("Tuple_2 with first 2 values: ",tuple_2[0:2])
print("Tuple_2 with from 2nd index: ",tuple_2[1:])


output:
*****Cust Info*****
CusId : 1001
CusName : Anitha
City : Pondicherry

Tuple 1:  (1, 2, 3, 4)
Tuple 1 count:  4
Max Value:  4
Min Value:  1
Tuple 2:  (1, 7, 8)
Empty Tuple:  ()
Empty Tuple after assigning values:  (1, 2, 3, 4, 1, 7, 8)
List after converting it to tuple:  ('Grapes', 'Apple', 'Orange')
Tuple_2 with first 2 values:  (1, 7)
Tuple_2 with from 2nd index:  (7, 8)


0 comments:

Post a Comment