TUPLE
Tuples are used to hold together multiple objects. Think of them as similar to lists, but without the extensive functionality that the list class gives you. One major feature of tuples is that they are immutable like strings i.e. you cannot modify tuples. Tuples are defined by specifying items separated by commas within an optional pair of parentheses. Tuples are usually used in cases where a statement or a user-defined function can safely assume that the collection of values i.e. the tuple of values used will not change.
###Tuple
var1=(1,2,3)
print("Values in Tuple ",var1)
print("Second Value in Tuple ",var1[1])
#var1[1] = 5 #Immutable
fruits1 = []
fruits1.insert(1,"Orange")
fruits1.insert(0,"Mango")
fruits1.insert(2,"Banana")
print("Type of Fruits ",type(fruits1))
fruits1 = tuple(fruits1)
print("Type of Fruits ",type(fruits1))
fruits1 = list(fruits1)
print("Type of Fruits ",type(fruits1))
#Tuples are very similar to List but doesnt allow mutability because of which
# it is faster to do programs using Tuples
More on Tuples
A tuple is just like a list of a sequence of immutable python objects.
The difference between list and tuple is that list are declared in square brackets and can be changed while tuple is declared in parentheses and cannot be changed.
However, you can take portions of existing tuples to make new tuples.
Syntax
Tup = ('Jan','feb','march')
Tuples Programs in the Class
#Comparing Tuples
#A comparison operator in Python can work with tuples.
#The comparison starts with a first element of each tuple.
#If they do not compare to =,< or > then it proceed to the second element and so on.
#It starts with comparing the first element from each of the tuples.
#case 1
a=(5,6)
b=(1,4)
if (a>b):print("a is bigger")
else: print("b is bigger")
#case 2
a=(5,6)
b=(5,4)
if (a>b):print("a is bigger")
else: print ("b is bigger")
#case 3
a=(5,6)
b=(6,4)
if (a>b):print("a is bigger")
else: print("b is bigger")
#Packing and Unpacking
#In packing, we place value into a new tuple while in unpacking we extract those values back into variables.
x = ("Quest Learning", 10, "Education") # tuple packing
(company, emp, profile) = x # tuple unpacking
print(company)
print(emp)
print(profile)
#Creating Nested Tuple
#Using a for loop we can iterate though each item in a tuple.
for name in ('Sachin','Sourav','Rahul','Sehwag'):
print("Hello",name)
#Using Tuples as keys in Dictionaries
#Since tuples are hashable, and list is not, we must use tuple as the key if we need to create a composite key to use in a dictionary.
#Example
#We would come across a composite key if we need to create a telephone directory that maps, first-name, last-name, pairs of telephone numbers, etc:
# directory[last,first] = number
#Implementation:
directory = {}
directory[("Sachin","Tendulkar")] = 33338888
directory[("Mahi","Dhoni")] = 44448888
#Inside the brackets, the expression is a tuple. We could use tuple assignment in a for loop to navigate this dictionary.
for last, first in directory:
print(first, last, directory[last, first])
#This loop navigates the keys in the directory, which are tuples. It assigns the elements of each tuple to last and first and then prints the name and corresponding telephone number.
#Deleting Tuples
#Tuples are immutable and cannot be deleted, but
# deleting tuple entirely is possible by using the keyword "del."
del a
#Slicing of Tuple
#To fetch specific sets of sub-elements from tuple or list, we use this unique function called slicing. Slicing is not only applicable to tuple but also for array and list.
x = ("a", "b","c", "d", "e")
print(x [2:4])
#Tuple Membership Test
#We can test if an item exists in a tuple or not, using the keyword in.
my_tuple = ('a','p','p','l','e',)
print('a' in my_tuple)
# Output: True
print('b' in my_tuple)
# Output: False
# Not in operation
print('g' not in my_tuple)
# Output: True