Python Lab File1
Python Lab File1
SCIENCE BHOPAL
NAME – CHANCHAL MALVIYA ROLL NO – 0506CS171005
BRANCH – COMPUTER SCIENCE SEMESTER – 5th
SUBJECT –PYTHON
The Greatest Common Divisor (GCD) of two or more integers is the largest positive integer that
divides a given integer values without remainder. For example, the GCD value of integer 8 and
12 is 4 because, both 8 and 12 are divisible by 1, 2, and 4 (remainder is 0) and the largest
positive integer among them is 4.
ef gcd(a,b):
if(b==0):
return a
else:
return gcd(b,a%b)
a=int(input("Enter first number:"))
b=int(input("Enter second number:"))
GCD=gcd(a,b)
print("GCD is: ")
print(GCD)
Runtime Test Cases
Case 1:
Enter first number:5
Enter second number:15
GCD is:
5
Case 2:
Enter first number:30
Enter second number:12
GCD is:
6
2.To write a Python Program to find the square root of a number by Newton’s Method.
Square root expression is rephrased as a parabola that can be optimized via Newtonian
optimization. The same can be applied to higher order roots. Optimal point for this
function is the square root for “a”.
def
newton_method(number,
number_iters = 500):
a = float(number) # number to get
square root of
for i in range(number_iters): # iteration
number
number = 0.5 * (number + a /
number) # update
# x_(n+1) = 0.5 * (x_n +a / x_n)
return number
print newton_method(9)
# Output: 3
print newton_method(2)
# Output: 1.41421356237
3. To write a Python program to find the exponentiation of a number.
Given two numbers a and b, we have to find a to the power b using exponential operator (**)
a = 10
b=3
Problem Description
The program takes a list and prints the largest number in the list.
Problem Solution
1. Take in the number of elements and store it in a variable.
2. Take in the elements of the list one by one.
3. Sort the list in ascending order.
4. Print the last element of the list.
5. Exit.
Program/Source Code
Here is source code of the Python Program to find the largest number in a list. The program
output is also shown below.
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])
Runtime Test Cases
Case 1:
Enter number of elements:3
Enter element:23
Enter element:567
Enter element:3
Largest element is: 567
Case 2:
Enter number of elements:4
Enter element:34
Enter element:56
Enter element:24
Enter element:54
Largest element is: 56
def selection_sort(alist):
for i in range(0, len(alist) - 1):
smallest = i
for j in range(i + 1, len(alist)):
if alist[j] < alist[smallest]:
smallest = j
alist[i], alist[smallest] = alist[smallest], alist[i]
alist = input('Enter the list of numbers: ').split()
alist = [int(x) for x in alist]
selection_sort(alist)
print('Sorted list: ', end='')
print(alist)
Runtime Test Cases
Case 1:
Enter the list of numbers: 3 1 4 5 2 6
Sorted list: [1, 2, 3, 4, 5, 6]
Case 2:
Enter the list of numbers: 2 10 5 38 1 7
Sorted list: [1, 2, 5, 7, 10, 38]
Case 3:
Enter the list of numbers: 5 3 2 1 0
Sorted list: [0, 1, 2, 3, 5]
8. To write a Python Program to perform insertion sort.
def insertion_sort(alist):
for i in range(1, len(alist)):
temp = alist[i]
j=i-1
while (j >= 0 and temp < alist[j]):
alist[j + 1] = alist[j]
j=j-1
alist[j + 1] = temp
alist = input('Enter the list of numbers: ').split()
alist = [int(x) for x in alist]
insertion_sort(alist)
print('Sorted list: ', end='')
print(alist)
Runtime Test Cases
Case 1:
Enter the list of numbers: 2 4 1 5 8 0
Sorted list: [0, 1, 2, 4, 5, 8]
Case 2:
Enter the list of numbers: 5 4 3 2 0 -1
Sorted list: [-1, 0, 2, 3, 4, 5]
Case 3:
Enter the list of numbers: 3 4 1 4 5 0 7
Sorted list: [0, 1, 3, 4, 4, 5, 7]
9.To write a Python Program to perform Merge sort.
# 3x3 matrix
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 r in result:
print(r)
Output
The Python sys module provides access to any of the command-line arguments via sys.argv. It
solves two purposes:
13. To write a Python program to find the most frequent words in a text read from a file.
Given the data set, we can find k number of most frequent words.
The solution of this problem already present as Find the k most frequent words from a file. But
we can solve this problem very efficiently in Python with the help of some high performance
modules.
In order to do this, we’ll use a high performance data type module, which is collections. This
module got some specialized container datatypes and we will use counter class from this
module.
Approach :
1. Import Counter class from collections module.
2. Split the string into list using split(), it will
3. return the lists of words.
4. Now pass the list to the instance of Counter class
5. The function 'most-common()' inside Counter will return
6. the list of most frequent words from list and its count.
Output :
[('Geeks', 5), ('to', 4), ('and', 4), ('article', 3)]
import pygame
import math
import sys
pygame.init()
screen = pygame.display.set_mode((600, 300))
pygame.display.set_caption("Elliptical orbit")
clock = pygame.time.Clock()
while(True):
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
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.fill((0, 0, 0))
pygame.draw.circle(screen, (255, 0, 0), [300, 150], 35)
pygame.draw.ellipse(screen, (255, 255, 255), [50, 50, 500, 200], 1)
pygame.draw.circle(screen, (0, 0, 255), [x1, y1], 15)
pygame.display.flip()
clock.tick(5)
Output:
15. To write a Python program to bouncing ball in Pygame.
# Bouncing ball
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Bouncing ball")
ball = pygame.image.load("ball.jpg")
ballrect = ball.get_rect()
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
screen.fill(background)
screen.blit(ball, ballrect)
pygame.display.flip()
ball.jpg
Output: