A list in Python is used to store the sequence of various types of data. Python lists are mutable type its mean we can modify its element after it created. The items in the list are separated with the comma (,) and enclosed with the square brackets [].
Syntax:-
L1 = ["Mohit", 102, "INDIA"]
List Indexing and Slicing
The indexing is processed in the same way as it happens with the strings. The elements of the list can be accessed by using the slice operator [].
The index starts from 0 and goes to length - 1.
Example:-
list = [1,2,3,4,5,6,7]
print(list[0])
print(list[1])
print(list[2])
print(list[3])
# Slicing the elements
print(list[0:6])
# By default the index value is 0 so its starts from the 0th element and go for index -1.
print(list[:])
print(list[2:5])
print(list[1:6:2])
Output:-
1
2
3
4
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6, 7]
[3, 4, 5]
[2, 4, 6]
Exercises:-
- Write a py program to find the length of the list
- Write a py program to find the maximum in the list
- Write a py program to find the minimum in the list
- Write a py program to add an element in the list
- Write a py program to count the number of times an item appeared in the list
- Write a py program to find the index of a particular item
- Write a py program to delete an item from the list
- Write a py program to reverse the list items
- Write a py program to sort the list items
Comments
Post a Comment