17
loading...
This website collects cookies to deliver better user experience
a=['a','b','1',2]
print(a[0])
print(a[1])
print(a[2])
print(a[3])
a
b
1
2
'1'
, 'b'
and 'a'
is of the data type character while 2
is of the data type int.list starts with 0. The first element of the list. There is no fourth index. Now if we try to acess the non-existant fourth element, an error will arise.a=[1,2,3,4]
print(a[0])
print(a[1])
print(a[2])
print(a[3])
print(a[4])
1
2
3
4
Traceback (most recent call last):
File "main.py", line 6, in <module>
print(a[4])
IndexError: list index out of range
a=['a','b','c','1','2']
print(a[0:2]) #the : is the slicing
['a', 'b']
print()
a=['a','b','c','1','2']
print(a)
['a', 'b', 'c', '1', '2']
len()
functiona=['a','b','c','1','2']
print(len(a))
a=['a','b','c','1','2']
for i in range(0,len(a)):
print(a[i])
a
b
c
1
2
a=['a','b','c','1','2']
print(a)
a.append(2)
#add 2 at the end
a.append('c')
print(a.count('c'))
#here we are asking how many times the character c is in the list.
#so answer must be 2
print(a.count(2))
#here we are asking how many times int 2 is in the list
print(a.index('1'))
#returns the index at the first occurrence of the element
b=['z','e']
a.extend(b)
#Adds the entire list to a
print(a)
['a', 'b', 'c', '1', '2']
2
1
3
['a', 'b', 'c', '1', '2', 2, 'c', 'z', 'e']
a=['B','t','m','a','N','Bat']
print(a)
a.insert(1,'a')
#insert a at second position
a.insert(len(a)-1,'-')
#insert '-' at the second last position
print(a)
a.remove('Bat')
#remove first occurance of 'Bat'
print(a)
a.pop()
#removes the last value.
#Pop is opposite of append
#pop also returns the last value.
b=a.pop()
print(b)
#We can also give value to pop to remove the element.
print(a)
print(a.pop(3))
print(a)
['B', 't', 'm', 'a', 'N', 'Bat']
['B', 'a', 't', 'm', 'a', 'N', '-', 'Bat']
['B', 'a', 't', 'm', 'a', 'N', '-']
N
['B', 'a', 't', 'm', 'a']
m
['B', 'a', 't', 'a']
names=[] #Declaration of the list without value
print(len(names))
num=int(input("Enter number of Names to be stored in a list "))
for i in range(0,num):
st=input("Enter a name ")
names.append(st)
print("Values stored in List: ")
for i in range(0,num):
print(names[i])
0
Enter number of Names to be stored in a list 4
Enter a name B
Enter a name a
Enter a name t
Enter a name m
Values stored in List:
B
a
t
m
names=["Batman", "123", "Bat","163","1723"]
#1
print(names)
print("\n Frequency of 'Bat' in the list\n")
print(names.count("Bat"))
print("\n Index of '163' in the list\n")
print(names.index("163"))
print("Adding A in the list")
names.append("A")
print("No. of values in the list = ", len(names))
print(names)
print("New list as last names")
lastnames=["L","M","N"]
print(lastnames)
print("names.extend(lastnames)")
names.extend(lastnames)
print("New values in the names list")
print(names)
print("Inserting new names O, at index 4 ")
names.insert(4, "O")
print(names)
print("Removing A from the list ")
names.remove("A")
print(names)
print("pop() function removes the last value from the list")
names.pop()
print(names)
#names.remove(2) #error msg
print("pop() accepts index and string as an argument")
names.pop(3)
print(names)
print("Reverse of a string")
names.reverse()
print(names)
>>> names=["Batman", "123", "Bat","163","1723"]
#1
>>> print(names)
['Batman', 'Bat', '123', '163', '1723']
>>> names.count("Bat")
1
>>> names.index("163")
3
>>> names.append("A")
>>> len(names)
6
>>> print(names)
['Batman', 'Bat', '123', '163', '1723', 'A']
>>> lastnames=["L","M","N"]
>>> names.extend(lastnames)
>>> names
['Batman', 'Bat', '123', '163', '1723', 'A', 'L', 'M', 'N']
>>> names.insert(4, "O")
>>> names
['Batman', 'Bat', '123', '163', 'O', '1723', 'A', 'L', 'M', 'N']
>>> names.remove("A")
>>> names
['Batman', 'Bat', '123', '163', 'O', '1723', 'L', 'M', 'N']
>>> names.pop()
'N'
>>> names
['Batman', 'Bat', '123', '163', 'O', '1723', 'L', 'M']
>>> names.remove(2)
Traceback (most recent call last):
File "<pyshell#19>", line 1, in <module>
names.remove(2)
ValueError: list.remove(x): x not in list
>>> names.pop(3)
'163'
>>> names
['Batman', 'Bat', '123', 'O', '1723', 'L', 'M']
>>> names.reverse()
>>> names
['M', 'L', '1723', 'O', '123', 'Bat', 'Batman']
Please enter a number 5
Please enter a number 1
Please enter a number 2
Please enter a number 3
Please enter a number 4
Please enter a number 5
Smallest value= 0 Greatest value= 5