Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
38 views

Practical File Of: Python Practicals

This document contains a practical file submitted by Abhishek Kumar Singh with roll number 19CSE001. It contains 6 programs related to Python programming including programs to find GCD, square root using Newton's method, power of a number, maximum of a list, linear search and binary search. The file is submitted to Ruchi Ma'am for the 3rd semester at Ganga Institute of Technology and Management.

Uploaded by

Abhishek
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Practical File Of: Python Practicals

This document contains a practical file submitted by Abhishek Kumar Singh with roll number 19CSE001. It contains 6 programs related to Python programming including programs to find GCD, square root using Newton's method, power of a number, maximum of a list, linear search and binary search. The file is submitted to Ruchi Ma'am for the 3rd semester at Ganga Institute of Technology and Management.

Uploaded by

Abhishek
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

ABHISHEK KUMAR

SINGH
19CSE001

PRACTICAL FILE OF
Python Practicals

GANGA INSTITUTE OF TECHNOLOGY


AND
MANAGEMENT
(2020-2021)
Submitted To Submitted By

RUCHI MAAM ABHISHEK KUMAR SINGH


Professor (CSE) 19CSE001

GITAM, Kablana 3rd Sem

PROGRAM-1
AIM:- Program to compute the GCD of 2 numbers

CREATE-

# Python code to demonstrate gcd()

# method to compute gcd

import

math #

prints 12

print ("The gcd of 60 and 48 is : ",end="")

print (math.gcd(60,48))

OUTPUT-

PROGRAM-2
AIM:- Write a program to compute the square
root of a number by using Newton’s method.
CREATE-

def squareRoot(n, l) :

x=n

# To count the number of

itera ons count = 0

while (1) :
count += 1

# Calculate more closed

x root = 0.5 * (x + (n / x))

# Check for

closeness if

(abs(root - x) < l) :

break
# Update root

x = root

return root
# Driver code

if name == " main " :

n = 327

l = 0.00001

print(squareRoot(n, l))

OUTPUT-
ti

PROGRAM 3
AIM:- Write a program in python to nd the power of a
number

CREATE-

a = 10

b=3

# calcula ng power using exponen al oprator

(**) result = a**b

print (a, " to the power of ", b, " is = ", result)

OUTPUT-

ti

ti

fi
PROGRAM 4
AIM:- Write a program in Python to nd the
maximum of a list of numbers.

CREATE-

# crea ng empty

list list1 = []

# asking number of elements to put in list

num = int(input("Enter number of elements in list:

")) # itera ng ll num to append elements in list

for i in range(1, num + 1):

ele = int(input("Enter elements:

")) list1.append(ele)

# print maximum element

print("Largest element is:",

max(list1))

OUTPUT-
ti
ti

ti

fi
PROGRAM 5
AIM :- Write a program in Python to implement linear
search.
CREATE-

items = [5, 7, 10, 12, 15]

print("list of items is", items)

x = int(input("enter item to

search:")) i = ag = 0

while i < len(items):

if items[i] == x:

ag = 1

break

i=i+1

if ag == 1:

print("item found at posi on:", i + 1)

else:

print("item not found")

OUTPUT-
fl

fl

fl

ti

PROGRAM 6
AIM:- Write a program in Python to implement Binary
search.
CREATE-

# It returns index of x in given array arr if present, # else

returns -1

def binary_search(arr, x):

low = 0

high = len(arr) - 1

mid = 0

while low <= high:

mid = (high + low) // 2

# Check if x is present at mid if

arr[mid] < x:

low = mid + 1

# If x is greater, ignore le half elif

arr[mid] > x:

high = mid - 1

# If x is smaller, ignore right half else:

return mid

# If we reach here, then the element was not present return -1

# Test array

arr = [ 2, 3, 4, 10, 40 ]

x = 10

# Func on call

result = binary_search(arr, x)

OUTPUT-

ti

ft

PROGRAM 7
AIM:- Write a program in Python to implement selec on
sort.
CREATE-

import sys

A = [64, 25, 12, 22, 11]

# Traverse through all array elements for i in

range(len(A)):

# Find the minimum element in remaining #

unsorted array

min_idx = i

for j in range(i+1, len(A)): if

A[min_idx] > A[j]:

min_idx = j

# Swap the found minimum element with # the

rst element

A[i], A[min_idx] = A[min_idx], A[i]

# Driver code to test above print

("Sorted array")

for i in range(len(A)):

print("%d" %A[I])

OUTPUT-
fi

ti
PROGRAM 8
AIM:- Write a program in Python to implement inser on
sort.
CREATE-

# Func on to do inser on

sort def inser onSort(arr):

# Traverse through 1 to

len(arr) for i in range(1,

len(arr)):

key = arr[i]

# Move elements of arr[0..i-1], that are

# greater than key, to one posi on

ahead # of their current posi on

j = i-1
while j >=0 and key < arr[j] :

arr[j+1] = arr[j]

j -= 1

arr[j+1] = key

# Driver code to test above

arr = [12, 11, 13, 5, 6]

inser onSort(arr)
print ("Sorted array

is:") for i in

range(len(arr)): print

("%d" %arr[I])

OUTPUT-

ti

ti

ti

ti

ti
ti

ti
PROGRAM 9
AIM:- Write a program to implement Merge Sort.
CREATE-

def mergeSort(arr):

if len(arr) > 1:

# Finding the mid of the

array mid = len(arr)//2

# Dividing the array

elements L = arr[:mid]

# into 2 halves

R = arr[mid:]

# Sor ng the rst

half mergeSort(L)

# Sor ng the second

half mergeSort(R)

i=j=k=0
# Copy data to temp arrays L[] and

R[] while i < len(L) and j < len(R):

if L[i] < R[j]:


arr[k] =

L[i] i += 1

else:
arr[k] =

R[j] j += 1

k += 1

# Checking if any element was

le while i < len(L):


ft

ti
ti

fi

arr[k] =

L[i] i += 1

k += 1

while j < len(R):

arr[k] =

R[j] j += 1

k += 1
# Code to print the

list def printList(arr):

for i in

range(len(arr)):

print(arr[i], end="

")

print()

# Driver Code

if name == ' main ':

arr = [12, 11, 13, 5, 6, 7]

print("Given array is",

end="\n") printList(arr)

mergeSort(arr)

print("Sorted array is: ",

end="\n") printList(arr)

OUTPUT-

PROGRAM 10
AIM:- Write a program to nd rst n prime numbers.

CREATE-

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-

fi
fi

PROGRAM 11
AIM:- Write a program in Python for the mul plica on
of Matrices.
CREATE-

import numpy as

np # take a 3x3

matrix A = [[12, 7,

3],

[4, 5, 6],
[7, 8, 9]]

# take a 3x4 matrix

B = [[5, 8, 1, 2],

[6, 7, 3, 0],

[4, 5, 9, 1]]

# result will be 3x4

result= [[0,0,0,0],

[0,0,0,0],

[0,0,0,0]]

result = np.dot(A,B)

for r in result:

print(r)

OUTPUT-

ti
ti
PROGRAM 12
AIM:- Write a program in python that takes
command – line arguments.
CREATE-

import sys

print(type(sys.argv))

print('The command line arguments

are:') for i in sys.argv:

print(i)

OUTPUT-

PROGRAM 13
AIM:- Write a program in Python to nd the most
frequent words in a text read from a le.
CREATE-

count = 0; word =

""; maxCount = 0;

words = [];

#Opens a le in read mode le =

open("data.txt", "r")

#Gets each line ll end of le is reached for line in

le:

#Splits each line into words

string = line.lower().replace(',','').replace('.','').split(" "); #Adding all words

generated in previous step into words for s in string:

words.append(s);

#Determine the most repeated word in a le for i in range(0,

len(words)):

count = 1;

#Count each word in the le and store it in variable count for j in range(i+1,

len(words)):

if(words[i] == words[j]): count =

count + 1;

#If maxCount is less than count then store value of count in maxCount #and corresponding

word to variable word

if(count > maxCount):

maxCount = count; word =

words[i];

print("Most repeated word: " + word); le.close();


fi

fi

ti

fi
fi

fi
fi
fi

fi
fi

PROGRAM 14
AIM:- Write a program in Python to simulate
ellip cal orbits in Pygame.
CREATE-

import pygame

import math

import sys

pygame.init()

#se ng screen

size

screen = pygame.display.set_mode((600, 300))

#se ng cap on

pygame.display.set_cap on("Ellip cal orbit")

#crea ng clock variable

clock=pygame. me.Clock()

while(True):
for event in pygame.event.get():

if event.type ==

pygame.QUIT:

sys.exit()

# se ng x and y radius of

ellipse xRadius = 250

yRadius = 100

for degree in range(0,360,10):

x1 = int(math.cos(degree * 2 * math.pi/360) * xRadius)

+300 y1 = int(math.sin(degree * 2 * math.pi/360) *

yRadius)+150 screen. ll((0, 0, 0))

pygame.draw.circle(screen, (255, 69, 0), [300, 150], 40)


tti
tti

tti
ti
ti

ti

ti

fi
ti

ti

p y g a m e . d ra w. e l l i p s e ( s c r e e n , ( 2 5 5 , 2 5 5 , 2 5 5 ) ,

[50,50,500,200],1)

pygame.draw.circle(screen, (0, 255, 0), [x1, y1],

20) pygame.display. ip()

clock. ck(5)# screen refresh rate

OUTPUT-

ti

fl

PROGRAM 15
AIM:- Write a program in Python to simulate
bouncing balls using Pygame.
CREATE-

import sys,

pygame

pygame.init()

size = width, height = 800,

400 speed = [1, 1]

background = 255, 255, 255


screen = pygame.display.set_mode(size)

pygame.display.set_cap on("Bouncing

ball") ball = pygame.image.load("ball.png")

ballrect = ball.get_rect()

while 1:
for event in pygame.event.get():

if event.type ==

pygame.QUIT:

sys.exit()

ballrect = ballrect.move(speed)

if ballrect.le < 0 or ballrect.right >

width: speed[0] = -speed[0]

if ballrect.top < 0 or ballrect.bo om >

height: speed[1] = -speed[1]

screen. ll(background)

screen.blit(ball,

ballrect)

pygame.display. ip(
fi

ft

fl

ti

tt

OUTPUT-

You might also like