Python Tuple
Python Tuple
Topics :
• Creating Tuple
• Deleting Tuples
Dr. Smita R. Chavan 3
Tuple in Python
• Tuple is a collection which is ordered and unchangeable.
Allows duplicate members.
Creating Tuple :
• # empty tuple
• tup1 = ()
• print(tup1)
• month = ('Jan',‘Feb',‘March')
• print(month) Accept a number and create a tuple.
Exercise 1:
• Write a python program which will define tuple of month in
words & accept number from user and print month in
words.
• my_tuple = ('p',‘y',‘t',‘h',‘o',‘n')
• print(my_tuple[0])
• # Output: 'p‘
• print(my_tuple[5])
• # Output: ‘n‘
Dr. Smita R. Chavan 9
• my_tuple = ('p',‘y',‘t',‘h',‘o',‘n')
• print(my_tuple[3:6])
• o/p : (‘h',‘o',‘n')
• print(my_tuple[:3])
• o/p :('p',‘y',‘t')
• print(my_tuple[3:])
• o/p : (‘h',‘o',‘n')
Dr. Smita R. Chavan 10
• print(my_tuple [:])
o/p :('p',‘y',‘t',‘h',‘o',‘n')
• print(my_tuple[-2:])
• o/p :(‘o',‘n')
• print(my_tuple[:-2])
• o/p :('p',‘y',‘t',‘h')
• print(my_tuple[::2])
• o/p :('p',‘t',‘o')
• You can loop through the tuple items by using a for loop:
fruit_tuple = ("apple", "banana", "cherry")
Built-in functions
T = (25,90,45,12,5)
num =(34,67,23,34,89)
Deleting Tuples
del fruit
print(fruit)