Question Bank For Python
Question Bank For Python
QUESTION BANK
Semester- II (for both Major and MDC)
Paper Code: MATH-H-SEC2-2-Th
Paper Name: Python Programming and Introduction to Latex
# Example usage:
list1 = [1, 2, 3, 4, 5]
list2 = [5, 6, 7, 8, 9]
if have_common_member(list1, list2):
print("Lists have at least one common member")
else:
print("Lists do not have any common member")
##Calculations
14.Write a python program to open and write the content to file and read it.
Opening and Closing a Text File in Python
file1 = open("myfile1.txt","a")
file2 = open("myfile2.txt","w+")
file1.close()
file2.close()
15. Write a python program to check whether a given year is leap year or not and also print all the
months of the given year.
def CheckLeap(Year):
if((Year % 400 == 0) or
(Year % 100 != 0) and
(Year % 4 == 0)):
print("Given Year is a leap Year");
else:
print ("Given Year is not a leap Year")
Year = int(input("Enter the number: "))
CheckLeap(Year)
16. Solve
4x1+3x2-5x3=2
-2x1-4x2+5x3=5
8x1+8x2=-3
import numpy as np
A = np.array([[4, 3, -5],
[-2, -4, 5],
[8, 8, 0]])
y = np.array([2, 5, -3])
x = np.linalg.solve(A, y)
print(x)
17. Write a program to ask the user to enter names of their 3 favorite movies and store them in a list.
movies=[]
mov1=input(“enter first movie:”)
mov2=input(“enter second movie:”)
mov3=input(“enter third movie:”)
movies.append(mov1)
movies.append(mov2)
movies.append(mov3)
print(movies)
19.Write a program to count the members of students with the “A” grade in the following tuple
Grade=(“C”,”D”,”A”,”A”,”B”,”B”,”A”)
print(grade.count(“A”))
20. Store the above values in a list and sort them from “A” to “D”.
grade=(“C”,”D”,”A”,”A”,”B”,”B”,”A”)
grade.sort()
print(grade)
23. Write a program to enter marks of 3 subjects from the user and store them is a dictionary. Start with
an empty dictionary and add one by one. Use subject name as key and marks as values.
marks={}
x=int(int(input(“enter phy:”))
marks.update({“phy”:x})
x=int(int(input(“enter math:”))
marks.update({“math”:x})
x=int(int(input(“enter chem:”))
marks.update({“chem”:x})
print(marks)
25. Write a program to find the greatest of 3 numbers entered by the users.
A=int(input(“Enter first number:”))
B=int(input(“Enter second number:”))
C=int(input(“Enter third number:”))
if(A>B and A>C):
print(“First Number is largest”, A)
elif(B>C)
print (“Second number is largest”,B)
else:
print(“Third number is largest”,C)
26. Write a program to find the largest of four numbers entered by the users.
27.Write a program to check if a number is multiple of 7 or not
x=int(input(“Enter number:”))
if(x%7==0):
print(“multiple of 7”)
else:
print(“NOT a multiple”)
29. Write a program to input side of a square and print its area.
side=float(input(“enter value of the side:”))
area=side*side
print(“Area of the square is :”,area)
30. Write a program to input 2 floating numbers and print their average.
val1=float(input(“enter first number:”))
val2=float(input(“enter second number:”))
avg=(val1+val2)/2
print(avg)
31. Write a program to input 2 int numbers a and b. print True if a is greater than or equal to b, if not
print False.
a=float(input(“Enter first number:”))
b=float(input(“Enter second number:”))
print(a>=b)
import math
def calculate_circle_area(radius):
return math.pi * radius ** 2
# Example usage
radius = float(input("Enter the radius of the circle: "))
area = calculate_circle_area(radius)
return True
# Example usage
start = 10
end = 50
print_primes_in_interval(start, end)
def is_prime(num):
if num <= 1:
return False
if num == 2:
return True # 2 is a prime number
if num % 2 == 0:
return False # Even numbers greater than 2 are not prime
return True
# Example usage
number = 29
if is_prime(number):
print(f"{number} is a prime number")
else:
print(f"{number} is not a prime number")
def fibonacci_iterative(n):
if n <= 0:
return None
elif n == 1:
return 0
elif n == 2:
return 1
return b
# Example usage
n = 10
fibonacci_number = fibonacci_iterative(n)
print(f"The {n}-th Fibonacci number is {fibonacci_number}")
def sum_of_squares(n):
sum_squares = 0
for i in range(1, n + 1):
sum_squares += i ** 2
return sum_squares
# Example usage
n=5
result = sum_of_squares(n)
print(f"The sum of squares of first {n} natural numbers is {result}")
def sum_of_cubes(n):
sum_cubes = 0
for i in range(1, n + 1):
sum_cubes += i ** 3
return sum_cubes
# Example usage
n=5
result = sum_of_cubes(n)
print(f"The sum of cubes of first {n} natural numbers is {result}")
39. Solve the following linear equations using Sympy library
x+y=1
x-y =1
# defining equations
eq1 = Eq((x+y), 1)
print("Equation 1:")
print(eq1)
eq2 = Eq((x-y), 1)
print("Equation 2")
print(eq2)
# defining equations
eq1 = Eq((x+y+z), 1)
print("Equation 1:")
print(eq1)
eq2 = Eq((x-y+2*z), 1)
print("Equation 2")
print(eq2)
eq3 = Eq((2*x-y+2*z), 1)
print("Equation 3")
(a) a or b
(b) (a and b) or c
(c) a and (b or c)
52. What is a list in Python? How can you check if 3 is an element of the list [1, 7, 5,
3, 4]?
53. Write down the syntax of an if-else statement in Python. Write a Python program
using if-else statements to check whether a number is even or odd.
54. Given a list: primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29], how do you ob-
tain the first four primes?
55. Write a Python program to find the surface area (4πr2 ) of a sphere, where radius of
sphere is given.
57. Explain the importance of “break” and “continue” statements with examples in Python.
58. Write a Python program to calculate the sum of three given numbers. If the values
are equal, print “The numbers are all equal”.
y = [x for x in range(10) if x % 2 == 0]
print(y)
1
(ii) Use SymPy to find the roots of the equation x4 − x2 + 1 = 0.
(iii) Use SymPy to simplify the expression (x3 + y 3 )(x3 y + 2y 2 ).
(i) sort()
(ii) append()
(iii) ]pop()
63. Write a Python program to check whether a year is a leap year or not.
64. Plot the functions f (x) = x2 and g(x) = x3 on the same graph over the interval [−2, 2]
using a dashed line for f (x) and a dotted line for g(x). Also add labels to the axes and
a title to the graph.
65. Plot the following two functions in one graph using SymPy: