xxxxxxxxxx
myfavouritefoods = ["Pizza", "burgers" , "chocolate"]
print(myfavouritefoods[1])
#or
print(myfavouritefoods)
xxxxxxxxxx
# example of list in python
myList = [9, 'hello', 2, 'python']
print(myList[0]) # output --> 9
print(myList[-3]) # output --> hello
print(myList[:3]) # output --> [9, 'hello', 2]
print(myList) # output --> [9, 'hello', 2, 'python']
xxxxxxxxxx
list = ['apple', 4, 'banana', 'bat', 0.44]
print(list)
#it will print all items in the list
xxxxxxxxxx
[1, 'hi', 'Python', 2]
[2]
[1, 'hi']
[1, 'hi', 'Python', 2, 1, 'hi', 'Python', 2]
[1, 'hi', 'Python', 2, 1, 'hi', 'Python', 2, 1, 'hi', 'Python', 2]
xxxxxxxxxx
myList = ["Test", 419]
myList.append(10)
myList.append("my code")
print(myList)
xxxxxxxxxx
# List = Flexible, mutable, can store hybrid data types together
some_list = [1,True, "Yes", None]
some_list[0] # First item
some_list[-1] # Last item
some_list[:] # All items
some_list[1:] # second to the last
some_list[:-1] # from first up to the last (excluding last item)
some_list[::2] # Every n-th element in the list
xxxxxxxxxx
fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']
fruits.count('apple') # count number of apples found in list
# output 2
fruits.count('tangerine') # count number of tangerines in list
# output 0
fruits.index('banana') # find the first index of banana
# output 3
fruits.index('banana', 4) # Find next banana starting a position 4
# output 6
fruits.reverse() # reverse fruits array
fruits
# output ['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange']
fruits.append('grape') # append grape at the end of array
fruits
# output ['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange', 'grape']
fruits.sort()
fruits
# output ['apple', 'apple', 'banana', 'banana', 'grape', 'kiwi', 'orange', 'pear']
len(fruits) # length of fruits array
# output 8
# loop and print each fruit
for fruit in fruits:
print(fruit)