UNIT 2_Solution_Python programming _QUESTION BANK_2023-24
UNIT 2_Solution_Python programming _QUESTION BANK_2023-24
UNIT-2
QUESTION BANK SOLUTIONS
1. 'Elif' stands for 'else if' and is used in Python programming to test multiple conditions. It is
written following an if statement in Python to check an alternative condition if the first
condition is false. The code block under the elif statement will be executed only if its
condition is true. ELSE IF is more efficient because the computer only has to check
conditions until it finds a condition that returns the value TRUE. By using multiple IF-
conditions the computer has to go through each and every condition and thus multiple IF-
conditions require more time.
2. Syntax:
for iterator_var in range ():
statements(s)
for iterator_var in range():
statements(s)
else:
statements(s)
Iteration of for loop in sequence
for iterator_var in sequence:
statements(s)
3.
4. Continue is used to skip the ongoing iteration and continues the remaining iterations of
loop. Use of continue statement is to jump to the next iteration by skipping the existing
one.
5. The Python pass statement is a null statement. But the difference between pass
and comment is that comment is ignored by the interpreter whereas pass is not ignored.
If we do not use pass or simply enter a comment or a blank here, we will receive
an IndentationError error message.
Here are a few scenarios where you might use the pass statement in loops:
while condition:
if some_condition:
# No action needed in this case
pass
else:
# Perform some action
do_something()
6.
def calculate_product(numbers):
product = 1
for num in numbers:
product *= num
return product
if i%2==0:
print(i)
if i=10:
continue
else:
print(i, end=" ")
10.
Q11 Ans
while True:
user_input = input("Enter a number (or enter 42 to terminate): ")
Q12Ans
a.
lst=[ 1, 6, 3, 5, 3, 4 ]
#checking if element 7 is present
# in the given list or not
i=7
# if element present then return
# exist otherwise not exist
if i in lst:
print("exist")
else:
print("not exist")
b.
# python program check a list contain specific value. using for else loop
def check_value_in_list(my_list, target_value):
for element in my_list:
if element == target_value:
print(f"{target_value} found in the list.")
break
else:
print(f"{target_value} not found in the list.")
# Example usage:
my_list = [1, 2, 3, 4, 5]
target_value = int(input("Enter number to found"))
check_value_in_list(my_list, target_value)
Q13Ans
#take user input
String = input('Enter the string :')
count = 0
#to check for less conditions
#keep string in lowercase
String = String.lower()
for i in String:
if i == 'a' or i == 'e' or i == 'i' or i == 'o' or i == 'u':
#if True
count+=1
#check if any vowel found
if count == 0:
print('No vowels found')
else:
print('Total vowels are :' + str(count))
Q14Ans
# Python3 program to display Prime numbers till N
#function to check if a given number is prime
def isPrime(n):
#since 0 and 1 is not prime return false.
if(n==1 or n==0): return False
#Run a loop from 2 to n-1
for i in range(2,n):
#if the number is divisible by i, then n is not a prime number.
if(n%i==0):
return False
#otherwise, n is prime number.
return True
# Driver code
N = 100;
#check for every number from 1 to N
for i in range(1,N+1):
#check if current number is prime
if(isPrime(i)):
print(i,end=" ")
Q15 Ans
# Define a function named 'prime_eratosthenes' that generates prime numbers using the Sieve
of Eratosthenes algorithm
def prime_eratosthenes(n):
prime_list = [] # Create an empty list to store prime numbers
# Iterate through the numbers from 2 to 'n'
for i in range(2, n+1):
if i not in prime_list:
# If 'i' is not in the 'prime_list,' it's a prime number; print it
print(i)
Q16 Ans
# Program to display the Fibonacci sequence up to n-th term
nterms = int(input("How many terms? "))
# first two terms
n1, n2 = 0, 1
count = 0
# check if the number of terms is valid
if nterms <= 0:
print("Please enter a positive integer")
# if there is only one term, return n1
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
# generate fibonacci sequence
else:
print("Fibonacci sequence:")
while count < nterms:
print(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1
Q17 Ans.
#Print right angle triangle in Python
x=int(input("Enter row number=\n"))
for i in range(x):
for j in range(i+1):
print("*",end='')
print("")
Q18 Ans.
# 1-22-333-4444 Pattern up to n lines
n = int(input("Enter number of rows: "))
for i in range(1,n+1):
for j in range(1, i+1):
print(i, end="")
print()
Q 19 Ans.
# Print Pascal's Triangle in Python
from math import factorial
# input n
n=5
for i in range(n):
for j in range(n-i+1):
for j in range(i+1):
# nCr = n!/((n-r)!*r!)
print(factorial(i)//(factorial(j)*factorial(i-j)), end=" ")