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

Lab Report Python

Uploaded by

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

Lab Report Python

Uploaded by

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

LAB REPORT

Python Programming
(CSE3011)
Class ID - BL2024250100337

Submitted by

Thilak Raaj
23BAI10724
in partial fulfillment for the award of the degree
of

BACHELOR OF TECHNOLOGY
in

COMPUTER SCIENCE AND ENGINEERING (Artificial Intelligence and


Machine learning)

SCHOOL OF COMPUTING SCIENCE AND ENGINEERING


VIT BHOPAL UNIVERSITY
KOTHRIKALAN, SEHORE
MADHYA PRADESH - 466114
August 2024

1
INDEX

SL. NO. TITLE DATE PAGE NO.

1 Write python program to print list of numbers using 07/08/24 3


range and for loop.

2 Write python program to print first n prime numbers. 07/08/24 4

3 Write python program to multiply matrices. 07/08/24 5

4 Write python program to take command line 07/08/24 7


arguments (word count).

5 Write python program in which a function is defined 07/08/24 8


and calling that function prints ‘Hello World’.

6 Write python program to let user enter some data in 07/08/24 9


string and then verify data and print welcome to user.

7 Write python program to store strings in list and then 07/08/24 10


print them.

8 Write python program to find the most frequent 07/08/24 11


words in a text read from a file.

9 Write python program in which a class is defined, then 07/08/24 13


create object of that class and call simple ‘print
function’ defined in class.

10 Write python program in which a function (with single 07/08/24 14


string parameter) is defined and calling that function
prints the string parameters given to function.

11 Simulate elliptical orbits in Pygame. 07/08/24 15

12 Simulate bouncing ball using Pygame. 07/08/24 19

2
Course Code: CSE3011
Course Name: Python Programming
Student Name: Thilak Raaj
Registration No: 23BAI10724
Lab 1

Write python program to print list of numbers using range and for loop.
Code:
r=int(input("Enter starting range : "))
e=int(input("Enter ending range : "))
print("The list of numbers starting from",r,"to",e,':')
for i in range(r,e):
print(i)

Output:
Enter starting range : 5
Enter ending range : 10
The list of numbers starting from 5 to 10 :
5
6
7
8
9

3
Course Code: CSE3011
Course Name: Python Programming
Student Name: Thilak Raaj
Registration No: 23BAI10724
Lab 2
Write python program to print first n prime numbers.
Code:
n=int(input("Enter n for printing prime numbers : "))
count=0
num=2
primes=[]
while count<n:
is_p=True
for i in range(2, int(num**0.5)+1):
if num%i==0:
is_p=False
break
if is_p:
primes.append(num)
count+=1
num+=1
print(f"The first {n} prime numbers are : {primes}")
Output:
Enter n for printing prime numbers : 10
The first 10 prime numbers are : [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]

4
Course Code: CSE3011
Course Name: Python Programming
Student Name: Thilak Raaj
Registration No: 23BAI10724
Lab 3

Write python program to multiply matrices.


Code:
A = [[1, 2, 3],[4, 5, 6]]
B = [[7, 8],[9, 10],[11, 12]]
rows_A = len(A)
cols_A = len(A[0])
rows_B = len(B)
cols_B = len(B[0])
if cols_A != rows_B:
print("Matrices cannot be multiplied")
else:
result = []
for i in range(rows_A):
result.append([0] * cols_B)
for i in range(rows_A):
for j in range(cols_B):
for k in range(cols_A):
result[i][j] += A[i][k] * B[k][j]

5
print("Result of matrix multiplication:")

for row in result:


print(row)

Output:
Result of matrix multiplication:
[58, 64]
[139, 154]

6
Course Code: CSE3011
Course Name: Python Programming
Student Name: Thilak Raaj
Registration No: 23BAI10724

Lab 4
Write python program to take command line arguments (word count).
Code:
a=input("Enter any line : ")
b=a.split()
c=len(b)
print("This line contains",c,"words")

Output:
Enter any line : a b c d e f g h i j k l m n o p q r s t u v w x y z
This line contains 26 words

7
Course Code: CSE3011
Course Name: Python Programming
Student Name: Thilak Raaj
Registration No: 23BAI10724

Lab 5
Write python program in which a function is defined and calling that function
prints ‘Hello World’.
Code:
def prt():
print("Hello world")

prt()

Output:
Hello world

8
Course Code: CSE3011
Course Name: Python Programming
Student Name: Thilak Raaj
Registration No: 23BAI10724

Lab 6
Write python program to let user enter some data in string and then verify data
and print welcome to user.
Code:
a="hello@123"
id="VS"
b=input("Enter ID : ")
c=input("Enter password : ")
if b==id and a==c:
print("Welcome back!")

Output:
Enter ID : VS
Enter password : hello@123
Welcome back!

9
Course Code: CSE3011
Course Name: Python Programming
Student Name: Thilak Raaj
Registration No: 23BAI10724

Lab 7
Write python program to store strings in list and then print them.
Code:
a=[]
n=int(input("Enter no of strings to enter : "))
for i in range(n):
str=input("Enter string :")
a.append(str)

for j in a:
print(j)

Output:
Enter no of strings to enter : 2
Enter string :a
Enter string :b
a
b

10
Course Code: CSE3011
Course Name: Python Programming
Student Name: Thilak Raaj
Registration No: 23BAI10724

Lab 8
Write python program to find the most frequent words in a text read from a file.
Code:
from collections import Counter
import re
def read_file(file_path):
with open(file_path,'r', encoding='utf-8') as file:
return file.read()
def preprocess_text(text):
text = re.sub(r'[^\w\s]','',text)
text = text.lower()
return text
def get_most_frequent_words(text, n=10):
words = text.split()
word_counts = Counter(words)
return word_counts.most_common(n)
def main(file_path):
text = read_file(file_path)
processed_text = preprocess_text(text)
most_frequent_words = get_most_frequent_words(processed_text)

11
print("The most frequent words are:")

for word, count in most_frequent_words:


print(f"{word}: {count}")

file_path ="C:\\Users\\mvmuk\\OneDrive\\Desktop\\Codes\\BACKEND\\Word.txt"

main(file_path)

Output
T he most frequent words are:
world: 3
hello: 2
have: 2
day: 2
a: 1
great: 1

12
Course Code: CSE3011
Course Name: Python Programming
Student Name: Thilak Raaj
Registration No: 23BAI10724

Lab 9
Write python program in which a class is defined, then create object of that class
and call simple ‘print function’ defined in class.
Code :
class A:
def view(self):
print("This is a simple class!")

b=A()
b.view()

Output :
This is a simple class!

13
Course Code: CSE3011
Course Name: Python Programming
Student Name: Thilak Raaj
Registration No: 23BAI10724

Lab 10
Write python program in which a function (with single string parameter) is
defined and calling that function prints the string parameters given to function.
Code:
def prt(str):
print(str)
s = input(“Enter a parameter : “)
prt(s)

Output:
Enter a parameter : python
python

14
Course Code: CSE3011
Course Name: Python Programming
Student Name: Thilak Raaj
Registration No: 23BAI10724

Lab 11
Simulate elliptical orbits in Pygame.
Code:
import pygame
import math

pygame.init()
width = 1000
height = 600
screen_res = (width, height)
pygame.display.set_caption("GFG Elliptical orbit")
screen = pygame.display.set_mode(screen_res)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
cyan = (0, 255, 255)

X_center = width//2
Y_center = height//2

15
X_ellipse = 400
Y_ellipse = 225
clock = pygame.time.Clock()
while True:
for degree in range(0, 360, 1):
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
screen.fill([0, 0, 0])

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


* X_ellipse) + X_center

y_planet_1 = int(math.sin(degree * 2 * math.pi/360)


* Y_ellipse) + Y_center

degree_2 = degree+180
if degree > 180:
degree_2 = degree-180
x_planet_2 = int(math.cos(degree_2 * 2 * math.pi/360)
* X_ellipse) + X_center

y_planet_2 = int(math.sin(degree_2 * 2 * math.pi/360)* Y_ellipse) +


Y_center

16
pygame.draw.circle(surface=screen, color=red, center=[
X_center, Y_center], radius=60)

pygame.draw.ellipse(surface=screen, color=green,
rect=[100, 75, 800, 450], width=1)

pygame.draw.circle(surface=screen, color=blue, center=[


x_planet_1, y_planet_1], radius=40)
pygame.draw.circle(surface=screen, color=cyan, center=[
x_planet_2, y_planet_2], radius=40)

clock.tick(5)
pygame.display.flip()

17
Output :

18
Course Code: CSE3011
Course Name: Python Programming
Student Name: Thilak Raaj
Registration No: 23BAI10724

Lab 12
Simulate bouncing ball using Pygame.
Code:
import pygame
pygame.init()
width = 1000
height = 600
screen_res = (width, height)

pygame.display.set_caption("GFG Bouncing game")


screen = pygame.display.set_mode(screen_res)
red = (255, 0, 0)
black = (0, 0, 0)
ball_obj = pygame.draw.circle(
surface=screen, color=red, center=[100, 100], radius=40)
speed = [1, 1]
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
19
exit()
screen.fill(black)
ball_obj = ball_obj.move(speed)
if ball_obj.left <= 0 or ball_obj.right >= width:
speed[0] = -speed[0]
if ball_obj.top <= 0 or ball_obj.bottom >= height:
speed[1] = -speed[1]
pygame.draw.circle(surface=screen, color=red,
center=ball_obj.center, radius=40)
pygame.display.flip()

20
Output:

21

You might also like