Python Assignment 1
March 27, 2026
Name: Tapasmit Naskar
Dept: Physics
Roll No. 002420701014
Class: UG II (4th sem)
1. Calculate BMI and print whether underweight, normal weight, overweight, obese.
[23]: wt = float(input("Enter your weight (in KG): "))
ht = float(input("Enter your height (in cm): "))
bmi = wt/(0.01*ht)**2
print("BMI: ", bmi, end=", Remark: ")
if bmi < 18.5:
print("Underweight")
elif bmi >= 18.5 and bmi <25:
print("Normal")
elif bmi >= 25 and bmi <30:
print("Overweight")
else:
print("Obese")
Enter your weight (in KG): 78
Enter your height (in cm): 172
BMI: 26.365603028664147, Remark: Overweight
2. Create a Paper, Rock, Scissor game with computer.
[24]: import random as rn
choice = ["Rock", "Paper", "Scissor"]
for i in range(3):
print(i+1, "-", choice[i])
user = int(input("Choose a number to pick: "))
print("Your choice: ",choice[user-1])
com = [Link](1, 4)
print("Computer's choice: ",choice[com-1])
lst = [1, 2, 3, 2, 3, 1]
if com == lst[[Link](user)+3]:
print("Computer Wins!")
elif com == user:
print("It's a tie")
1
else:
print("You win!")
1 - Rock
2 - Paper
3 - Scissor
Choose a number to pick: 3
Your choice: Scissor
Computer's choice: Scissor
It's a tie
3. Write a Python program that asks the user to input a number and then checks if the number
is even or odd.
[25]: num = int(input("Enter a number: "))
if num%2 == 0:
print(num, "is even.")
elif num%2 == 1:
print(num, "is odd.")
Enter a number: 11
11 is odd.
4. Write a Python program that checks if a given year is a leap year. (A year is a leap year if
it is divisible by 4, except for years that are divisible by 100 but not divisible by 400)
[26]: year = int(input("Enter the year: "))
leap = 0
if year%4 == 0:
if year%100 == 0:
if year%400 == 0:
print("Leap Year")
else:
print("Not a Leap Year")
else:
print("Leap Year")
else:
print("Not a Leap Year")
Enter the year: 2026
Not a Leap Year
5. Write a program that asks the user to input two numbers and a mathematical operation (+,
-, *, /). The program then performs the operation and outputs the result. If the user enters
an invalid operation, print an error message.
[28]: num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
2
ops = ["+","-","*","/"]
op = input("Choose an operation (+,-,*,/) : ")
if op in ops:
if [Link](op) == 0:
print(num1, "+", num2, "=", num1+num2)
if [Link](op) == 1:
print(num1, "-", num2, "=", num1-num2)
if [Link](op) == 2:
print(num1, "*", num2, "=", num1*num2)
if [Link](op) == 3:
print(num1, "/", num2, "=", num1/num2)
else:
print("Invalid operator")
Enter first number: 4
Enter second number: 6
Choose an operation (+,-,*,/) : /
4 / 6 = 0.6666666666666666
6. Take a number as input and print its factorial using both for and while loops.
[30]: n = int(input("Enter a non negative integer: "))
fact = 1
for i in range(n):
fact *= i+1
print("By 'for' loop,", n, "factorial is", fact)
fact = 1
while n > 0:
fact *= n
n -= 1
print("and by 'while' loop, it is", fact)
Enter a non negative integer: 6
By 'for' loop, 6 factorial is 720
and by 'while' loop, it is 720
7. Write a Python program that checks if a number n is prime or not. Use “for” loop.
[31]: num = int(input("Enter a number: "))
prime = True
for i in range(2, int(num**0.5)+1):
if num%i == 0:
prime = False
break
if prime:
print(num, "is a prime.")
else:
print(num, "isn't a prime")
3
Enter a number: 19
19 is a prime.
8. Write a program that prints the first n numbers of the Fibonacci sequence using a for loop.
[33]: n = int(input("Enter number of terms: "))
a, b = 0, 1
for i in range(n-1):
print(a, end=", ")
a, b = b, a+b
print(a)
Enter number of terms: 12
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89
9. Bisection Method for Root Finding: Find the root of a function using the bisection
method. Use a while loop to iterate until the root is found within a given tolerance.
[34]: def f(x):
return x**3 - 27
acc = 5
a, b = 0, 10
c = (a+b)/2
while abs(f(c)) > 10**(-acc):
c = (a+b)/2
if f(a)*f(c) < 0:
b = c
elif f(c)*f(b) < 0:
a = c
print("Root is approx.", round(c,acc))
Root is approx. 3.0
10. Newton’s Method for Solving Equations: Implement Newton-Raphson method to find
roots of a given function. Use a while loop to update the estimate until convergence.
[21]: def f1(x):
return x**2 - 2
def deriv(f,x):
return (f(x+10**(-5)) - f(x-10**(-5))) / (2*10**(-5))
acc = 5
guess = float(input("Enter a guess root value: "))
while abs(f1(guess)) > 10**(-acc):
guess -= f1(guess)/deriv(f1,guess)
print("Root is approx.", round(guess,acc))
Enter a guess root value: 4
Root is approx. 1.41421
11. Take a number as input and print its multiplication table up to 10.
4
[22]: n = int(input("Enter a number: "))
for i in range(10):
print(n, "x", i+1, "=", n*(i+1))
Enter a number: 12
12 x 1 = 12
12 x 2 = 24
12 x 3 = 36
12 x 4 = 48
12 x 5 = 60
12 x 6 = 72
12 x 7 = 84
12 x 8 = 96
12 x 9 = 108
12 x 10 = 120