Pythonn
Pythonn
n1 = int(input("Enter 1st"))
n2 = int(input("Enter 2nd"))
n3 = int(input("Enter 3rd"))
n4 = int(input("Enter 4th"))
Write python program to read contents of abc.txt and write same content to
pqr.txt.
with open("abc.txt","r") as f1:
file1 = f1.read()
print(file1)
Write a python program to read contents of one file and write same content to
another file.
fr = input("Enter file name:")
fw = input("Enter file name:")
main.py:
import add
import sub
print(f"Addition is {add.add(10,20)}")
print(f"Subtraction is {sub.sub(20,10)}")
Using recursion:
n = int(input("Enter a number:"))
def fact(n):
if n == 0:
return 1
else:
return n*fact(n-1)
result = fact(n)
print(f"Factorial of {n} is: {result}")
Write a program to create a dictionary of students that includes their ROLL NO.
and Names.
1. Add 3 students in the above dictionary.
stud_dict = {1:"Kraken",2:"Bing",3:"ecA"}
print(stud_dict)
n = int(input("Enter number:"))
result = fibonacci(n)
print(f"fibonacci upto {n} terms is:{result}")
Design a class student with data members; Name, roll number address. Create
suitable method for reading and printing students details.
class Student:
def getStudentDetails(self):
self.name = input("Enter Name:")
self.rollno = int(input("Enter roll no:"))
self.address = input("Enter Adresss")
def printStudentDetails(self):
print(self.name, self.rollno, self.address)
s1 = Student()
s1.getStudentDetails()
print("Student Details are:")
s1.printStudentDetails()
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 ( ).
class Animal:
def breathe(self):
print("I breathe oxygen")
def feed(self):
print("I eat food")
class Herbivores(Animal):
def feed(self):
print("I eat plants only, I am veg")
herbi = Herbivores()
herbi.feed()
herbi.breathe()
rows = 4
for i in range(1, rows+1):
for j in range(1, i+1):
print(j, end=" ")
print()
2
468
10 12 14 16 18
num = 3
k=2
for i in range(1,num+1):
for j in range(i*2-1):
print(k,end = " ") [replace k with “*” to make It star patterns]
k = k+2
print()
1 2 3 4
2 3 4
3 4
4
for i in range(1,5):
for j in range(i,5):
print( j , end=" ") [replace j with “*” to make It star patterns]
print()
****
***
**
*
num = 4
for i in range(num,0,-1):
for j in range(0,num - i):
print(end=" ")
for j in range(0,i):
print("*", end=" ")
print()
Write a program to open a file in write mode and append some content at the end
of file
file1 = open("first.txt","w")
s = ("This is the some text\n","some more text\n")
file1.writelines(s)
file1.close()
file1 = open("first.txt","a")
file1.write("\n Some more appended text")
file1.close()
file1 = open("first.txt","r")
f = file1.read()
print(f)
Q]
T = (‘spam’, ‘Spam’, ‘SPAM!’, ‘SaPm’)
print (T [2] )
SPAM!
print (T [-2] )
SPAM!
print (T[2:] )
('SPAM!', 'SaPm')
print (List (T))
['spam', 'Spam', 'SPAM!', 'SaPm'] (this output if L in List(T) is in lowercase, maybe printing
mistake)
Write a program to create class EMPLOYEE with ID and NAME and display its
contents.
class employee:
def getdata(self,id,name):
self.id = id
self.name = name
def printData(self):
print("ID:", self.id)
print("Name:" , self.name)
e = employee()
e.getdata(107,"Jayesh")
e.printData()
Write a python program to input any two tuples and interchange the tuple
variables.
print("Original tuples:")
print("Tuple 1:", t1)
print("Tuple 2:", t2)
t1, t2 = t2 ,t1
print("\nAfter interchanging:")
print("Tuple 1:", t1)
print("Tuple 2:", t2)
a = input("Enter a string:")
a = a.lower()
b = a[-1::-1]
if(a == b):
print(f"{a} is palindrome")
else:
print(f"{a} is not palindrome")
while(number>0):
dig = number%10
reverse = reverse*10 + dig
number = number//10
if temp == reverse:
print(f"{temp} is a palindrome")
else:
print(f"{temp} is not a palindrome")
Write a Python program to calculate sum of digit of given number using function.
def sum_of_digits(num):
sum = 0
Design a python program to calculate area of triangle and circle and print the
result
def triangle(base,height):
return 0.5* base* height
def circle(radius):
return 3.14* radius*radius
triangle_area = triangle(base,height)
circle_area = circle(radius)
numpy ex:
import numpy as np
arr1 = np.array([1,2,3,4,5])
print(f"1D array: {arr1}")
arr2 = np.array([[1,2,3],[4,5,6],[7,8,9]])
print(f"2D array: {arr2}")