Python Programs (Except 9 & 12)
Experiment 1
a) Create a program that asks the user to enter their name and their age. Print out a message
addressed to them that tells them the year that they will turn 100 years old.
program
name = input("Enter your name: ")
age = int(input("Enter your age: "))
year = 2025 - age + 100
print(f"Hi {name}, you will turn 100 in the year {year}.")
b) Enter the number from the user and depending on whether the number is even or odd, print
out an appropriate message to the user.
program
num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even number")
else:
print("Odd number")
Experiment 2
a) Write a Python script to sort (ascending and descending) a dictionary by value.
program
data = {'a': 3, 'b': 1, 'c': 2}
print("Ascending:", dict(sorted([Link](), key=lambda x: x[1])))
print("Descending:", dict(sorted([Link](), key=lambda x: x[1], reverse=True)))
b) Write a Python program to get the largest number from a list.
program
nums = [5, 2, 8, 1, 9]
print("Largest number is:", max(nums))
Experiment 3
Operators Example) i) Arithmetic Operators ii) Relational Operators iii) Assignment
Operators iv) Logical Operators v) Bitwise Operators vi) Ternary Operator vii) Membership
Operators viii) Identity Operators
program
a, b = 5, 2
print("Arithmetic:", a + b, a - b, a * b, a / b)
print("Relational:", a > b, a == b)
print("Assignment:", (a := 10))
print("Logical:", a > 0 and b > 0)
print("Bitwise:", a & b, a | b)
print("Ternary:", "Positive" if a > 0 else "Negative")
print("Membership:", 2 in [1, 2, 3])
print("Identity:", a is b)
Experiment 4
a) Write a Python program to get the Fibonacci series.
program
n = int(input("Enter number: "))
a, b = 0, 1
for _ in range(n):
print(a, end=" ")
a, b = b, a + b
b) Write a Program to display all prime numbers within an interval.
program
start = int(input("Start: "))
end = int(input("End: "))
for num in range(start, end + 1):
if num > 1:
for i in range(2, num):
if num % i == 0:
break
else:
print(num)
Experiment 5
a) Write a program to add and multiply complex numbers.
program
a = complex(2, 3)
b = complex(1, 4)
print("Sum:", a + b)
print("Product:", a * b)
b) Write a program to find the largest element among three Numbers.
program
a, b, c = 10, 20, 15
print("Largest:", max(a, b, c))
Experiment 6
a) Write a Python function to calculate the factorial of a number.
program
def fact(n):
return 1 if n == 0 else n * fact(n-1)
print(fact(int(input("Enter number: "))))
b) Write a program to define a function with multiple return values.
program
def calc(a, b):
return a + b, a * b
print(calc(3, 4))
Experiment 7
a) Write a program to check whether the given number is palindrome or not.
program
num = input("Enter number: ")
print("Palindrome" if num == num[::-1] else "Not palindrome")
b) Write a program to define a function using default arguments.
program
def greet(name="User"):
print("Hello", name)
greet()
greet(input("Enter your name: "))
Experiment 8
a) Write a program to find the length of the string without using any library functions
program
text = input("Enter string: ")
count = 0
for _ in text:
count += 1
print("Length:", count)
b) Write a program to check if the substring is present in a given string or not.
program
s = input("Enter string: ")
sub = input("Enter substring: ")
print("Found" if sub in s else "Not found")
Experiment 10
Student Class) Design a class that store the information of student and display the same using
python programming.
program
class Student:
def __init__(self, name, roll_no,branch, age):
[Link] = name
self.roll_no = roll_no
[Link] = branch
[Link] = age
def display(self):
print("Name:", [Link])
print("Roll No:", self.roll_no)
print("Branch:", [Link])
print("Age:", [Link])
s = Student("Ram", 101, "EEE", 20)
[Link]()
Experiment 11
Exception Handling) Write a python program to implement exception handling.
program
try:
num = int(input("Enter number: "))
print(10 / num)
except ZeroDivisionError:
print("Cannot divide by zero")
except ValueError:
print("Invalid input")