List and Tuples in Python with example
- How to declare a List
list1 = ['Tam', 'Raj', 1997, 2000];
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
tup1 = ('Cat', 'Mat', 1997, 2000, 7000);
- 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 valuesprint(list1[0])print(list1[1])# To get the size of listprint(len(list1))# To print a all values from the List having multiple valuesfor li in list1:print(li)
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- How to Access a Tuple
# To print a single values from the tuple having multiple valuesprint(tup1[0])print(tup1[1])# To get the size of Tupleprint(len(tup1))# To print a all values from the tuple having multiple valuesfor tu in tup1:print(tu)
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- What are the difference in List and Tuple.
- List are mutable which means we can change the values of a list however tuples are not.
- List are slower while access the data however Tuples are faster as compared to list
Post a Comment