Download our e-book of Introduction To Python
Pallavi Dhotre
2 years ago
demo_list=[] #empty list
demo_list=[1,2,3,4] #list with integer values
demo_list=['Ram','Sham','Siya'] #list of strings
demo_list=['Ram','Sham','Siya'] #list of strings
print(demo_list[0]) #this will access the first element
print(demo_list[1]) #this will access the second element
print(demo_list[2]) #this will access the third element
Ram
Sham
Siya
marks = [] # define an empty list
for i in range(5):
a = int(input("Enter the marks:")) #accepting marks for 5 times
marks.append(a) #python append list,using append() function to push the element into the empty list
print(marks) #display list
Enter the marks:45
Enter the marks:55
Enter the marks:66
Enter the marks:77
Enter the marks:88
[45, 55, 66, 77, 88]
['sub1', 'sub2', 'sub3']
['sub2', 'sub3']
demo=['sub1','sub2','sub3'] #list
print(demo) #display list before removing the element
del demo[1] #remove second element
print(demo) #list after removing element
['sub1', 'sub2', 'sub3']
['sub1', 'sub3']
demo=['sub1','sub2','sub3','sub1','sub2'] #list
print(demo) #display list before removing the element
print(demo.count('sub1')) #this will display the number of occurrences of the specific items
['sub1', 'sub2', 'sub3', 'sub1', 'sub2']
2
a=['Ana','John','Nick'] #first list
b=['Harry','Peter'] #second list
a.extend(b) #combining two python lists using extend()
print(a) #display final list
['Ana', 'John', 'Nick', 'Harry', 'Peter']
a=['Ana','John','Nick'] #first list
a.insert(3,'Neha') #inserting element at index 3 in list
print(a) #display list
['Ana', 'John', 'Nick', 'Neha']
a=['Ana','John','Nick'] #first list
print(a.pop()) #removes the last element
print(a) #display list
Nick
['Ana', 'John']
a=[10,20,55,44,34,9,6] #first list
a.sort() #sorts the given list
print(a) #display list
[6, 9, 10, 20, 34, 44, 55]
a=[10,20,55,44,34,9,6] #first list
b=['hi'] # second list
print(a+b) #python lists concatenation
print(b*4) #python lists replication
[10, 20, 55, 44, 34, 9, 6, 'hi']
['hi', 'hi', 'hi', 'hi']
a=[10,20,55,44,34,9,6] #first list
print(len(a))#display the length of the list
7
#convert list to string python program
demo=['inside','AI','ML']
str='' #empty string
for ele in demo: #iterating through the list
str += ele #converting the list elements to string
print(str) #display string
insideAIML