0% found this document useful (0 votes)
12 views9 pages

UNIT 2_Solution_Python programming _QUESTION BANK_2023-24

Uploaded by

uditchaudhary140
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
12 views9 pages

UNIT 2_Solution_Python programming _QUESTION BANK_2023-24

Uploaded by

uditchaudhary140
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 9

Python programming (BCC302)

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:

for item in collection:


# TODO: Implement functionality here
Pass

while condition:
if some_condition:
# No action needed in this case
pass
else:
# Perform some action
do_something()
6.

7. total = 0 #defined total


for number in range(1,101):
if number %2 == 0:
total = sum(int(number))
print(total)
8.

def calculate_product(numbers):
product = 1
for num in numbers:
product *= num
return product

# Get input from the user


input_str = input("Enter a list of numbers separated by spaces: ")
numbers_list = list(map(float, input_str.split()))

# Calculate product using the function


result = calculate_product(numbers_list)
print("The product of the numbers is:", result)
9. for i in range(1, 21):

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): ")

# Check if the user entered 42


if user_input == "42":
print("Terminating the loop. You entered 42.")
break
This program continuously prompts the user to enter a number. It keeps running until the
user enters the number 42. Once the user inputs 42, the program prints a message and
breaks out of the loop, ending the program

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)

# Mark all multiples of 'i' as non-prime by adding them to 'prime_list'


for j in range(i*i, n+1, i):
prime_list.append(j)
# Call the 'prime_eratosthenes' function with 'n' set to 100 to generate prime numbers
# The function does not have a return value, so it prints the prime numbers directly
n=int(input("Enter the number"))
prime_eratosthenes(n)

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

# Python Program for n-th Fibonacci number Using recursion


# Function for nth Fibonacci number
def Fibonacci(n):
if n<= 0:
print("Incorrect input")
# First Fibonacci number is 0
elif n == 1:
return 0
# Second Fibonacci number is 1
elif n == 2:
return 1
else:
return Fibonacci(n-1)+Fibonacci(n-2)
# Driver Program
print(Fibonacci(10))

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 left spacing


print(end=" ")

for j in range(i+1):

# nCr = n!/((n-r)!*r!)
print(factorial(i)//(factorial(j)*factorial(i-j)), end=" ")

# for new line


print()
20 Ans.
# Reading number of rows
row = int(input('Enter how many lines? '))
# Generating pattern
for i in range(1,row+1):
# for space
for j in range(1, row+1-i):
print(' ', end='')
# for increasing pattern
for j in range(1,i+1):
print(j, end='')
# for decreasing pattern
for j in range(i-1,0,-1):
print(j, end='')
# Moving to next line
print()

You might also like