Sunday, February 2, 2020

List - Basic


List
A list is a collection which is ordered and changeable. In Python lists are written with square brackets.
Create a List:
list = ["aaa", "bbb", "ccc"]

Access Items with slicing
To access values in lists, use the square brackets for slicing along with the index or indices to obtain value available at that index.

my_list = ['p','r','o','g','r','a','m']
print(my_list[2:5])
print(my_list[:-5])
print(my_list[5:])
print(my_list[:])

Output

['o', 'g', 'r']
['p', 'r']
['a', 'm']
['p', 'r', 'o', 'g', 'r', 'a', 'm']



P
R
O
G
R
A
M
Positive index
0
1
2
3
4
5
6
Negative index
-7
-6
-5
-4
-3
-2
-1

No comments:

Post a Comment