1#Creating lists
2my_list = ['foo', 4, 5, 'bar', 0.4]
3my_nested_list = ['foobar', ['baz', 'qux'], [0]]
4
5#Accessing list values
6my_list[2] # 5
7my_list[-1] # 0.4
8my_list[:2] # ['foo', 4, 5]
9my_nested_list[2] # ['baz', 'quz']
10my_nested_list[-1] # [0]
11my_nested_list[1][1] # 'qux'
12
13#Modifying lists
14my_list.append(x) # append x to end of list
15my_list.extend(iterable) # append all elements of iterable to list
16my_list.insert(i, x) # insert x at index i
17my_list.remove(x) # remove first occurance of x from list
18my_list.pop([i]) # pop element at index i (defaults to end of list)
19my_list.clear() # delete all elements from the list
20my_list.index(x[, start[, end]]) # return index of element x
21my_list.count(x) # return number of occurances of x in list
22my_list.reverse() # reverse elements of list in-place (no return)
23my_list.sort(key=None, reverse=False) # sort list in-place
24my_list.copy() # return a shallow copy of the list
1myfavouritefoods = ["Pizza", "burgers" , "chocolate"]
2print(myfavouritefoods[1])
3#or
4print(myfavouritefoods)
1fruits = ['apple', 'banana', 'mango', 'cherry']
2
3for fruit in fruits:
4 print(fruit)
1#Creating your lists
2my_list = ['foo', 4, 5, 'bar', 0.4]
3my_nested_list = ['foobar', ['baz', 'qux'], [0]]
4
5#Accessing list values
6my_list[2] # 5
7my_list[-1] # 0.4
8my_list[:2] # ['foo', 4, 5]
9my_nested_list[2] # ['baz', 'quz']
10my_nested_list[-1] # [0]
11my_nested_list[1][1] # 'qux'
12
13#Modifying lists
14my_list.append(x) # append x to end of list
15my_list.extend(iterable) # append all elements of iterable to list
16my_list.insert(i, x) # insert x at index i
17my_list.remove(x) # remove first occurance of x from list
18my_list.pop([i]) # pop element at index i (defaults to end of list)
19my_list.clear() # delete all elements from the list
20my_list.index(x[, start[, end]]) # return index of element x
21my_list.count(x) # return number of occurances of x in list
22my_list.reverse() # reverse elements of list in-place (no return)
23my_list.sort(key=None, reverse=False) # sort list in-place
24my_list.copy() # return a shallow copy of the list