Professional Documents
Culture Documents
return
for i in range(n):
print()
def print_reverse(arr):
def copy_array(arr):
# Performing operations
print_first_n(arr, n_elements)
print_reverse(arr)
copy_array(arr)
matrix = []
for i in range(rows):
row = []
for j in range(cols):
row.append(entry)
matrix.append(row)
return matrix
result_matrix = []
for i in range(len(matrix_a)):
row = []
for j in range(len(matrix_a[0])):
row.append(matrix_a[i][j] + matrix_b[i][j])
result_matrix.append(row)
return result_matrix
if p == m and n == q:
matrix_a = input_matrix(m, n)
matrix_b = input_matrix(p, q)
print(f"Matrix a = {matrix_a}")
print(f"Matrix b = {matrix_b}")
else:
# Write Code
34. Write a Python program to find the sum of N natural numbers using user-defined
functions.
def sum_of_natural_numbers(n):
if n <= 0:
return
sum_n = (n * (n + 1)) // 2
sum_of_natural_numbers(num)
35. Write a Python program that has a function that takes a list and returns a sorted list.
def sort_list(input_list):
sorted_list = sorted(input_list)
return sorted_list
sorted_result = sort_list(input_list)
except ValueError:
36. Write a python program to print factorial of given number using a Function.
def fact(x):
if x == 0:
return 1
else:
return x * fact(x - 1)
try:
x = int(input("x: "))
result = fact(x)
print(result)
except ValueError:
def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n-1)
# Input from the user
if num < 0:
elif num == 0:
else:
38. Write a python program to print fibonacci series up to given range using functions
without using recursion
def fibonacci_series(n):
a, b = 0, 1
while a <= n:
print(a)
a, b = b, a + b
fibonacci_series(range_limit)
if n <= 0:
return []
elif n == 1:
return [0]
elif n == 2:
return [0, 1]
else:
fib_series = fibonacci_series(n - 1)
fib_series.append(fib_series[-1] + fib_series[-2])
return fib_series
max_limit = int(input("Enter the maximum limit to generate the fibonacci series: "))
result = fibonacci_series(max_limit)
print(num)
40. Write a program that takes a list of age of members from user and find the
members eligible for voting with age grater than 17 using filter() function with
lambda.
# Input from the user
# Using filter() with lambda to find members eligible for voting (age greater than 17)
41. Write a program that takes a list animal names in lower case and changes those strings to
upper case strings using map() function with lambda.
# Taking input from the user and converting it to a list of lowercase animal names
animal_names = input("Enter animal names: ").split()
42. Write a program that takes a list of integer from user and find the maximum element
using reduce() function with lambda.
Given two integer inputs num1 and num2. Perform division operation
on num1 and num2 using try-except block.
try:
print(num1/num2)
except:
print("exception occurred")
44. Let's write a program with a try block and two except blocks that handles two
types of exceptions. Follow the below-given instructions and write the missing code
in the provided editor.
In the given try block take two integer inputs a and b. Perform the arithmetic
operations +, / and * on a, b and store the results in c, d and e.
Print try successful.
Open an except block with name ZeroDivisionError.
Print zero division error occurred.
Open an except block with name NameError.
Print name error occurred.
Open an except block with name Exception.
Print exception occurred.
try:
a=int(input("a: "))
b=int(input("b: "))
c=a+b
d=a/b
e=a*b
if a == 0:
f = fun1(a)
print("try successful")
except ZeroDivisionError:
except NameError :
except Exception :
print("exception occurred")
def fun1(n):
print(n)
46. Write a python program to create a new file and add the given text into the file.
file_name = "output_file.txt"
file.write(text_to_write)
file_contents = file.read()
print(file_contents)
47.write a python program to add the contents into an existing file and display the contents
of a file from the specified position.
def write_content(file_name, content):
file.write(content)
if position < 0:
print("Invalid position")
return
file.seek(position)
content = file.read()
return content
file_name = "myfile.txt"
write_content(file_name, content)
print(result)
48.write a Python program to find the product of two given numbers, implementing
the following logic:
Consider an example of 14 x 20
def find_product(num1, num2):
# Displaying the two numbers
#print(num1)
#print(num2)
# Initializing variables
sum_result = 0
if num1 % 2 == 0:
print(num2)
sum_result += 0
num1 = num1 // 2
num2 = num2 * 2
else:
print("+" + str(num2))
sum_result += num2
num1 = num1 // 2
num2 = num2 * 2
print("stop")
num1 = int(input())
num2 = int(input())
find_product(num1, num2)