#Tuples: Tuple is a collection datatype which holds group of values
#Tuple Properties:
1.Tuple represented using ()
2.Tuple allows both homogenous(same datatype) and heterogeneous(different datatype) elements
ex: x=(10,20,30,40,50) #homogenous elements
y=(101,"James",70890.70,[1,2,3]) #heterogeneous elements
3. Tuple allows duplicate elements ex: x=(10,20,30,10,20)
4. In tuple insertion order is preserved(followed) ,i.e the order in which the
elements are inserted,in the same order they are stored or displayed
5. Every element of a tuple,is identified or accessed by a unique index
tuple support 2 types of indexes:
1.Forward index (or) Positive index---->starts from left to right
----->starts with 0
2.Backward index(or) Negative index----->Starts from right to left
----->starts with -1.
6. Tuple is a immutable object i,e changes and modifications are not allowed
within a tuple
but elements of a tuple can be either mutable or immutable
ex: y=(101,"James",70890.70,[1,2,3])
7. tuple can also be created by using tuple() function.
#Creating tuples
x=(10,20,30,40,50) #Homogeneous tuple
print(x)
print(type(x))
print(id(x))
print(len(x))
y=(101,"Blake",70890.70,"pune") #Heterogeneous tuple
print(y)
print(type(y))
print(len(y))
z=10,20,30
print(z)
print(type(z))
print(len(z))
cities="hyd","pune","mumbai","chennai"
print(cities,type(cities),len(cities))
w=("python")#If there is only one element within a tuple then it is not atuple.
print(w,type(w),len(w))
p=("python",)
print(p,type(p),len(p))
q=(10)
print(q,type(q))
r=(10,)
print(r,type(r),len(r))
s1=10
print(s1,type(s1))
s2=10,
print(s2,type(s2),len(s2))
x=([10,20,30])
print(x,type(x),len(x))
x=([10,20,30],[40,50])
print(x,type(x),len(x))
print(len(x[0]))
print(len(x[1]))
y=({10,20,30})
print(y,type(y),len(y))
x=[10]
print(x,type(x),len(x))
#-------------------------------------------------------------------------------------------
#Creating a tuple using tuple():
x=tuple() #Empty tuple created
print(x)
print(type(x))
print(len(x))
#Creating a tuple with content
x=tuple("mumbai")
print(x)
print(type(x))
print(len(x))
#Ex:2
'''
x=tuple("mumbai","chennai")#Error ----->Bcoz tuple() accepts only one argument and
print(x) #that argument should be of iterable type like string,list,tuple,set
print(type(x))
print(len(x))
'''
#Ex:3
'''
x=tuple(10) #Error---->bcoz int is not iterable.
print(x)
print(type(x))
print(len(x))
'''
#ex:4
x=tuple(["mumbai","chennai"])
print(x)
print(type(x))
print(len(x))
#Note:If we pass a string to a tuple() function--->then each character of
#string will be take as element of the tuple.
# IF we pass any collection to a tuple() function--->each element of the
#collection will be considered as each element of the tuple.
#ex:5
x=tuple((10,20,30))
print(x)
print(type(x))
print(len(x))
#ex:6
x=tuple({10,20,30,40})
print(x)
print(type(x))
print(len(x))
#Diff between a List and a Tuple
'''
1.using list we can perform the following operations
1.Insertions
2.Updations
3.Deletions
'''
emp=[101,"Ajay","Asst.Manager",50000]
print(emp)
#after some days, the emp got promotion,he became manager and his salary got hiked
emp[2]="Manager"
emp[3]=80000
print(emp)
#here we can modify
#we can also insert---->dept---->"sales"
emp.append("sales")
print(emp)
#----------------------------------------------------------------------------------------------
#2.Using tuple, we cannot perform the above operations
# we can only perform Retrieval operations(read operation)
x=("Ajay",30987650,34)
print(x)
#Retrieve only rank
print(x[2])
#Tuple slicing:
x=("Amar","Rohith","Rahul","Blake","James","Miller")
#Extract----"Blake" using -ve index
print(x[-3])
print(x[3])
#Extract---->first 3 names
print(x[0:3])#Always upper bound is excluded
#Extract---->"Rahul","Blake","James"
print(x[2:5])
print(x[5:2]) #Empty tuple--->bcoz always slicing in the forward direction
#Extract---->"Rahul","Blake","James" using -ve index
print(x[-4:-1])
#Extract--->"Rahul" to "miller"
print(x[2: ])
print(x[2:6])
print(x[-4: ])
print(x[ :3])
print(x[ : ])
print(x)
print(x[0: ])
print(x[0:6:2])
print(x[::2])
print(x[::-1]) #reverses the elements of a tuple
print(x[6::-1])
#Working with tuple addresses
#ex1: 2 Lists with same content
x=[10,20,30,40,50]
y=[10,20,30,40,50]
print(x,id(x))
print(y,id(y))
print(x is y) #Compares the address
print(x == y) #Compares the content
#-------------------------------------------------------------------------------------------
#ex:2 Tuples with same content
a=(10,20,30,40,50)
b=(10,20,30,40,50)
print(a,id(a))
print(b,id(b))
print(a is b) #Compares the address
print(a == b) #Compares the content
#Note: 2 Mutable objects with same content always returns 2 diff objects with
# 2 diff addresses' but immutable objects with same content returns only
# one object with only one address.
#------------------------------------------------------------------------------------------
#ex:3
x=([10,20,30,40,50])
y=([10,20,30,40,50])
print(x,type(x),len(x),id(x))
print(y,type(y),len(y),id(y))
print(x is y) #Compares the address
print(x == y) #Compares the content
#------------------------------------------------------------------------------------------
#ex:4
x=[(10,20,30,40,50)]
y=[(10,20,30,40,50)]
print(x,type(x),len(x),id(x))
print(y,type(y),len(y),id(y))
print(x is y) #Compares the address
print(x == y) #Compares the content
#---------------------------------------------------------------------------------------------
#ex:5
p=((10,20,30),(40,50,60),[70,80,90])
q=((10,20,30),(40,50,60),[70,80,90])
print(p,type(p),id(p))
print(q,type(q),id(q))
print(p is q)
print(p == q)
#Note: 2 Tuples with same content--------->only one object is created
#but if the tuples contain list or set or dictionary within it---->diff objects will be created---
#with diff addresses
#----------------------------------------------------------------------------------------------
#ex:6
print(p[0][0],type(p[0][0]),id(p[0][0]))
print(q[0][0],type(q[0][0]),id(q[0][0]))
print(p[0][0] is q[0][0])
a=10
print(a is p[0][0])
x=[10,20,30]
print(x,len(x))
print(x[0] is p[0][0])
#Accepting list of emps from the user
emps=[]
n=int(input("Enter no of employees to be added :"))
for p in range(1,n+1):
x=input("Enter the name of emp"+str(p)+":")
emps.append(x)
print(emps)
#Tuples within a tuple
x=(("Kohli",95),("Dhoni",45),("Rohith",82),("Hardik",30))
print(x,len(x))
for p in x:
print(p,type(p))
for p,q in x:
print("PLAYER:",p)
print("RUNS:",q)
print("====================")
#--------------------------------------------------------------------------------------------
#ex:2
emps=((101,"Miller",50000,"Manager"),
(102,"Blake",60000,"Professor"),
(103,"James",70000,"Teacher"),
(104,"Ajay",80000,"Principal"))
print(emps,len(emps))
for p in emps:
print(p,type(p))
for p,q,r,s in emps:
print("EID:",p)
print("ENAME:",q)
print("SALARY:",r)
print("DESIGNATION:",s)
print("=======================")
#Modifying a Tuple
x=(10,20,30,40,50)
print(x,type(x))
x[2]=35 #Error--->tuple object doesnt support item assignemt
print(x)
#Case 1: List as Tuple element
x=((10,20,30),(40,50,60),[70,80,90])
print(x,len(x))
#x[0][1]=25
x[2][0]=75
print(x)
x[2]="python"
#Case 2: tuple as list element
x=[[10,20,30],[40,50,60],(70,80,90)]
print(x,len(x))
x[0][1]=25
x[1][2]=3.5
#x[2][0]=75
print(x)
x[2]="python"
print(x)
x[1]=True
x[0]=3.5
print(x)