Slip Programs
Slip Programs
Slip Programs
Design a class Employee with data members: name, department and salary. Create suitable
methods for reading and printing employee information
class Employee:
def read_employee(self):
def print_employee(self):
print("Employee Information")
employee1 = Employee()
employee1.read_employee()
employee1.print_employee()
Slip 2:
def calculate_factorial(number):
if number == 0 or number == 1:
return 1
else:
if num < 0:
else:
result = calculate_factorial(num)
Write a python program to read contents of first.txt file and write same content in
second.txt file
try:
file1_content = file1.read()
file2.write(file1_content)
except FileNotFoundError:
input_file_name = "first.txt"
output_file_name = "second.txt"
copy_file_contents(input_file_name, output_file_name)
Slip 4:
def print_fibonacci(n):
if n <= 0:
num1 = 0
num2 = 1
next_number = num2
count = 1
count += 1
print()
Write a program to input any two tuples and interchange the tuple variable.
list1 = []
list1.append(num)
tup1 = tuple(list1)
list2 = []
list2.append(num)
tup2 = tuple(list2)
Design a python program to calculate area of triangle and circle and print the result
import math
def calculate_circle_area(radius):
print(math.pi * radius**2)
# Calculate areas
calculate_triangle_area(base_tri, height_tri)
calculate_circle_area(radius_circle)
Slip 7:
Design a python program which will throw exception if the value entered by user is
less than zero
while True:
try:
if user_input < 0:
else:
break
except ValueError as e:
print(e)
Slip 8:
Design a python program to create a class EMPLOYEE with ID and NAME and display its contents
class Employee:
self.id = employee_id
self.name = employee_name
def display_employee_data(self):
employee1.display_employee_data()
Slip 9:
Design a program to importing a module for addition and subtractions of two numbers
Slip 10:
Root_folder/
├── Calculator/
│ ├── __init__.py
│ ├── module.py
└── main.py
Calculator/module.py
Calculator/main.py
print(m.addition(90, 10))
print(m.subtract(20, 10))
Slip 11:
Design a program to open a file in write mode and append some content at the end of file
file_name = "example.txt"
content_to_append = "\nThis is new content to append."
append_to_file(file_name, content_to_append)
Slip 12:
def count_character_frequency(file_name):
try:
with open(file_name, "r") as file:
content = file.read()
char_frequency = {}
for char in content:
if char.isalpha():
char = char.lower()
char_frequency[char] =
char_frequency.get(char, 0) + 1
print("Character frequencies:")
for char, frequency in char_frequency.items():
print(f"{char}: {frequency}")
except FileNotFoundError:
print(f"Error: File '{file_name}' not found.")
except Exception as e:
print(f"An error occurred: {e}")
file_name = "example.txt"
count_character_frequency(file_name)
Slip 13:
Design a class student with data members; Name, roll number address. Create suitable
method for reading and printing student’s details.
class Student:
def read_student_data(self):
self.name = input("Enter student name: ")
self.roll_number = input("Enter roll number: ")
self.address = input("Enter address: ")
def print_student_data(self):
print("Student Details:")
print("Name:", self.name)
print("Roll Number:", self.roll_number)
print("Address:", self.address)
student1 = Student()
student1.read_student_data()
student1.print_student_data()
Slip 14:
Create a parent class named Animals and a child class Herbivorous which will extend the
class Animal. In the child class Herbivorous over side the method feed( ) Create an object
class Animals:
def feed(self):
print("Generic feeding method.")
class Herbivorous(Animals):
def feed(self, diet):
print(f"Feeding with a herbivorous diet: {diet}.")
herbivorous_animal = Herbivorous()
herbivorous_animal.feed("Grass")
Slip 15:
Design a python program using module, show how to write and use module by importing
it.
Calculator.py
main.py
import calculator as c
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
print(f"Sum: {sum_result}")
print(f"Difference: {difference_result}")
print(f"Product: {product_result}")
print(f"Division: {division_result}")
Slip 16:
Root_folder/
├── Calculator/
│ ├── __init__.py
│ ├── module.py
└── main.py
Calculator/module.py
Calculator/main.py
print(m.addition(90, 10))
print(m.subtract(20, 10))
Slip 17:
Design a program to create class student with Roll no. and Name and display its contents
class Student:
def __init__(self, roll_no, name):
self.roll_no = roll_no
self.name = name
def display_student_info(self):
print("Student Details:")
print("Roll No:", self.roll_no)
print("Name:", self.name)
Slip 18:
class Animal:
def speak(self):
print("Animal Speaking")
class Dog(Animal):
def bark(self):
print("Dog Barking")
d = Dog()
d.bark()
d.speak()
Slip 19:
Write a python program that takes a number and checks whether it is palindrome or not
def is_palindrome(string):
str_repr = str(string)
return str_repr == str_repr[::-1]
if is_palindrome(user_input):
print(f"{user_input} is a palindrome.")
else:
print(f"{user_input} is not a palindrome.")
Slip 20:
Write a python program to create a tuple and find maximum and minimum number from it
list1 = []
for i in range(1, 6):
num = int(input("Enter number: "))
list1.append(num)
tup = tuple(list1)
print("Max number in tuple: ", max(tup))
Slip 21:
try:
number1 = float(input("Enter the numerator: "))
number2 = float(input("Enter the denominator: "))
divide_numbers(number1, number2)
except ValueError:
print("Error: Please enter valid numeric values.")
Slip 22:
Write a python function that takes a number as parameter and check the number is prime or not.
def is_prime(number):
if number < 2:
return False
for i in range(2, int(number**0.5) + 1):
if number % i == 0:
return False
return True
if is_prime(user_number):
print(f"{user_number} is a prime number.")
else:
print(f"{user_number} is not a prime number.")
Slip 23:
Write a python program to calculate sum of digit of given number using function
def sum_of_digits(number):
num_str = str(number)
digit_sum = sum(int(digit) for digit in num_str)
return digit_sum
Create a parent class named Animal and a child class Herbivorous which will extend the class
Animal.in the child class Herbivorous over side the method feed() create an object
class Animals:
def feed(self):
print("Generic feeding method.")
class Herbivorous(Animals):
def feed(self, diet):
print(f"Feeding with a herbivorous diet: {diet}.")
herbivorous_animal = Herbivorous()
herbivorous_animal.feed("Grass")
Slip 25:
Write a python program to create a class ‘degree’ having a method ‘getdegree’ that prints “I got
degree”. It has two subclasses namely ‘Undergraduate’ and ‘Postgraduates’ each having a method
with the same name that prints “I am an undergraduate” and “I am a postgraduate” respectively.
Call the method by creating object of each of the three classes
class Degree:
def get_degree(self):
print("I got a degree.")
class Undergraduate(Degree):
def get_degree(self):
print("I am an undergraduate.")
class Postgraduate(Degree):
def get_degree(self):
print("I am a postgraduate.")
degree_holder = Degree()
undergraduate_student = Undergraduate()
postgraduate_student = Postgraduate()
degree_holder.get_degree()
undergraduate_student.get_degree()
postgraduate_student.get_degree()