EXECUTION
1.write a python program to convent given temperature from Fahrenheit to Celsius
and vice versa depending upon user’s choice.
CODING:-
sel = input("to convert Fahrenheit press F for Celsius C: ")
if(sel == 'f' or sel== 'F'):
temp = int(input('Enter temperture in celsius:' ))
fer=(temp*(9/5))+ 32
print ("temperture in Fahrenheit:" fer )
elif(sel=='C' or sel== 'c'):
temp= int (input("enter temture in fahrenheit :"))
celius= (trmp- 32)/9(9/5)
print("temperture in celsius:",celius)
else:
print(invalid choice)
OUTPUT:-
to convert Fahrenheit press F for Celsius C: f
Enter temperture in celsius:5
temperture in Fahrenheit: 41.0
to convert Fahrenheit press F for Celsius C: c
enter temture in fahrenheit :41
temperture in celsius: 5.0
2.Write a Python program that allows the user to enter any integer base and integer
exponent, and displays the value of the base raised to that exponent.
CODING:-
base = int(input("Enter the base (integer): "))
exponent = int(input("Enter the exponent (integer): "))
result = base ** exponent
print(f"{base} raised to the power of {exponent} is: {result}")
OUTPUT:-
Enter the base (integer): 2
Enter the exponent (integer): 3
2 raised to the power of 3 is: 8
3. write a python program to iuput any 10 numbers and displaybthe unique numbers in sorted
order after removing the duplicate entries.
CODING:-
def remove (num):
list=[]
for i in num:
if i not in list:
list.append(i)
return list
print("Enter any 10 numbers:")
num=[int(input())for i in range(10)]
num =remove(num)
num.sort()
print("\n The unique numbers in sorted order after removing the duplicate entries")
print(num)
OUTPUT:-
Enter any 10 numbers:
1
2
3
4
4
5
6
7
8
8
The unique numbers in sorted order after removing the duplicate entries
[1, 2, 3, 4, 5, 6, 7, 8]
4.Write a Python program to calculate total marks, percentage and grade of a student. Marks
obtained in each of the three subjects are to be input by the user. Assign grades according to the
following criteria:
Grade A: Percentage >=80
Grade B: Percentage>=70 and <80
Grade C: Percentage>=60 and <70
Grade D: Percentage>=40 and <60
Grade E: Percentage<40
CODING:-
print('Enter the marks\n')
sub1=int(input("c = "))
sub2=int(input("c++ = "))
sub3=int(input("python = "))
total=sub1+sub2+sub3
per = (total/300)*100
if(per>=80):
print('\nGrade A')
elif(per>= 70 and per<80):
print('\nGrade B')
elif(per>= 60 and per<70):
print('\nGrade C')
elif(per>= 40 and per<60):
print('\nGrade D')
else:
print("\nGrade E")
OUTPUT:-
Enter the marks
c = 85
c++ = 85
python = 96
Grade A
5. Write a program to input any 10 numbers and calculate their average using user defined
function.
CODING:-
def avg():
sum=0
for i in range (len (nums)):
sum=sum+nums[i]
avg=sum/len(nums)
print("average:",avg)
print("enter any 10 number:")
nums=[int(input()) for i in range (10)]
avg()
OUTPUT:-
enter any 10 number:
1
2
3
4
5
6
7
8
9
0
average: 4.5
6. Write a Python program to find the area of rectangle, square, circle and triangle by accepting
suitable input parameters from user using user-defined function.
CODING:-
def rectangle (length, breadth):
area=length*breadth
print("Area of Rectangle:",area)
def square(side):
area=side*side
print("Area of square:",area)
def circle(radius):
area=3.142*(radius*radius)
print("Area of circle:",area)
def triangle (base,height):
area=0.5*base*height
print("Area of Triangle:",area)
lenght=float(input("Enter the lenght of rectangle:"))
breadth=float(input("Enter the breadth of rectangle:"))
rectangle(lenght,breadth)
side=float(input("\n Enter the side of square:",))
square(side)
radius=float(input("\n\nEnter the radius of Circle:"))
circle(radius)
base=float(input("\n Enter the base of triangle:"))
height=float(input(" Enter the height of triangle:"))
triangle(base, height)
OUTPUT:-
Enter the lenght of rectangle:4
Enter the breadth of rectangle:6
Area of Rectangle: 24.0
Enter the side of square:8
Area of square: 64.0
Enter the radius of Circle:4
Area of circle: 50.272
Enter the base of triangle:4
Enter the height of triangle:4
Area of Triangle:
7. Write a Python program to Print and evaluate the following series. The series is -- Sum = (x)-
(x^2/2!) )+(x^3/3!) )-(x^4/4!) +(x^5/5!) )- ………………..
CODING:-
def fact(n):
if(n==1):
return 1
else:
return(n*fact(n-1))
r=int(input("Enter range:"))
series="(x)"
for i in range(2,r+1):
if(i%2==0):
series=series+"-(x^"+str(i)+"/"+str(i)+"!)"
else:
series=series+"+(x^"+str(i)+"/"+str(i)+"!)"
print("The series is----\nSum=",series)
x=int(input("Enter a value of x:"))
sum=x
for i in range(2,r+1):
if(i%2==0):
sum=sum-(x**i/fact(i))
else:
sum=sum+(x**i/fact(i))
print("sum=",sum)
OUTPUT:-
Enter range:5
The series is----
Sum= (x)-(x^2/2!)+(x^3/3!)-(x^4/4!)+(x^5/5!)
Enter a value of x:2
sum= 0.9333333333333333
8. Write a program to print current date and time. In addition to this, print each component of
date (i.e. year, month, day) and time (i.e. hours, minutes and microseconds) separately.
CODING:-
from datetime import*
today=datetime.today()
print("Today is:",today)
for attr in['year','month','day','hour','minute','microsecond']:
print(attr,":\t",getattr(today,attr))
OUTPUT:-
Today is: 2024-01-17 08:47:34.770009
year : 2024
month : 1
day : 17
hour : 8
minute : 47
microsecond : 770009
9. Write a Python program to calculate the subtraction of two compatible matrices.
CODING:-
matrix1 = [[4, 3],
[4, 5]]
matrix2 = [[1, 2],
[4, 6]]
matrix3 = [[0, 0],
[0, 0]]
for i in range(len(matrix1)):
for j in range(len(matrix1[0])):
matrix3[i][j] = matrix1[i][j] - matrix2[i][j]
for r in matrix3:
print(r)
OUTPUT:-
[3, 1]
[0, -1]
10. Write a Python program to calculate the addition of diagonal elements of a matrix.
CODING:-
def addition(x,n):
d1=0
d2=0
for i in range(0,len(x)):
for j in range(0,len(x[i])):
if(i==j):
d1=d1+x[i][j]
if(i==n-j-1):
d2=d2+x[i][j]
return d1,d2
a=int(input("Enter the number of row:"))
b=int(input("Enter the number of column:"))
if(a>b):
n=a
else:
n=b
print("Enter the elements of matrix:")
x=[[int(input())for j in range(b)]for i in range(a)]
for i in range(0,len(x)):
for j in range(0,len(x[i])):
print(x[i][j],end="\t")
print()
print(addition(x,n))
OUTPUT:-
Enter the number of row:3
Enter the number of column:3
Enter the elements of matrix:
2
6
7
3
9
5
7
3
5
2 6 7
3 9 5
7 3 5
(16, 23)
1.Write a Python program to search a given string from the list of strings using recursion.
CODING:-
def sea(sub,i):
if(i==len(list)):
return print("Given string is not found")
if(sub==list[i]):
return print("Given string is found")
else:
i=i+1
sea(sub,i)
list=['nagpur','mumbai','pune','nashik','surat']
print("original string list:" +str(list));
subs=input("Enter string to search:")
i=0
sea(subs,i)
OUTPUT:-
original string list:['nagpur', 'mumbai', 'pune', 'nashik', 'surat']
Enter string to search:naghid
Given string is not found
2. Write a Python program to input any 10 numbers and find out the second largest number
without using sort function.
CODING:-
print("Enter any 10 numbers:")
num=[int(input())for i in range(10)]
max1=max(num[0],num[1])
max2=min(num[0],num[1])
for j in range(2,len(num)):
if(num[j]>max1):
max2=max1
max1=num[j]
else:
if(num[j]>max2):
max2=num[j]
print("Second largest number:",max2)
OUTPUT:-
Enter any 10 numbers:
1
2
3
4
5
6
7
8
8
9
Second largest number: 8
3. Write a program to print first 10 prime numbers.
CODING:-
print("First 10 prime numbers")
a=2
q=0
while(q<10):
k=0
for i in range(2,a//2+1):
if(a%i==0):
k=k+1
if(k<=0):
print(a)
q=q+1
a=a+1
OUTPUT:-
First 10 prime numbers
2
3
5
7
11
13
17
19
23
29
4. Write a program to demonstrate the concept of inheritance in python.
CODING:-
class Rectangle:
def __init__(self, base, height):
# self.base = base
# self.height = height
area = base * height
print("Rectangle Area:", area)
class Triangle:
def __init__(self, base, height):
# self.base = base
# self.height = height
area = (base * height) / 2
print("Triangle Area:", area)
# Remove the import statements if you have the classes defined in the same file.
rect = Rectangle(4, 5)
trey = Triangle(4, 5)
OUTPUT:-
Rectangle Area: 20
Triangle Area: 10.0
5. Write a program to demonstrate the concept of method overriding in python.
CODING:-
class Awesome:
def greetings(self, message = "Hello World"):
print("Greetings from Awesome:", message)
class SuperAwesome(Awesome):
def greetings(self, message = None):
if message != None:
print("Greetings from SuperAwesome:", message)
else:
print("Greetings from SuperAwesome!")
cObj = SuperAwesome() # child class object
cObj.greetings()
OUTPUT:-
Greetings from SuperAwesome!
6. Write a Python program to print Fibonacci series for n terms using generator.
CODING:-
def fibo():
a=0
b=1
while True:
yield a
a, b=b, a+b
n = int(input("Enter range: "))
fib = fibo();
j=1
for i in fib:
if j <= n:
print(i)
else:
break
j=j+1
OUTPUT:-
Enter range: 8
0
1
1
2
3
5
8
13
7. Write a program to create three buttons labeled as red, green and blue respectively.
Background color of the window should be change accordingly when user click on the button.
CODING:-
from tkinter import *
window=Tk()
window.title('buttons')
def tog():
if window.cget('bg')=='red':
window.configure( bg='green' )
else:
window.configure( bg='red' )
btn_tog=Button(window,text='red', command=tog)
def tog1():
if window.cget('bg')=='green':
window.configure( bg='green' )
else:
window.configure( bg='green' )
btn_tog1=Button(window,text='green', command=tog1)
def tog2():
if window.cget('bg')=='blue':
window.configure( bg='green' )
else:
window.configure( bg='blue' )
btn_tog2=Button(window,text='blue', command=tog2)
btn_tog.pack(padx= 150, pady= 20)
btn_tog1.pack(padx= 150, pady= 20)
btn_tog2.pack(padx= 150, pady= 20)
window.mainloop()
OUTPUT:-
8. Write a program for gender selection using radio button. When the user clicks on submit button
it should display the selected gender on a label.
CODING:-
from tkinter import*
import tkinter.messagebox as box
window=Tk()
window.title('Radio Button Example')
frame=Frame(window)
book=StringVar()
radio_1=Radiobutton(frame,text='Male',variable=book,value='you select male')
radio_2=Radiobutton(frame,text='Female',variable=book,value='you select female')
radio_1.select()
def dialog():
box.showinfo('Selection','Your Choice:\n'+book.get())
btn=Button(frame,text='Choose',command=dialog)
btn.pack(side=RIGHT,padx=5)
radio_1.pack(side = LEFT)
radio_2.pack(side = LEFT)
frame.pack(padx=30,pady=30)
window.mainloop()
OUTPUT:-
9. Write a program to select a course from list box. When the user clicks on submit button, it
should display the message about selected course.
CODING:-
from tkinter import*
import tkinter.messagebox as box
window=Tk()
window.title('Listbox:sample')
frame=Frame(window)
listbox=Listbox(frame)
listbox.insert(1,'you select a course BCCA')
listbox.insert(2,'you select a course BCA')
listbox.insert(3,'you select a course MCA')
def dialog():
box.showinfo('Selection','Your Choice:'+ listbox.get(listbox.curselection()))
btn=Button(frame,text='Choose',command=dialog)
btn.pack(side=RIGHT,padx=5)
listbox.pack(side=LEFT)
frame.pack(padx=30,pady=30)
window.mainloop()
OUTPUT:-
10. Write a program that allows the user select the items using checkbox. When the user clicks on
submit button it should display the selected items name.
CODING:-
from tkinter import*
import tkinter.messagebox as box
window=Tk()
window.title('Check Button Example')
frame=Frame(window)
var_1 = IntVar()
var_2 = IntVar()
var_3 = IntVar()
book_1 = Checkbutton(frame, text = 'HTML5' ,variable = var_1, onvalue = 1, offvalue = 0)
book_2 = Checkbutton(frame, text = 'CSS3' ,variable = var_2, onvalue = 1, offvalue = 0)
book_3 = Checkbutton(frame, text = 'JS' ,variable = var_3, onvalue = 1, offvalue = 0)
def dialog():
str = 'Your Choice:'
if var_1.get() == 1 : str += '\nHTML5 in easy steps'
if var_2.get() == 1 : str += '\nCSS3 in easy steps'
if var_3.get() == 1 : str += '\nJavaScript in easy steps'
box.showinfo('Selection' , str)
b= Button(frame , text = 'Submit' , command = dialog)
b.pack(side = RIGHT,padx = 5)
book_1.pack(side = LEFT)
book_2.pack(side = LEFT)
book_3.pack(side = LEFT)
frame.pack(padx = 30, pady = 30)
window.mainloop()
OUTPUT:-