Tuples
Tuples
days = ("monday", "tuesday", "wednesday", "thursday", "friday",
"saturday", "sunday")
# append, pop, insert, del, remove ---> can't be used on tuples
# Tuples are immutable
# Tuples are faster than lists
mixed_tuple = ("string1", 23, True, 3.4, [12, "str2", False], ("str3", 64))
# methods
# count, len
# indexing
print(mixed_tuple[4])
print(mixed_tuple[4][1])
mixed_tuple[4][1] = "Hello"
print(mixed_tuple)
# mixed_tuple[2] = "Hello" ---> error
# print(mixed_tuple)
# slicing
# print(mixed_tuple[1:5])
# Tuple with one element
x = ("string",)
print(type(x))
# Tuple without parenthesis
months = "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec"
print(type(months))
# Tuple unpacking
songs = ("Dynamite", "ON", "Mic Drop")
song1, song2, song3 = songs
print(song1)
print(song2)
print(song3)
# Looping on Tuples
# for loop
# for i in days:
# print(i)
# while loop
x = 0
while x < len(days):
print(days[x])
x += 1
# functions
# min, max, sum
num = (3,2,1,5,4)
print(min(num))
print(max(num))
print(sum(num))
def calc(a,b):
x = a + b
y = a - b
z = a * b
return x,y,z
# when we return more on value, we get them in the form of tuple
print(calc(5,2))
l = ["str1",34, True]
z = tuple(l)
print(type(z))
t = ("str1",34, True)
print(type(list(t)))
print(list("strings"))
print(type(str(l)))
Post a Comment