Python Programming Questions - Operation on List[SET-2]
1. Python program to find sum of elements in list
count = int(input(" Enter The Number of Elements Present In List : "))
myList = []
for i in range(count):
myList.append(int(input()))
addition = sum(myList)
print(" Sum of All Elements of List is : ",addition)
Enter The Number of Elements Present In List : 5
1
2
3
4
5
Sum of All Elements of List is : 15
2. Python Program To Multiply all numbers in the list
count = int(input(" Enter The Number of Elements Present In List : "))
myList = []
for i in range(count):
myList.append(int(input()))
multiplication = 1
for j in myList:
multiplication = multiplication * j
print(" Multiplication of All Elements of List is : ",multiplication)
Enter The Number of Elements Present In List : 5
1
2
3
4
5
Multiplication of All Elements of List is : 120
3. Python program to find smallest number in a list
count = int(input(" Enter The Number of Elements Present In List : "))
myList = []
for i in range(count):
myList.append(int(input()))
minimum = min(myList)
print(" Smallest Number in List : ",minimum)
Enter The Number of Elements Present In List : 5
85
75
80
82
65
Smallest Number in List : 65
4. Python program to find largest number in a list
count = int(input(" Enter The Number of Elements Present In List : "))
myList = []
for i in range(count):
myList.append(int(input()))
maximum = max(myList)
print(" Largest Number in List : ",maximum)
Enter The Number of Elements Present In List : 5
85
95
100
99
82
Largest Number in List : 100
5. Python program to find second largest number in a list
count = int(input(" Enter The Number of Elements Present In List : "))
myList = []
for i in range(count):
myList.append(int(input()))
myList.sort()
print(" Second Largest Number in List : ",myList[-2])
Enter The Number of Elements Present In List : 5
85
82
80
83
81
Second Largest Number in List : 83
6. Python program to find N largest elements from a list
myList = []
for i in range(count):
myList.append(int(input()))
nthValue = int(input(" Enter The Number of Largest Value Required : "))
large = []
for j in range(0,nthValue):
large.append(max(myList))
myList.remove(max(myList))
large.sort()
print(nthValue," Largest Value In Given List Are : ",large)
Enter The NUmber Of Elements Present In List : 5
60
78
59
85
69
Enter The Number of Largest Value Required : 3
3 Largest Value In Given List Are : [69, 78, 85]
7. Python program to print even numbers in a list
count = int(input(" Enter The Number Of Elements Present In List : "))
myList = []
for i in range(count):
myList.append(int(input()))
even = []
for j in range(0,len(myList)):
if(myList[j] % 2==0):
even.append(myList[j])
print(even)
Enter The Number Of Elements Present In List : 6
2
4
6
8
9
7
[2, 4, 6, 8]
8. Python program to print odd numbers in a list
count = int(input(" Enter The Number Of Elements Present In List : "))
myList = []
for i in range(count):
myList.append(int(input()))
odd = []
for j in range(0,len(myList)):
if(myList[j] % 2 != 0):
odd.append(myList[j])
print(odd)
Enter The Number Of Elements Present In List : 6
1
3
5
4
6
8
[1, 3, 5]
9. Python program to print all even numbers in a range
start = int(input(" Enter The Starting Range : "))
ends = int(input(" Enter The Ending Range : "))
myList = []
for i in range(start,ends):
if(i % 2 == 0):
myList.append(i)
print(myList)
Enter The Starting Range : 2
Enter The Ending Range : 50
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48]
10. Python program to print all odd numbers in a range
start = int(input(" Enter The Starting Range : "))
ends = int(input(" Enter The Ending Range : "))myList = []
for i in range(start,ends):
if(i % 2 != 0):
myList.append(i)
print(myList)
Enter The Starting Range : 1
Enter The Ending Range : 50
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49]
Comments
Post a Comment