15. Write the output of the following Python code.
A=[0,1,2,3]
for A[2] in A:
print(A[2], end="#")
Output:
0#1#1#3#
16: Write the output of the following code:
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 : "))
# Calculates simple interest
SI = (P * R * T) / 100
print(SI)
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: "))
# calculate the semi-perimeter
s = (a+b+c)/2
# calculate the area
area = (s*(s-a)*(s-b)*(s-c))**0.5
print(area)
Output :
Enter first side: 5
Enter second side: 6
Enter third side: 7
14.696938456699069
Program 5: Write a program to find whether a given number is even or odd?
num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even Number")
139
else:
print("Odd Number")
Output :
Enter a number: 95
Odd Number
Program 6: Write a program to find the factorial of a number.
num = int(input("Enter a number to find factorial: "))
factorial = 1
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
else:
for i in range(2,num+1):
factorial = factorial*i
print("The factorial of is ",factorial)
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
Program 10: Write a Python program to remove duplicates from a list.
L = [2, 4, 10, 20, 5, 2, 20, 4]
F = []
for n in L:
if n not in F:
F.append(n)
print(F)
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
words = ['abc', 'xyz', 'aba', '1221']
count=0
for w in words:
if len(w) > 1 and w[0] == w[-1]:
count = count + 1
print(count)
Output :
2
Program 12: Write a Python program to find the sum of all even numbers and all odd numbers from
the given tuple.
T = (8, 10, 13, 5, 6,9)
L= len(T)
even=0
odd=0
for i in range(L):
if T[i]%2==0:
even=even + T[i]
else:
odd = odd + T[i]
print("Sum of Even Numbers is = ", even)
print("Sum of Odd Numbers is = ", odd)
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.
L = [8, 10, 13, 5, 6, 9]
print("Original List = ", L)
for i in range(len(L)):
if L[i]%2==0:
L[i]=L[i] + 1
else:
L[i] = L[i] - 1
print("Updated List = ", L)
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:
Enter the employee id: 101
Enter the name of employee: Rahul
Do you want to add more elements(y/n): y
Enter the employee id: 102
Enter the name of employee: Hardik
Do you want to add more elements(y/n): y
Enter the employee id: 103
Enter the name of employee: Virat
Do you want to add more elements(y/n): y
Enter the employee id: 104
Enter the name of employee: Jaspreet
Do you want to add more elements(y/n): n
Dictionary is : {101: 'Rahul', 102: 'Hardik', 103: 'Virat', 104: 'Jaspreet'}
Employee name longer than 5 characters:
HARDIK
JASPREET
Question : Write the output of the following code:
a = "Year 2022 at All the best"
a = a.split('2')
b = a[0] + ". " + a[1] + ". " + a[3]
print (b)
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
2) Returns the number of times a specified value (b). extend()
occurs in a string
3) Splits the string at the specified separator, and (c). append()
returns a list
4) Add the elements of a list (or any iterable), to (d). isdigit()
the end of the current list
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
Question: Write the output of the following code:
tuple1 = (11, 22, 33, 44, 55 ,66)
list1 =list(tuple1)
new_list = []
for i in list1:
if i%2==0:
new_list.append(i)
new_tuple = tuple(new_list)
print(new_tuple)
145
Output :
(22, 44, 66)
Question: Write the output of the following code:
N={'A':'B','C':'D'}
for k,v in N.items():
print(k,v)
Output :
AB
CD
Assertion and Reason Type Questions
1. Assertion (A): Strings in Python are immutable.
Reason (R): Once a string object is created, it cannot be changed.
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
2. Consider the code given below:
x=5
print(x==5)
A: Output of above python code is True.
R: '==' is logical Operator.
A. Only A is True.
B. Only R is True.
C. Both A and R are True, but R is not the correct reason for A.
D. Both A and R are True, but R is the correct reasoning for A.
Answer : A. Only A is true.
3. Assertion (A): The in operator in python is membership Operator.
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
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 is True but R is False
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.
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. Both A and R are False
Answer: Both A and R are False
6. Assertion (A): extend() method is used to merge two dictionaries.
147
Reason (R): items() method is used to display the keys and also the values of the dictionary.
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 is False but R is True
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.
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. Both A and R are False
Answer: Both A and R are False
--------------------------------------------------------------------------------------------------------------------
148