Python File
Python File
b=int(input(“second number”))
add = a+b
sub =a-b
mult = a*b
div = a/b
exp = a**b
print(“\n sum is :”, sum, “\n difference is :”, sub, “\n product is:”,mult, “\n quoitent is :”,div, “\n
exponent is :”,exp)
Output:
second number 2
sum is 5
difference is 1
product is 6
quotient is 1.5
exponent is 9
def calculate_area(name):
name = name.lower()
if name == "rectangle":
rect_area = l * b
sqt_area = s * s
tri_area = 0.5 * b * h
pi = 3.14
circ_area = pi * r * r
para_area = b * h
else:
print("Sorry! This shape is not available)
shape_name = input("Enter the name of shape whose area you want to find: ")
calculate_area(shape_name)
Output:
Rectangle
a=1
b=5
c=6
Output:
Enter a: 1
Enter b: 5
Enter c: 6
for i in range(L,U+1):
print(i)
Output:
x = int(input("x: "))
y = int(input("y: "))
z = int(input("z: "))
if x == y == z:
print("Equilateral triangle")
print("isosceles triangle")
else:
print("Scalene triangle")
Output:
x: 6
y: 6
z: 10
isosceles triangle
Output:
5*1=5
5*2=10
5* 3=15
5*4=20
5*5=25
5*6=30
5*7=35
5*8=40
5*9=45
5*10=50
a = num
sum = 0
if num <= 0:
else:
num = num - 1;
print(sum)
Output:
n1, n2 = 0, 1
count = 0
if nterms <= 0:
elif nterms == 1:
print(n1)
else:
print("Fibonacci sequence:")
print(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1
Output:
Fibonacci sequence:
0 1 1 2 3 5 8 13 21 34
factorial = 1
if int(n) >= 1:
factorial = factorial * i
print("Factorial of ",n , " is : ",factorial)
Output:
Enter a number: 5
Factorial of 5 is : 120
Count =0
While (num>0) :
temp=num%10
If (temp==5):
Count = Count +1
num=num/10
print(Count)
Output:
Enter a number:2356515
12. Print Geometric & Harmonic means of a series input by the users.
Output:
geometric mean:7.2456
Enter the element: 0.11 0.22 0.33 0.44 0.55 0.66 0.77 0.88 0.99
harmonic mean:0.350
a. x-x2/2!+x3/3!- x4/4!+…xn/n!
b. x-x3/3!+x5/5!- x7/7!+…xn/n!
def fact(m):
if m-=0:
return 1
else:
return m*fact(m-1)
s=0
x=int(input('enter x:'))
n=int(input('enter n:'))
for i in range(1,n+1):
s=s+((-1)*(i+1))((x**i)/fact(i))
print (s)
Output:
Enter x:1
Enter n:3
1.0
0.5
0.66666666666666666
b) def fact(m):
if m==0:
return 1
else:
return m*fact(m-1)
s=0
x=int(input('enter x :'))
n=int(input('enter n:'))
k=2*i-1
s=s+((-1)*(i+1)((x**k)/fact(k))
Output:
enter x:1
enter n:4
sum of series:0.841
Comb([4,5,6])
Output:
456
465
546
564
645
654
if (num%i) ==0:
break
else:
print (num)
Output:
11 13 17 19 23 29 31 37 41 43 47
C=0
for x in L:
C=C+1
Print (C)
Output:
N=4
for i in range(N):
for j in range(M):
B[i][j] = A[j][i]
A = [ [1, 1, 1, 1],
[2, 2, 2, 2],
[3, 3, 3, 3]]
# To store result
transpose(A, B)
for i in range(N):
for j in range(M):
print()
Output:
Result matrix is
1 2 3
1 2 3
1 2 3
1 2 3
X = [[1,2,3],
[4 ,5,6],
[7 ,8,9]]
Y = [[9,8,7],
[6,5,4],
[3,2,1]]
result = [[0,0,0],
[0,0,0],
[0,0,0]]
for i in range(len(X)):
for j in range(len(X[0])):
print(r)
2) Subtraction
X = [[1,2,3],
[4 ,5,6],
[7 ,8,9]]
Y = [[9,8,7],
[6,5,4],
[3,2,1]]
result = [[0,0,0],
[0,0,0],
[0,0,0]]
for i in range(len(X)):
for j in range(len(X[0])):
for r in result:
print(r)
3) Multiplication
X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
# 3x4 matrix
Y = [[5,8,1,2],
[6,7,3,0],
[4,5,9,1]]
# result is 3x4
result = [[0,0,0,0],
[0,0,0,0],
[0,0,0,0]]
for i in range(len(X)):
for j in range(len(Y[0])):
for k in range(len(Y)):
for r in result:
print(r)
ip_str = ip_str.casefold()
count = {}.fromkeys(vowels,0)
if char in count:
count[char] += 1
print(count)
Output:
print(len(final))
print(final)
vowels = "AaEeIiOoUu"
Check_Vow(string, vowels);
Output:
11
['e', 'a', 'e', 'u', 'i', 'o', 'o', 'u', 'i', 'e', 'i']
rev_str = reversed(my_str)
if list(my_str) == list(rev_str):
else:
Output:
1) Insert an element
list1 = [ 1, 2, 3, 4, 5, 6, 7 ]
list1.insert(4, 10)
print(list1)
Output:
[1, 2, 3, 4, 10, 5, 6, 7]
2) delete an element
lis = [2, 1, 3, 5, 4, 3, 8]
del lis[2 : 5]
print(lis)
Output:
[2, 1, 3, 8]
cars.sort()
print(cars)
Output:
cars.sort()
del(cars)
print(cars)
23. Display word after Sorting in alphabetical order.
words.sort()
print(word)
Output:
all
are
cgc
from
landran
we
for i in range(len(arr)):
if arr[i] == x:
return i
return -1
arr = ['t','u','t','o','r','i','a','l']
Output:
for i in range(len(arr)):
if arr[i] == x:
return i
return -1
arr = [1,2,3,4,5,6,7]
Output:
class notebook:
def fun(self):
def disp(self):
print(self.dic)
ob=notebook()
ob.fun()
ob.disp()
Output:
dic['Country']='India’
print(dic)
Output:
del dic['Col']
print(dic)
Output :
3) change
dic['Country']='England’
print(dic)
Output:
X=1
Y = 50
def checkRange(num):
29. Write a Python function that accepts a string and calculates number of
upper case letters and lower case letters available in that string.
def fun():
upper=0
lower=0
for i in range(len(str)):
lower+=1
upper+=1
fun()
Output:
return largest
a = 10
b = 14
c = 12
print(maximum(a, b, c))
Output:
14
result = 1
for x in myList:
result = result * x
return result
list1 = [1, 2, 3]
print(multiplyList(list1))
Output:
if n <= 1:
return n
else:
return(recur_fibo(n-1) + recur_fibo(n-2))
nterms = 10
if nterms <= 0:
else:
print("Fibonacci sequence:")
for i in range(nterms):
print(recur_fibo(i))
Output:
Fibonacci sequence:
0 1 1 2 3 5 8 13 21 34
if n == 1:
return n
else:
return n*recur_factorial(n-1)
if num < 0:
elif num == 0:
else:
Output:
def fact(n):
if n == 1:
return 1
else:
return (n * fact(n-1))
def check_num(a):
if a > 0:
elif a == 0:
else:
Ouput:
import findfact
findfact.fact(5)
120
class Rectangle():
def __init__(self, l, w):
self.length = l
self.width = w
def rectangle_area(self):
return self.length*self.width
Output:
120
36. Design a Python class named Circle constructed by a radius and two
methods which will compute the area and the perimeter of a circle.
import math
class circle():
def __init__(self,radius):
self.radius=radius
def area(self):
return math.pi*(self.radius**2)
def perimeter(self):
return 2*math.pi*self.radius
obj=circle(r)
print("Area of circle:",round(obj.area(),2))
print("Perimeter of circle:",round(obj.perimeter(),2))
Output:
return newSentence
print(reverseWordSentence(Sentence))
Output:
file_read('test.txt')
Output:
Studying python
40. Construct a Python program to write and append text to a file and
display the text.
file1.writelines(L)
file1.close()
# Append-adds at last
print(file1.read())
print()
file1.close()
Output:
We are from
CGC
LANDRAN
MOHALI PUNJAB