Lab Report Python
Lab Report Python
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
1
INDEX
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
5
print("Result of matrix multiplication:")
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:")
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])
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
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)
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)
20
Output:
21