what are list and tuple in python code example
Example 1: what is a tuple in python
# A tuple is a sequence of immutable Python objects. Tuples are
# sequences, just like lists. The differences between tuples
# and lists are, the tuples cannot be changed unlike lists and
# tuples use parentheses, whereas lists use square brackets.
tup1 = ('physics', 'chemistry', 1997, 2000);
tup2 = "a", "b", "c", "d";
# To access values in tuple, use the square brackets for
# slicing along with the index or indices to obtain value
# available at that index.
tup1[0] # Output: 'physics'
Example 2: python list of tuples
#List of Tuples
list_tuples = [('Nagendra',18),('Nitesh',28),('Sathya',29)]
#To print the list of tuples using for loop you can print by unpacking them
for name,age in list_tuples:
print(name,age)
#To print with enumerate--->enumerate is nothing but gives the index of the array.
for index,(name,age) in list_tuples:
#print using fstring
print(f'My name is {name} and age is {age} and index is {index}')
#print using .format
print('My name is {n} and age is {a} and index is {i}'.format(n=name,a=age,i=index))