Python Imp Program Msbte Campus Academy
Python Imp Program Msbte Campus Academy
2. Write a program to input any two tuples and interchange the tuple variable
a=(1,2,3,4,5)
b=(11,12,13,14,24,35)
a,b=b,a
print(a)
print(b)
3. Write a python program to read contents of first.txt file and write same
content in second.txt file
infile=open("one.txt","rt")
outfile=open("two.txt","wt")
i=0
for i in range(n+1):
line=infile.readline()
outfile.write("\n"+line)
infile.close()
outfile.close()
infile=open("two.txt","rt")
print(infile.read())
infile.close()
def factorial(n):
if n==0:
return 1
else:
return n*factorial(n-1)
if num<0:
print("Factorial is not defined for nr=egative numbers!!")
else:
result=factorial(num)
print("Factorial of : ",num," is ",result)
5. Design a class Employee with data members: name, department and salary.
Create suitable methods for reading and printing employee information
class Employee:
def __init__(self):
self.name=""
self.department=""
self.salary=0.0
def readinfo(self):
self.name=input("Enter your name : ")
self.department=input("Enter your Department : ")
self.salary=float(input("Enter your Salary : "))
def dispinfo(self):
print("Name of employee is : "+self.name)
print("Department of employee is : "+self.department)
print("Salary of employee is : ",self.salary)
employee= Employee()
employee.readinfo()
employee.dispinfo()
import math
atriangle=0.5*base*height
print("Area of trianngle is : ",atriangle)
acircle=math.pi*radius**2
print("Area of Circle is : ",acircle)
7. Design a python program which will throw exception if the value entered
by user is less than zero
def validate(value):
if value<0:
raise ValueError("Value cannot be less than 0 ")
try:
num=int(input("Enter a value : "))
validate(num)
print("Value is valid")
except ValueError as e:
print("Error : "+str(e))
def con(str1,str2):
concatenated=str1+str2
return concatenated
string="Hello "
string1="World!"
result=con(string,string1)
print("Concatenated string are : "+result)
class Employee:
def __init__(self):
self.id=""
self.name=""
def readin(self):
self.id=int(input("Enter your id :"))
self.name=input("Enter your name :")
def printin(self):
print("Employee id is : ",self.id)
print("Employee name is : "+self.name)
employee= Employee()
employee.readin()
employee.printin()
10.Write a program for importing module for addition and subtraction of two
numbers.
Mathop.py
ex.py
import mathop
print("Addition:", resultadd)
print("Subtraction:", resultsub)
students = {}
students[1] = 'Pritam'
students[2] = 'Sameer'
students[3] = 'Saurabh'
students[2] = 'Shreyas'
del students[1]
Directory file
- my_program.py
- my_package/
- mathop.py
- __init__.py
# mathop.py
# my_program.py
print("Addition:", result_add)
print("Subtraction:", result_subtract)
13.Write a program to open a file in write mode and append some contents at
the end of file
#Single Inheritance
class Parent:
def fun1(self):
print("This is fun1 of Parent class")
class Child(Parent):
def fun2(self):
print("This is fun2 of Child class")
ob=Child()
ob.fun1()
ob.fun2()
class Child:
def fun2(self):
print("This is fun2 of Child class")
class child(Parent,Child):
def fun3(self):
print("Hello i am class 3")
ob=child()
ob.fun1()
ob.fun2()
ob.fun3()
#Multilevel Inheritance
class Parent:
def fun1(self):
print("This is fun1 of Parent class")
class Child(Parent):
def fun2(self):
print("This is fun2 of Child class")
class child(Child):
def fun3(self):
print("Hello i am class 3")
ob=child()
ob.fun1()
ob.fun2()
ob.fun3()
#Hirarchical Inheritance
class Parent:
def fun1(self):
print("This is fun1 of Parent class")
class Child(Parent):
def fun2(self):
print("This is fun2 of Child class")
class child(Parent):
def fun3(self):
print("Hello i am class 3")
ob=child()
ob1=Child()
ob.fun1()
ob1.fun2()
ob.fun3()
if num > 0:
print("The number is positive.")
elif num < 0:
print("The number is negative.")
else:
print("The number is zero.")
list1 = [ ]
num = int(input("Enter number of elements in list: "))
for i in range(1, num + 1):
element = int(input("Enter elements: "))
list1.append(element)
print("Largest element is:", max(list1))
import sys
x=int(sys.argv[1])
y=int(sys.argv[2])
sum=x+y
print("The addition is :",sum)
class Student:
def __init__(self):
self.name = ""
self.roll_number = ""
self.address = ""
def read_details(self):
self.name = input("Enter student's name: ")
self.roll_number = input("Enter student's roll number: ")
self.address = input("Enter student's address: ")
def print_details(self):
print("Student's Name:", self.name)
print("Roll Number:", self.roll_number)
print("Address:", self.address)
student1 = Student()
student1.read_details()
print("Student Details:")
student1.print_details()
20.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.
class Animal:
def feed(self):
print("Animal is being fed.")
class Herbivorous(Animal):
def feed(self):
print("Herbivorous animal is being fed with plants.")
herbivorous_animal = Herbivorous()
herbivorous_animal.feed()
for i in range(1,5):
for j in range(1,i+1):
print(j,end=' ')
print()
def swap(a,b):
print("Before swapping")
print("a : ",a)
print("b : ",b)
temp=a
a=b
b=temp
print("After swapping")
print("a : ",a)
print("b : ",b)
import random
random_integers = []
for _ in range(6):
random_int = random.randint(20, 50)
random_integers.append(random_int)
def factorial(n):
if n==0:
return 1
else:
return n*factorial(n-1)
if num<0:
print("Factorial is not defined for nr=egative numbers!!")
else:
result=factorial(num)
print("Factorial of : ",num," is ",result)
result = add_numbers(a,b)
print(result)
try:
dividend = int(input("Enter the dividend: "))
divisor = int(input("Enter the divisor: "))
except ZeroDivisionError:
print("Error: Division by zero is not allowed.")
except ValueError:
print("Error: Invalid input. Please enter integer values.")
except Exception as e:
print("An error occurred:", e)
27.Design class rectangle with data members length and breadth. Create
suitable methods for reading and printing the area and perimeter of
rectangle
class Rectangle:
def __init__(self):
self.length = 0
self.breadth = 0
def read_dimensions(self):
self.length = float(input("Enter the length of the rectangle: "))
self.breadth = float(input("Enter the breadth of the rectangle: "))
def area(self):
rarea = self.length * self.breadth
return rarea
def perimeter(self):
rperimeter = 2 * (self.length + self.breadth)
def print_details(self):
print("Area:", self.area())
print("Perimeter:", self.perimeter())
rectangle1 = Rectangle()
rectangle1.read_dimensions()
rectangle1.print_details()
# 1. Absolute value
num1 = -5
abs_value = abs(num1)
print("Absolute value of", num1, "is", abs_value)
# 2. Square root
num2 = 16
sqrt_value = math.sqrt(num2)
print("Square root of", num2, "is", sqrt_value)
# 3. Exponential
num3 = 2
exp_value = math.exp(num3)
print("Exponential of", num3, "is", exp_value)
# 4. Trigonometric functions
angle = math.pi / 4
sin_value = math.sin(angle)
cos_value = math.cos(angle)
tan_value = math.tan(angle)
print("Sine of", angle, "is", sin_value)
print("Cosine of", angle, "is", cos_value)
print("Tangent of", angle, "is", tan_value)
# 5. Rounding
num4 = 3.75
round_value = round(num4)
print("Rounded value of", num4, "is", round_value)
infile=open("abc.txt","rt")
outfile=open("pqr.txt","wt")
i=0
for i in range(n+1):
line=infile.readline()
outfile.write("\n"+line)
infile.close()
outfile.close()
infile=open("pqr.txt","rt")
print(infile.read())
infile.close()
class ShapeCalculator:
def calculate_area(self, length, breadth=None):
if breadth is None:
area = length * length
else:
area = length * breadth
return area
calculator = ShapeCalculator()
square_area = calculator.calculate_area(7)
print("Area of square:", square_area)
Reference:
Click