xxxxxxxxxx
#Lists are used to store multiple items in a single variable.
#List is a collection which is ordered and changeable. Allows duplicate members.
list1 = ["apple", "banana", "cherry"]
list2 = [1, 5, 7, 9, 3]
list3 = [True, False, False]
# A List can contain differnet data type
mixed = ["abc", 34, True, 40, "male"]
zeros = [0] * 5 # Output: [0, 0, 0, 0, 0]
combined = zeros + list1
# Output: [0, 0, 0, 0, 0, 'apple', 'banana', 'cherry']
numbers = list(range(5)) #output: [0, 1, 2, 3, 4]
chars = list("Hello World!")
# Output: ['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!']
# A list can have multiple list
matrix = [[0,1], [2,3]]
# A list can have multiple type of list
multiple_list = [[2,3], ["a", "b"]]
# The list() Constructor
# note the double round-brackets
list_constructor = list(("apple", "banana", "cherry"))
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
myfavouritefoods = ["Pizza", "burgers" , "chocolate"]
print(myfavouritefoods[1])
#or
print(myfavouritefoods)
xxxxxxxxxx
mylist = [] # creating an empty list
# appending values entered by user to list
for i in range(5):
print("Enter value of n[", i, "]")
mylist.append(input())
# printing final list
print(mylist)
xxxxxxxxxx
fruits = ['apple', 'banana', 'mango', 'cherry']
for fruit in fruits:
print(fruit)
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)