Professional Documents
Culture Documents
Python
Python
Output:
Program 2
To write a python program to perform Matrix Multiplication.
A=[]
n=int(input("Enter N for N x N matrix: "))
print("Enter the element ::>")
for i in range(n):
row=[]
for j in range(n):
row.append(int(input()))
A. append(row)
print(A)
B=[]
n=int(input("Enter N for N x N matrix : "))
print("Enter the element ::>")
for i in range (n):
row=[]
for j in range(n):
row.append(int(input()))
B. append(row)
print(B)
for i in range(len(A)):
for j in range(len(B[0])):
for k in range(len(B)):
result[i][j] += A[i][k] * B[k][j]
print("The Resultant Matrix Is ::>")
for r in result:
print(r)
Output:
Program 3
To write a python program to compute the GCD of two numbers.
Output:
Program 4
To write a python program to find the most frequent words in a text file.
import string
text = open("fruits.txt", "r")
d = dict()
for line in text:
line = line.strip()
line = line.lower()
line = line.translate(line.maketrans("", "", string.punctuation))
words = line.split(" ")
for word in words:
if word in d:
d[word] = d[word] + 1
else:
d[word] = 1
for key in list(d.keys()):
print(key, ":", d[key])
Output:
Program 5
To write a python program find the square root of a number (Newton’s
method).
Output:
Program 6
To write a python program exponentiation (power of a number).
def power(base,exp):
if(exp==1):
return(base)
if(exp!=1):
return(base*power(base,exp-1))
base=int(input("Enter base: "))
exp=int(input("Enter exponential value: "))
print("Result:",power(base,exp))
Output:
Program 7
To write a python program find the maximum of a list of numbers.
a=[]
n=int(input("Enter number of elements:"))
for i in range(1,n+1):
b=int(input("Enter element:"))
a.append(b)
a.sort()
print("Largest element is:",a[n-1])
Output:
Program 8
To write a python program linear search.
array = [2, 4, 0, 1, 9]
x=1
n = len(array)
result = linearSearch(array, n, x)
if(result == -1):
print("Element not found")
else:
print("Element found at index: ", result)
Output:
Program 9
To write a python program Binary search.
else:
# Element is not present in the
array return -1
# Test array
arr = [ 2, 3, 4, 10, 40 ]
x = 10
# Function call
result = binary_search(arr, 0, len(arr)-1, x)
if result != -1:
print("Element is present at index",
str(result)) else:
print("Element is not present in array")
Output:
Program 10
To write a python program selection sort.
data = [14,46,43,27,57,41,45,21,70]
size = len(data)
selectionSort(data, size)
print('Sorted Array in Ascending Order:')
print(data)
Output:
Program 11
To write a python program Insertion sort.
def insertionSort(nlist):
for index in range(1,len(nlist)):
currentvalue = nlist[index]
position = index
while position>0 and nlist[position-1]>currentvalue:
nlist[position]=nlist[position-1]
position = position-1
nlist[position]=currentvalue
nlist = [14,46,43,27,57,41,45,21,70]
insertionSort(nlist)
print(nlist)
Output:
Program 12
To write a python program merge sort.
numr=int(input("Enter range:"))
print("Prime numbers:",end=' ')
for n in range(1,numr):
for i in range(2,n):
if(n%i==0):
break
else:
print(n,end=' ')
Output:
Program 14
To write a python program simulate bouncing ball in Pygame.
import pygame
import time
pygame.init() #Initializes pygame
screen=pygame.display.set_mode((500,300)) #Sets the Pygame Window 500*300
y=1 #y co-ordinate of the centre of the ball
direction=1 #To decide whether to Increment/Decrement y value
counter=0 #To count the number of bounces
while True:
screen.fill((255,255,255))#White Screen
pygame.draw.circle(screen,(255,0,0),(250,y),13,0)#Draws the red ball at
(250,y) pygame.display.update()#Updates the above in the Pygame window
time.sleep(.006)#To pause for .006 seconds
if y==300: #The ball is at the bottom of window
direction=-1 #Move up by decrementing by 1
elif y==0: #The ball is at the top of the window
direction=1 #Move down by incrementing by 1
counter=counter+1 #One bounce completed
y=y+direction # Updates the next y co-ordinate of centre of ball
if counter==3: #Three bounces are over
pygame.quit() #Uninitializes pygame and window closes
break #Control comes out of while loop
Output:
Program 15
Write a program to compute area and circumference of a triangle.
Take input from user.
Output:
Program 16
Write a program to check if a number is Odd or even. Take input from
user.
Output:
Program 17
Write a program to check that a given year is Leap Year or not.
Output:
Program 18
To print ‘n terms of Fibonacci series using iteration.
# Program to display the Fibonacci sequence up to n-th term where n is provided by the user
# change this value for a different result nterms = 10
# uncomment to take input from the user
nterms = int(input("How many terms? "))
Output: