Programming Chit Python
Programming Chit Python
Sol:- This program reads a file and counts how many times each character appears.
text = file.read()
char_freq = {}
if char in char_freq:
char_freq[char] += 1
else:
char_freq[char] = 1
print(f"'{char}': {freq}")
file.close()
Write python program to read contents of abc.txt and write same content to
pqr.txt
Sol:- # Open abc.txt in read mode
content = source_file.read()
destination_file.write(content)
Design a class student with data members; Name, roll number address.
Create suitable method for reading and printing students details.
Sol:- class Student:
def __init__(self)
self.name = ""
self.roll_no = 0
self.address = ""
def read_details(self):
def print_details(self):
s1 = Student()
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 a object of the class Herbivorous and call the method feed
Sol:- # Parent class
class Animals:
def feed(self):
class Herbivorous(Animals):
def feed(self):
h = Herbivorous()
greatest = a
greatest = b
elif c >= d:
greatest = c
else:
greatest = d
# if-elif-else ladder
print("Grade: A+")
print("Grade: A")
print("Grade: B")
print("Grade: C")
print("Grade: D")
else:
print("Fail")
class Animal:
self.name = name
self.age = age
def speak(self):
class Dog(Animal):
super().__init__(name, age)
self.breed = breed
def speak(self):
print(f"{self.name} barks.")
# Calling methods
Write a program to open a file in write mode and append some contents at
the end of file.
Sol: # Step 1: Open the file in write mode and write initial content
file.close()
# Step 2: Open the same file in append mode and add more content
file.close()
content = file.read()
print(content)
file.close()
Let's assume we want to create a package named my_package with the following structure
my_package/
__init__.py
module1.py
module2.py
module1.py:
def greet(name):
module2.py:
return a + b
y = 10
print(f"The sum of {x} and {y} is {add(x, y)}.") # Calling the function from module2
Write a program to create class EMPLOYEE with ID and NAME and display its
contents.
Sol: # Creating the EMPLOYEE class
class EMPLOYEE:
def display(self):
employee1.display()
# Parent class 1
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
# Parent class 2
class Mammal:
self.habitat = habitat
def describe_habitat(self):
self.breed = breed
def display_info(self):
print(f"Name: {self.name}")
print(f"Breed: {self.breed}")
print(f"Habitat: {self.habitat}")
import numpy as np
# Generate five random integers between 10 and 50