Extra Questions

Q3 d. Given a list of integers L, write code to calculate and display the sum of all even numbers in the list. Also display how many numbers in the list ends with 3.

user_input=int(input('Please enter length of the list: '))
numList=[]
sum=0
count=0
print('\n')

for x in range(user_input):
 numList.append(str(input('enter list elements: ')))
 if (int(numList[x])%2)==0:
  sum=int(numList[x])+sum
 if numList[x][len(numList[x])-1]=='3':
  count=count+1

 
print('\nSum of all the even integers: ',sum) 
print('Numbers ending in 3 : ',count)       
Q3 e. Write a python code to generate and print a dictionary that contains a number (between 1 and n) in the form (x:x*x)
my_Dictionary={}

index=int(input('enter a number betwen 1 and n: '))

for x in range(1,index):
 key=x
 value=x*x
 my_Dictionary[key]=value

print(my_Dictionary)       

Comments

Popular posts from this blog

Chapter 05 - File Handling

Dictionary (Python)

Tuple (Python)