The list is used to store a list of values in a single variable. Although there are some differences you can call it an array.
names = ["Jone", "Doe", "Jems"]
names = (("Jone", "Doe", "Jems"))
Print the list:
print(names)
You can access items on the list by its index. Keep in mind that the index starts from 0. So first element is names[0].
print(names[0])
print(names[1])
print(names[2])
names[0] = "Mamun"
names[1] = "Sarkar"
student = ["Jone Doe", 10, True, 3.5, "male"]
len(names)
We can access the list by using Negative Indexing. -1 for the last item and -2 for the second last item and so on.
print(names[-1])
print(names[-2])
l = ['one', 'two', 'three', 'four', 'fine', 'six']
print (l[1:3])
print (l[:4])
print (l[2:])
Output:
['two', 'three']
['one', 'two', 'three', 'four']
['three', 'four', 'fine', 'six']
l = ['one', 'two', 'three', 'four', 'fine', 'six']
print (l[-3:-1])
print (l[:-2])
print (l[-4:])
Output:
['four', 'fine']
['one', 'two', 'three', 'four']
['three', 'four', 'fine', 'six']
print("two" in l)
It will return True if the item present otherwise returns False.
items =[
[1, 2, 3, 4],
[4, 5, 6, 7],
['One', 'Two', "Three"]
]
print(items[0][0])
print(items[2][1])
print(items[1][3])
Output:
1
Two
7