1. Compute the sum, subtraction, multiplication, division, and exponent by given variable.
Input:-
x=int(input('enter the value of a'))
y=int(input('enter the value of b'))
print(x+y)
print(x-y)
print(x/y)
print(x*y)
print(x%y)
OUTPUT:-
2. find the areas of triangle, circle, trapezoid, rectangle, square, parallelogram.
#AREA OF SQUARE
Input:-
s=int(input('enter the side of sqaure'))
print(s*s)
area of triangle
a=int(input('enter the value of base of a triangle'))
b=int(input('enter the value of height of a triangle'))
print(1/2*a*b)
print()
#area of rectangle
l=int(input('enter the value of lenth of a rectangle'))
b=int(input('enter the value of breadth of a rectangle'))
print(l*b)
print()
#area of trapezoid formula (a+b/2)*h a and b sum of two parllel sides h = height
a=int(input('enter the value of base of one side '))
b=int(input('enter the value of base of second side'))
h=int(input('enter the value of height of trapezoid'))
print(a+b/2* h)
print()
#area of ||gm
b=int(input('enter the value of base of a parallelogram'))
h=int(input('enter the value of height of a parallelogram'))
print(b*h)
print()
#area of circle
Pi=3.14
r=float(input('enter the value of radius r'))
area= 2*Pi*r*r
print(area)
OUTPUT :-
3. Compute the volume of the following 3D shapes cube, cylinder, cone, and sphere.
VOL OF CUBE
Input:-
s=int(input('enter the vol of side of cube'))
print(s*s*s)
print()
#vol of cycl formula=ah a= area h= height
a=int(input('enter the value of area of cycl'))
h=int(input('enter the value of height of cycl'))
print(a*h)
print()
#vol of cone formula =1/3(ah)
a=int(input('enter the value of area of cone'))
h=int(input('enter the value of height of cone'))
print(1/3*a*h)
#vol of sphere formula=4/3pie rcube
pi=3.14
r=int(input('enter the value of radius of sphere'))
print(4/3*pi*r*r*r)
OUTPUT:-
4. Compute and print roots of quadratic equation ax2+bx+c=0, where the values of a,b, and c are the input by the user.
Input:-
# import complex math module
import cmath
a = float(input('Enter a: '))
b = float(input('Enter b: '))
c = float(input('Enter c: '))
# calculate the discriminant
d = (b**2) - (4*a*c)
# find two solutions
sol1 = (-b-cmath.sqrt(d))/(2*a)
sol2 = (-b+cmath.sqrt(d))/(2*a)
print('The solution are {0} and {1}'.format(sol1,sol2))
5. Print numbers up to N which are not divisible by 3,6,9 e.g. 1,2,4,5,7.
INPUT:-
N = int(input('enter N:'))
print("the numbers from 1 to ",N)
print('not divisble by 3,6,9')
for i in range(1,N+1):
if i%3!=0:
print(i,end=",")
OUTPUT:-
6. write a program to determine whether a triangle is isosceles or not.
Input:-
s1=input('side 1 :')
s2=input('side 2 :')
s3=input('side 3 :')
if s1==s2 and s2!=s3 and s1!=s3 :
print("triangle is isosceles")
elif s2==s3 and s2!=s1 and s1!=s3 :
print("triangle is isosceles")
elif s3==s1 and s1!=s2 and s3!=s2 :
print("triangle is isosceles")
else:
print('triangle is not isosceles')
OUTPUT:-
7. Print multiplication table of no. input by the user.
Input:-
n=int(input('enter a number :'))
for i in range(1,11):
print(n,"*" , i , "=", (n*i))
Output:-
8. Compute the sum of natural from one to n numbers.
Input:-
N=int(input('enter a number:'))
if N<0:
print('enter a positive number')
else:
sum=0
while(N>0):
sum+=N
N-=1
print("the sum is ",sum)
Output:-
9. Print fibonacci series up to n numbers e.g.0 11 2 3 5 8 13….n.
Input:-
n= int(input("How many terms? "))
n1, n2 = 0, 1
count = 0
if n <= 0:
print("Please enter a positive integer")
elif n == 1:
print("Fibonacci sequence upto",n,":")
print(n1)
else:
print("Fibonacci sequence:")
while count < n:
print(n1)
nth = n1 + n2
n1 = n2
n2 = nth
count += 1
Output:-
10. Compute factorial of a given number.
Input:-
def factorial(n):
if (n<=1):
return 1
return n*factorial(n-1)
print(factorial(int(input("enter a factorial of number:"))))
Output:-
11. Count occurrences of digit 5 in a given no. input by the user in python.
Input:-
s =int(input('enter a no.'))
s=str(s)
print("count of 5 :" ,s.count("5"))
print()
Output:-
12. Print Geometric and harmonic means of a series input by the user.
Input:-
a=int(input('enter 1st no.'))
b=int(input('enter 2nd no.'))
GM=(a*b)**0.5
HM=(2*a*b)/(a+b)
print('geometric mean of' ,a, 'and' ,b, '=',GM)
print('Harmonic mean of' ,a, 'and' ,b, '=',HM)
Output:-
13. Evaluate the following expression.
Input:-
expressions: a. x-x2/2!+x3/3!-x4/4!+…..xn/n!
b. x-x3/3!+x5/5!-x7/7!+…..xn/n
Output:-
14. Print all the possible combinations of 4,5,6.
Input:-
def comb(L):
for i in range(3):
for j in range(3):
for k in range(3):
if (i != j and j != k and i != k):
print(L[i], L[j], L[k])
comb([4, 5, 6])
Output:-
15. Determine prime numbers within a specific range.
Input:-
lower = int(input("Enter lower range: "))
upper = int(input("Enter upper range: "))
for num in range(lower, upper + 1):
if num > 1:
for i in range(2, num):
if (num % i) == 0:
break
else:
print(num)
Output:-
16. Count the number of people of age above 60 and below 90.
Input:-
n=int(input('enter the list or no. of person'))
l=[]
count=0
print('enter age of person in list')
for i in range(0,n):
print('input age of ',i+1,"person:")
k=int(input())
l.append(k)
if (l[i]>60 and l[i]<90):
count=count+1
print('no. of person of age above 60 and below 90=',count)
Output:-
17. Compute the transpose of a matrix.
Input:-
X = [[1, 2],
[4, 5],
[7, 8]]
Result = [[0, 0, 0],
[0, 0, 0]]
for i in range(len(X)):
for j in range(len(X[0])):
Result[j][i] = X[i][j]
for r in Result:
print(r)
Output:-
18. Perform the following operation on two matrices.
Input:-
MATRICES ADDITION
X = [[12,7],
[4 ,5],
[7 ,8]]
Y = [[5,8],
[6,7],
[4,5,]]
result = [[0,0],
[0,0],
[0,0]]
for i in range(len(X)):
for j in range(len(X[0])):
result[i][j] = X[i][j] + Y[i][j]
for r in result:
print(r)
Output:-
# SUBTRACTION OF TWO MATRICES.
Input:-
X = [[12, 7],
[4, 5],
[7, 8]]
Y = [[5, 8],
[6, 7],
[4, 5, ]]
result = [[0, 0],
[0, 0],
[0, 0]]
for i in range(len(X)):
for j in range(len(X[0])):
result[i][j] = X[i][j] - Y[i][j]
for r in result:
print(r)
Output:-
# MULTIPLICATION OF TWO MATRICES.
Input:-
X = [[12, 7],
[4, 5],
[7, 8]]
Y = [[5, 8],
[6, 7],
[4, 5, ]]
result = [[0, 0],
[0, 0],
[0, 0]]
for i in range(len(X)):
for j in range(len(X[0])):
result[i][j] = X[i][j] * Y[i][j]
for r in result:
print(r)
Output:-
19. Count the occurrence of vowels.
Input:-
n = str(input("Please type a sentence: "))
def Check_Vow(string, vowels):
final = [each for each in string if each in vowels]
print(len(final))
print(final)
vowels = "AaEeIiOoUu"
Check_Vow(n, vowels);
Output:-
20. Count the total no. vowels in a word.
Input:-
ch=str(input('enter a string'))
counta=0
counte=0
counti=0
counto=0
countu=0
for i in ch:
if i=='a'or i=='A':
counta= counta+1
elif i == 'i' or i == 'I':
counti = counti + 1
elif i == 'o' or i == 'O':
counto = counto + 1
elif i == 'e' or i == 'E':
counte = counte + 1
elif i == 'u' or i == 'U':
countu = countu + 1
print('a occurs','=',counta)
print('e occurs','=',counte)
print('i occurs','=',counti)
print('o occurs','=',counto)
print('u occurs','=',countu)
Output:-
21. Determine whether the string is palindrome or not.
Input:-
my_str = 'hey yoo'
my_str = my_str.casefold()
rev_str = reversed(my_str)
if list(my_str) == list(rev_str):
print("The string is a palindrome.")
else:
print("The string is not a palindrome.")
Output:-
22. Perform the following operations on a list of no.
Input:-
#insert an element in list
list = [89, 56, 90, 34, 89, 12]
list.insert(2, 23)
print("The list elements are")
for i in range(0, len(list)):
print(list[i])
Output:-
#deleting an item
Input:-
data=[2,4,5,6,7]
del data[3]
print(data)
Output:-
23. Display word after sorting in alpha order.
Input:-
my_str = "Hello i am sourav juneja"
words = [word.lower() for word in my_str.split()]
words.sort()
print("The sorted words are:")
for word in words:
print(word)
Output:-
24. Perform a sequential search on the list of given numbers.
Input:-
nlist = [4, 2, 7, 5, 12, 54, 21, 64, 12, 32]
print('List has the items: ', nlist)
searchItem = int(input('Enter a number to search for: '))
found = False
for i in range(len(nlist)):
if nlist[i] == searchItem:
found = True
print(searchItem, ' was found in the list at index ', i)
break
if found == False:
print(searchItem, ' was not found in the list!')
Output:-
25. Perform a sequential search on the ordered list of given numbers.
Input:-
li = [1,5,7,9,2,0,10,15,6,7]
k = -1
li.sort()
print("Ordered list: ",li)
while k !=0 :
n = int(input("\nEnter an element which you want to search: "))
for i in range(0,len(li)) :
if li[i]==n :
print("Found!!! At index = ",i,"\n")
k = 0
if k == -1 :
print("Not found!!!, Enter again")
Output:-
26. Maintain practical notebooks as per their serial numbers in the library using python dictionary.
Input:-
dict={"101:'PYTHON' , 102:'DATA STRUCT', 103:'MATHS' ,104:'ENGLISH' ,105:'PCAT' ,106:'COMPUTER'"}
print(dict)
Output:-
27. Perform following operations on dictionary 1)insert 2)delete 3) change.
# check whether a no. is in given range using functions
Input:-
def ran():
i=int(input('enter the 1st elemnet of range :'))
j = int(input('enter the 2nd elemnet of range :'))
if i>j:
n=int(input('enter the no. to check whether it is present or not'))
if n in range(i,j+1):
print('found!!')
else:
print('not found!!!!!!')
else:
print('wronge range input !!!! input again \n')
ran()
ran()
Output:-
28. Write a python function that accepts a string and calculates no. of upper case and lower case letters available in that string.
Input:-
ch=str(input('enter a string'))
s1=0
s2=0
for i in ch:
if(i.isupper()):
s1=s1+1
elif(i.islower()):
s2=s2+1
print('total no. of uppercase letter:',s1)
print('total no. of lowecase letter:',s2)
Output:-
29. To find a max of three numbers using function.
Input:-
def greater():
i = int(input('enter the 1st number :'))
j = int(input('enter the 2nd number :'))
k = int(input('enter the 3rd number :'))
if i>=j:
if i>=k:
print(i,'is maximum..')
else:
print(k,'is maximum..')
elif j>=i:
if j>=k:
print(j,'is maximum..')
else:
print(k,'is maximum..')
greater()
Output:-
30. Multiply all the numbers in a list using functions.
Input:-
def multi(k):
a=1
for i in k:
a=a*i
print('multiplication of all list element =',a)
li=[1,2,3,4,5,6]
multi(li)
Output:-
31. Solve the Fibonacci sequence using recursion.
Input:-
def fibonacci(n):
if n<=1:
return n
else:
return(fibonacci(n-1)+fibonacci(n-2))
s=int(input('enter a no.'))
print('series')
for i in range(s):
print(fibonacci(i),end=',')
Output:-
32. Get the factorial of a non-negative integer using recursion.
Input:-
def fact(x):
if(x==0):
return 1;
elif(x>0):
x=x*fact(x-1);
return x;
else:
return -1;
k=int(input('enter a number'))
m=fact(k)
if (m==-1):
print('wrong output!! enter other no.')
else:
print('fact of ',k,'is',m)
Output:-
33. Design a python class named rectangle constructed by length and width also design a method to which will compute the area of the rectangle.
Input:-
class Rectangle():
def __init__(self, l, w):
self.length = l
self.width = w
def rectangle_area(self):
return self.length*self.width
newRectangle = Rectangle(12, 10)
print(newRectangle.rectangle_area())
Output:-
34. Write a Python class named Circle constructed by a radius and two methods that will compute the area and the perimeter of a circle.
Input:-
class Circle():
def __init__(self, r):
self.radius = r
def area(self):
return self.radius**2*3.14
def perimeter(self):
return 2*self.radius*3.14
NewCircle = Circle(8)
print(NewCircle.area())
print(NewCircle.perimeter())
Output:-
35. Write a Python program to reverse a string word by word.
Input:-
class string:
__line=str()
def insert(self):
s=str(input('enter a string:'))
print()
self.line=s
print('before reversing')
print()
def __reversed__(self):
self.line=self.line[::-1]
return self.line
obj=string()
obj.insert()
print('string after reversing ',obj.__reversed__())
Output:-
36. Write a Python program to read an entire text file.
Input:-
sample_text = open("C:/Users/abcd/Desktop/kida.txt",'r')
for line in sample_text:
print(line)
sample_text.close()
Output:-
37. design a python program to read the first n lines of text files.
Input:-
def nfile():
try:
f = open('C:/Users/sourav/Desktop/kida.txt', 'r')
count = 0
for i in f:
count = count + 1
f.seek(0)
print('total numbers of line in file:' , count)
print()
try:
n = int(input('enter no.of lines(0 < n <='+str(count) +') to print: '))
except ValueError:
print('invalid input!!! Input again……..')
print()
nfile()
print()
if (n > 0 and n <= int(count)):
for k in range(n):
print('line', k+1,':', f.readline())
else:
print('invalid input!!! Input again….')
print()
nfile()
except:
print('error !!!!')
finally:
f.close()
nfile()
Output:-
38. Construct a python program to write and append text to a file and display the text.
Input:-
open('C:/Users/sourav/Desktop/kida.txt','r')
print('before appending...\n')
print(fi.read(),'\n')
fi.close()
fi=open('C:/Users/abc/Desktop/kida.txt',"a")
fi.write("\nmy name is junior")
fi.close()
fi=open('C:/Users/abc/Desktop/kida.txt','r')
print('after appending..\n')
print(fi.read(),"\n")
fi.close()
Output:-