Professional Documents
Culture Documents
# Display of List with elements.
list = [10, 20,30]
print (list)
Output
[10, 20, 30]
list = [10, 20,30, 40,50,60,70,80,90,100]
print (list)
Output
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
Output
[20, 30, 40, 50, 60, 70, 80, 90, 100]
list.pop(0)
print (list)
Output
[30, 40, 50, 60, 70, 80, 90, 100]
#delete all elements from index 2
del list[2:]
print (list)
Output
[30, 40]
list = [10, 20,30, 40,50,60,70,80,90,100]
print (list)
del list[2:4]
print (list)
Output
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
[10, 20, 50, 60, 70, 80, 90, 100]
del list[:4]
print (list)
Output
[70, 80, 90, 100]
#Display both string and numbers in same list 23 elements (Heterogeneous elements)
list = [10,15, 20,25,30, 35,40,45,50,55,60,65,70,75,80,85,90,95,100,'x','y','z', "MCA"]
print (list)
Output
[10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100, 'x', 'y', 'z', 'MCA']
list = [10,15, 20,25,30, 35,40,45,50,55,60,65,70,75,80,85,90,95,100,'x','y','z', "MCA"]
print (list)
del list[-20:-4]
print (list)
print (list[-1]) #negative indexing
print (list[-4])
Output
[10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100, 'x', 'y', 'z', 'MCA']
[10, 15, 20, 'x', 'y', 'z', 'MCA']
MCA
x
#Finding the range of the Lists.
n=range(5)
for i in n:
print(i)
Output
0
1
2
3
4
for i in range(10):
print(i)
Output
0
1
2
3
4
5
6
7
8
9
#to print even numbers
for i in range(0,20,2): #range(start, stop[ , step])
print("the value of i is",i)
Output
the value of i is 0
the value of i is 2
the value of i is 4
the value of i is 6
the value of i is 8
the value of i is 10
the value of i is 12
the value of i is 14
the value of i is 16
the value of i is 18
#to print odd numbers
for i in range(1,20,2):
print("the value of i is",i)
Output
the value of i is 1
the value of i is 3
the value of i is 5
the value of i is 7
the value of i is 9
the value of i is 11
the value of i is 13
the value of i is 15
the value of i is 17
the value of i is 19
#to print negative even numbers
for i in range(0,-20,-2):
print("the value of i is",i)
Output
the value of i is 0
the value of i is -2
the value of i is -4
the value of i is -6
the value of i is -8
the value of i is -10
the value of i is -12
the value of i is -14
the value of i is -16
the value of i is -18
#looping in list
list= [ ]
n= int(input("Enter the limit")) #how many nos. to enter in a list
for i in range(n):
a= input("Enter the values")
print(list)
list.append(a)
print (list)
Output
Enter the limit 5
Enter the values 1
Enter the values a
Enter the values 2
Enter the values b
Enter the values 3
['1', 'a', '2', 'b', '3']
#square of a number looping in list
list= [ ]
n= int(input("Enter the limit:")) #how many nos. to enter in a list
for i in range(n):
a= int(input("Enter the numbers:"))
list.append(a ** 2)
print ("The squared numbers are",list)
Output
Enter the limit:5
Enter the numbers:10
Enter the numbers:20
Enter the numbers:30
Enter the numbers:40
Enter the numbers:50
The squared numbers are [100, 400, 900, 1600, 2500]
#odd and even numbers using loop
n=int(input("enter a number"))
a= [i ** 2 for i in range(0,n,2)]
print ("the even squared numbers are:",a)
b=[i ** 2 for i in range(1,n,2)]
print ("the odd squared values are:",b)
Output
enter a number20
the even squared numbers are: [0, 4, 16, 36, 64, 100, 144, 196, 256, 324]
the odd squared values are: [1, 9, 25, 49, 81, 121, 169, 225, 289, 361]
string=input(("Enter a string:"))
if(string==string[::-1]):
print("The string is a palindrome")
else:
print("Not a palindrome")
Output
Enter a string: madam
The string is a palindrome
#Various methods of list
#append
fruits = ["apple", "banana", "cherry"]
print(fruits)
fruits.append("orange")
print("After append fn",fruits)
Output
['apple', 'banana', 'cherry']
After append fn ['apple', 'banana', 'cherry', 'orange']
#Clear()
fruits.clear()
print(fruits)
Output
[]
# append()
fruits.append("orange")
print(fruits)
Output
['orange']
fruits = ["apple", "banana", "cherry"]
fruits.append("orange")
print(fruits)
Output
['apple', 'banana', 'cherry', 'orange']
# copy()
x = fruits.copy()
print(x)
# count()
print (fruits)
x = fruits.count("cherry")
print("the count of cherry is",x)
fruits.append("orange")
print(fruits)
x = fruits.count("orange")
print("the count of orange is",x)
Output
['apple', 'banana', 'cherry', 'orange']
['apple', 'banana', 'cherry', 'orange']
the count of cherry is 1
['apple', 'banana', 'cherry', 'orange', 'orange']
the count of orange is 2
# extend( )
fruits = ["apple", "banana", "cherry"]
veg = ["carrots", "radish", "potato"]
fruits.extend(veg)
print ( fruits)
Output
['apple', 'banana', 'cherry', 'carrots', 'radish', 'potato']
#index()
y= fruits.index("potato")
print (y)
Output
5
fruits.insert(0, "mushroom")
print(fruits)
Output
['mushroom', 'apple', 'banana', 'cherry', 'carrots', 'radish', 'potato']
# pop ()
fruits.pop(0)
print(fruits)
Output
['apple', 'banana', 'cherry', 'carrots', 'radish', 'potato']
z=fruits.pop(4)
print(z)
print(fruits)
radish
Output
['apple', 'banana', 'cherry', 'carrots', 'potato']
# insert()
fruits.insert(0, "mushroom")
print(fruits)
fruits.remove("mushroom")
print(fruits)
Output
['mushroom', 'apple', 'banana', 'cherry', 'carrots', 'potato']
# reverse()
fruits.reverse()
print(fruits)
Output
['potato', 'carrots', 'cherry', 'banana', 'apple']
# sort()
fruits.sort()
print(fruits)
Output
['apple', 'banana', 'carrots', 'cherry', 'potato']
# sort()
num=[10,6,9,6,3,5,1,10,45,56]
num.sort()
print(num)
Output
[1, 3, 5, 6, 6, 9, 10, 10, 45, 56]