4marks Programs
4marks Programs
1
12
123
1234
Answer:
print()
Write a program to create class EMPLOYEE with ID and NAME and display its contents.
class EMPLOYEE:
self.emp_id = emp_id
self.name = name
def display(self):
e1 = EMPLOYEE(101, "John")
e1.display()
Write a program for importing module for addition and substraction of two numbers.
Mymath.py
return a + b
return a – b
main.py
import mymath
a = 10
b=5
Write a program to create dictionary of students that includes their ROLL NO. and NAME.
students = {}
print("Step 1: Adding three students")
students[1] = "Amit"
students[2] = "Rahul"
students[3] = "Sneha"
print(students)
students[2] = "Shreyas"
print(students)
del students[1]
print(students)
operations.py
a = 20
b=5
Output:
Multiplication: 100
Division: 4.0
Write a Python program to find the factorial of a number provided by the user.
factorial = 1
if num < 0:
else:
factorial *= i
Write a python program to input any two tuples and interchange the tuple variables.
print("After swapping:")
class MyException(Exception):
pass
if num < 0:
else:
output::-
...
1010101
10101
101
1
# Set number of rows
rows = 4
for i in range(rows):
# Print 1 0 pattern
# ii) Access set elements (Sets are unordered, so accessing will show all elements)
my_set.add(60)
output:::
Set after adding an element: {50, 20, 40, 10, 60, 30}
dict1⋅update(dict2);
answer:::---
Google 1
Facebook 2
Microsoft 2
GFG 1
Youtube 3
Write a python program that takes a number and checks whether it is a palindrome.
reverse_num = str(num)[::-1]
if str(num) == reverse_num:
print(f"{num} is a palindrome")
else:
Write a python program to create a user defined module that will ask your program name and
display the name of the program.
def display_program_name():
program_name = input("Enter your program name: ")
print("The name of your program is:", program_name)
Output:
Enter your program name: My First Program
The name of your program is: My First Program
Write a python program takes in a number and find the sum of digits in a number.
# Initialize sum to 0
sum_digits = 0
output::-
Enter a number: 12
Sum of digits: 3
Write a program function that accepts a string and calculate the number of uppercase letters and
lower case letters.
def count_letters(s):
upper = sum(1 for c in s if c.isupper())
lower = sum(1 for c in s if c.islower())
print(f"Uppercase: {upper}, Lowercase: {lower}")
count_letters("Hello World")
Uppercase: 2, Lowercase: 8
Write a python program to create class student with roll-no and display its contents.
class Student:
self.roll_no = roll_no
def display(self):
student1 = Student(101)
student1.display()
operations.py
a = 20
b=5
Output:
Multiplication: 100
Division: 4.0
Illustrate with example method overloading.
Python does not support traditional method overloading like some other languages (e.g., Java or C++),
but you can simulate method overloading using default arguments or *args.
class Calculator:
def add(self, a=0, b=0, c=0):
return a + b + c
calc = Calculator()
Output:
Sum of 2 numbers: 30
Sum of 3 numbers: 60
Sum of 1 number: 10
try:
print("Result:", result)
except ZeroDivisionError:
OP:
Enter a dividend: 3
Enter a divisor: 0
fruit = 'banana'
fruit[:3] → 'ban'
fruit[3:] → 'ana'
fruit[3:3] → ''
fruit[:] → 'banana'
Write a Python program to read contents from “a.txt” and write same contents in “b.txt”
data = file_a.read()
print("data is read")
# Write to b.txt
file_b.write(data)
print("data is written")