logo codesdope
Level 1
1.
Take 10 integer inputs from user and store them in a list and print them on screen.
Answer
i = 10
a = []
while i>0:
  print "Enter number"
  num = input()
  a.append(num)
  i = i-1
print a
									

2.
Take 10 integer inputs from user and store them in a list. Again ask user to give a number. Now, tell user whether that number is present in list or not.
( Iterate over list using while loop ).
Answer
i = 10
a = []
while i>0:
  print "Enter number"
  num = input()
  a.append(num)
  i = i-1
print "Enter a number"
n = input()
i = 9
count = 0
while i>=0:
  if n == a[i]:
    print "Yes"
    count = count + 1
  i = i-1
if count == 0:
  print "No"

#python way, without loop
if n in a:
  print "Yes"
else:
  print "No"
									

3.
Take 20 integer inputs from user and print the following:
number of positive numbers
number of negative numbers
number of odd numbers
number of even numbers
number of 0s.
Answer
i = 20
a = []
while i>0:
  print "Enter number"
  num = input()
  a.append(num)
  i = i-1
odd = 0
even = 0
zero = 0
positive = 0
negative = 0
i = 19
while i>=0:
  if a[i] == 0:
    zero = zero+1
  elif a[i]>0:
    positive = positive + 1
    if a[i]%2 == 0:
      even = even + 1
    else:
      odd = odd + 1
  else:
    negative = negative + 1
    if a[i]%2 == 0:
      even = even + 1
    else:
      odd = odd + 1
  i = i-1
print "EVEN :",even,"ODD :",odd,"ZERO :",zero,"POSITIVE :",positive,"NEGATIVE :",negative

									

4.
Take 10 integer inputs from user and store them in a list. Now, copy all the elements in another list but in reverse order.
Answer
i = 10
a = []
while i>0:
  print "Enter number"
  num = input()
  a.append(num)
  i = i-1
# reverse elements of a
a.reverse()
#copied to new list
b = a
#again back to initial order
a.reverse()
									

5.
Write a program to find the sum of all elements of a list.

6.
Write a program to find the product of all elements of a list.

7.
Initialize and print each element in new line of a list inside list.
Answer
a=[[1,2,3],[4,5,6]]
i = 0
while i<len(a):
  j = 0
  while j < len(a[i]):
    print a[i][j]
    j = j+1
  i = i+1
									

8.
Find largest and smallest elements of a list.
Answer
a = [2,312,123,3,12,23,12,12]
largest = a[0]
i = 0
while i<len(a):
  if a[i]>largest:
    largest = a[i]
  i = i+1
print largest
#similarly find smallest									

9.
Write a program to print sum, average of all numbers, smallest and largest element of a list.

10.
Write a program to check if elements of a list are same or not it read from front or back. E.g.-
2 3 15 15 3 2
Answer
# You can take any list
a = [1,2,3,3,2,1]
i = 0
mid = (len(a))/2
same = True
while i<mid:
  if a[i] != a[len(a)-i-1]:
    print "No"
    same = False
    break
  i = i+1
if same == True:
  print "Yes"

									

11.
Make a list by taking 10 input from user. Now delete all repeated elements of the list.
E.g.-
INPUT : [1,2,3,2,1,3,12,12,32]
OUTPUT : [1,2,3,12,32]
Answer
a = [1,2,3,2,1,3,12,12,32]
i = 0
while i < len(a):
  j = i+1
  while j < len(a):
    if a[i] == a[j]:
      del(a[j])
    j=j+1
  i = i+1
print a

#python way
a = [1,2,3,2,1,3,12,12,32]
a = list(set(a))
print a

									

12.
Take a list of 10 elements. Split it into middle and store the elements in two dfferent lists. E.g.-
INITIAL list :
58 24 13 15 63 9 8 81 1 78

After spliting :
58 24 13 15 63
9 8 81 1 78
Answer
a = [58,24,13,15,63,9,8,81,1,78]
print a[:len(a)/2]
print a[len(a)/2:]

									

13.
Ask user to give integer inputs to make a list. Store only even values given and print the list.
Answer
#Hint
if num%2 == 0:
  a.append(num)

									
Level 2
1.
Take a list of length n where all the numbers are non-negative and unique. Find the element in the list possessing the highest value. Split the element into two parts where first part contains the next highest value in the list and second part hold the required additive entity to get the highest value. Print the list where the highest value get splitted into those two parts.
Sample input: 4 8 6 3 2
Sample output: 4 6 2 6 3 2
Answer
a = [4,8,6,3,2]
i = 0
largest = a[0]
while i<len(a):
  if largest < a[i]:
    largest = a[i]
    largest_index = i
  i = i+1
print largest_index
sec_largest =  a[0]
i = 0
while i<len(a):
  if largest != a[i] and sec_largest < a[i]:
    sec_largest = a[i]
  i = i+1
diff = largest - sec_largest
print a[:largest_index]+[sec_largest,diff]+a[largest_index+1:]


									

2.
Write a program to shift every element of a list to circularly right. E.g.-
INPUT : 1 2 3 4 5
OUTPUT : 5 1 2 3 4
Answer
a = [1,2,3,4,5]
b = [a[len(a)-1]]+a[:len(a)-1]
print b
a = b
print a


									

3.
Write a program to add and multiply two 3x3 matrices.
Doubt? Ask question
Close

Welcome.please sign up.

Close

Welcome.please login.