Programming Questionsvv
Programming Questionsvv
A=[0,1,2,3]
for A[2] in A:
print(A[2], end="#")
Output:
0#1#1#3#
L = [None] * 10
print(len(L))
Output:10
Programming Questions
Program 1: Write a program to calculate the sum of two numbers and print their sum.
num1 = 1.5
num2 = 6.3
sum = num1 + num2
print(sum)
Output :
7.8
Program 2: Write a program that accepts the radius of a circle and prints its area.
radius = float(input("Input the radius of the circle : "))
area = (22/7)*radius*radius
print (area)
Output :
Input the radius of the circle : 7.0
154.0
138
Program 3:Write a program to compute simple interest.
P = float(input("Enter Principal: "))
R = float(input("Enter Rate of Interest (in % per annum) : "))
T = float(input("Enter Time Period : "))
Output :
Enter Principal: 1000
Enter Rate of Interest (in % per annum) : 20
Enter Time Period : 3
600.0
Program 4: Write a program to take sides of a triangle as input and print its area.
a = float(input("Enter first side: "))
b = float(input("Enter second side: "))
c = float(input("Enter third side: "))
Output :
Enter first side: 5
Enter second side: 6
Enter third side: 7
14.696938456699069
139
else:
print("Odd Number")
Output :
Enter a number: 95
Odd Number
Output :
Enter a number to find factorial: 6
The factorial of is 720
Program 7: Write a program to display the Fibonacci sequence up to n-th term where n is provided
by the user
first = 0
second = 1
counter = 0
number_of_terms = int(input("Enter Number of terms : "))
if number_of_terms <= 0:
print("Please enter a positive integer")
else:
while counter < number_of_terms:
print(first, end = " ")
temp = first + second
first = second
second = temp
counter = counter + 1
Output :
Enter Number of terms : 10
0 1 1 2 3 5 8 13 21 34
140
Program 8: Write a program in Python to check if a number entered by a user is a prime number or
not.
number = int(input("Enter a positive number greater than one : "))
flag = 0
if number > 1:
for i in range(2,number):
if ((number%i) == 0):
flag = 1
break
if (flag==1):
print("Not prime")
else:
print("Prime")
Output :
Enter a positive number greater than one : 79
Prime
Program 9: Write a Program to check if the input year is a leap year or not
year = int(input("Enter a year : "))
if(((year % 4 == 0) and (year % 100 != 0)) or (year % 400 == 0)):
print("Leap Year")
else:
print("Non Leap Year")
Output :
Enter a year : 2100
Non Leap Year
141
Output :
[2, 4, 10, 20, 5]
Program 11: Write a Python program to count the number of strings of length 2 or more and the first
and last character are same from a given list of strings
Output :
2
Program 12: Write a Python program to find the sum of all even numbers and all odd numbers from
the given tuple.
Output:
Sum of Even Numbers is = 24
Sum of Odd Numbers is = 27
142
Program 13: Write a program in Python, To input a list of numbers. Thereafter increments all even
numbers by 1 and decrements all odd numbers by 1.
Output:
Original List = [8, 10, 13, 5, 6, 9]
Updated List = [9, 11, 12, 4, 7, 8]
Program 14: Write a program in python, to input a dictionary Emp which contains eid and ename
as key value pair and displays the name in uppercase of the values whose names are longer than 5
characters.
For example:
Emp = {101: 'Rahul', 102: 'Hardik', 103: 'Virat', 104:'Jaspreet'}
The output should be:
Hardik
Jaspreet
Emp = {}
c='y'
while c in 'yY':
eid = int(input("Enter the employee id: "))
ename = input("Enter the name of employee: ")
Emp[eid] = ename
c = input("Do you want to add more elements(y/n): ")
print("Dictionary is : ", Emp)
print("Employee name longer than 5 characters: ")
for name in Emp.values():
143
if len(name)>5:
print(name.upper())
Output:
Output :
Year . 0. at All the best
Question :
Match the following: -
144
1) Returns True if all characters in the string are (a). count()
digits
5) Removes the first item with the specified value (e). split()
(f). pop()
(g). remove()
Answer:
1 - D, 2 - A, 3 - E, 4 - B, 5 - G
145
Output :
(22, 44, 66)
N={'A':'B','C':'D'}
for k,v in N.items():
print(k,v)
Output :
AB
CD
Answer : A. Both A and R are true and R is the correct explanation for A
146
Reason (R): in operator in Python is used searching for specific elements within sequences like
lists and strings etc.
A. Both A and R are true and R is the correct explanation for A
B. Both A and R are true and R is not the correct explanation for A
C. A is True but R is False
D. A is False but R is True
Answer : A. Both A and R are true and R is the correct explanation for A
4. Assertion (A): Modules in Python are reusable pieces of code that can be imported into other
Python scripts.
Reason (R): import statement must be the first line of the program
5. Assertion (A): pop() used to delete the last element from the dictionary.
Reason (R): popitem() used to delete the specific element from the dictionary.
147
Reason (R): items() method is used to display the keys and also the values of the dictionary.
7. Assertion (A): L.len() statement used to return the total number of elements in the list.
Reason (R): count() method is used to find the total number of elements in the list.
148