Python Interview Questions and Answers || List and Tuples in Python with example

 


List and Tuples in Python with example 

  • How to declare a List 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
        list1 = ['Tam', 'Raj', 1997, 2000];
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 

  • How to declare a Tuple
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
    
    tup1 = ('Cat', 'Mat', 1997, 2000, 7000);
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 

  • python code to check in python if its a list or a tuple in python

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 

print(type(tup1))
print(type(list1))

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  

  • How to Access a List 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  
# To print a single values from the List having multiple values
print(list1[0])
print(list1[1])
# To get the size of list
print(len(list1))
# To print a all values from the List having multiple values
for li in list1:
    print(li)
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  

  • How to Access a Tuple
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  
# To print a single values from the tuple having multiple values
print(tup1[0])
print(tup1[1])
# To get the size of Tuple
print(len(tup1))
# To print a all values from the tuple having multiple values
for tu in tup1:
    print(tu)
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  

  • What are the difference in List and Tuple.
  1. List are mutable which means we can change the values of a list however tuples are not.
  2. List are slower while access the data however Tuples are faster as compared to list    



Post a Comment