Tuple

'''
Python Tuples:
Tuple is a built-in data type used to store multiple items in a single variable.
Tuple items are separated by commas and enclosed within round brackets().

Basic Characteristics of Tuple:
1.Ordered: The items have a defined order and that order will not change.
2.Immutable: We cannot change, add, or remove items after tuple is created.
3.Allow duplicates: Tuples can have items with the same values.
4.Can contain mixed types: Like integers, strings, list etc.
'''
tup1 = (10, 20, 30, 40, 50)
print(tup1)                     #output = (10, 20, 30, 40, 50)
print(type(tup1))           #output = <class 'tuple'>

tup2 = (100)
print(type(tup2))           #output = <class 'int'>

tup3 = (100, )
print(type(tup3))           #output = <class 'tuple'>
print(type(tup3), tup3)     #output = <class 'tuple'> (100,)
#If we have only one item in the tuple, we have to put a comma after the item to make it "class tuple", otherwise Python considers it as "class integer".

#tup3[0] = 90
#print(tup3)                 #output = Typeerror: 'tuple' object does not support item assignment
#tup4 = (24, 2, 2002)
#tup4[0] = 28
#print(tup4)                 #output = Typeerror: 'tuple' object does not support item assignment 
#As tuple is immutable, we cannot change the items after the creation.

tup5 = (24, 2, 2002, "Sindhu", True)
print(tup5)                  #output = (24, 2, 2002, 'Sindhu', True)
print(tup5[0])               #output = 24
print(tup5[4])               #output = True
print(tup5[3])               #output = Sindhu
print(tup5[-1])              #output = True

country = ("India", "England", "Italy", "Japan")
if "India" in country:
    print("Yes India is present in tuple.")
else:
    print("India is absent.")
#output = Yes India is present in tuple.

country1 = country[1:4]
print(country1)             #output = ('England', 'Italy', 'Japan')
country2 = country[:4] 
print(country2)             #output = ('India', 'England', 'Italy', 'Japan')
country3 = country[1:-1]
print(country3)             #output = ('England', 'Italy')








Comments

Popular posts from this blog

Print Function

Arithmetic Operators

Sets in Python