- Like list only as sequence of values based on integer index, except they are – Immutable.
- List -> [ ], Dict -> { }, Tuple -> (, )
- Aabhar : http://do1.dr-chuck.com/pythonlearn/EN_us/pythonlearn.pdf
# Creating tuple using (, ) or tuple()
myTuple = tuple() # With no argument, creates an empty tuple
# Pass only Iteratable items only like string, list of tuple - no int/float
#myTuple2 = tuple(100)
myTuple = tuple('IND') #Will create a tuple with all chars individually
print(type(myTuple), " ----", myTuple)
# RES -> <class 'tuple'> ---- ('I', 'N', 'D')
myTuple2 = (100, ) # (100)- Without comma, it will be just plain int class
myTuple2 = (100, 200.0, (True,))
print(type(myTuple2), " ----", myTuple2)
# RES -> <class 'tuple'> ---- (100, 200.0, (True,))
# Traversing of elements
for myTups in myTuple2: print(myTups)
""" RES ->
100
200.0
(True,)
"""
for x in tuple('OM'): print(x)
""" RES ->
O
M
"""
for i in range(len(myTuple)): print(myTuple[i])
""" RES ->
I
N
D
"""
# Slice
print(myTuple2[1:])
# RES -> (200.0, (True,))
#TypeError: 'tuple' object does not support item assignment
#myTuple2[0] = 'Cant assing as immutable'
#The tuple assignment
person = ('Bachchan', 70)
name = person[0]
age = person[1]
print(name, "--", age)
# RES -> Bachchan -- 70
# Treat tuple more like a key-value pair for a symmetric tuples
myTup = ( ('XX', 'Male', 30), ('YY', 'Female', 55), ('ZZ', 'Female', 18) )
for name,gender,age in myTup: print(name, gender, age)
""" RES ->
XX Male 30
YY Female 55
ZZ Female 18
"""
# Comparing two lists - item by item until you get the very first match
print ( (1,2,3) > (1,1,3) )
# RES -> True
myEmail = 'xyz@google.com'
name, domain = myEmail.split('@') # Split is of type list
print(name, domain)
# RES -> xyz google.com