0% found this document useful (0 votes)
74 views21 pages

Python Programming Lab Report CSE3011

Uploaded by

thilakraaj47
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views21 pages

Python Programming Lab Report CSE3011

Uploaded by

thilakraaj47
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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:
[Link](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):
[Link]([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=[Link]()
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 :")
[Link](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 [Link]()
def preprocess_text(text):
text = [Link](r'[^\w\s]','',text)
text = [Link]()
return text
def get_most_frequent_words(text, n=10):
words = [Link]()
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\\[Link]"

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()
[Link]()

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

[Link]()
width = 1000
height = 600
screen_res = (width, height)
[Link].set_caption("GFG Elliptical orbit")
screen = [Link].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 = [Link]()
while True:
for degree in range(0, 360, 1):
for event in [Link]():
if [Link] == [Link]:
exit()
[Link]([0, 0, 0])

x_planet_1 = int([Link](degree * 2 * [Link]/360)


* X_ellipse) + X_center

y_planet_1 = int([Link](degree * 2 * [Link]/360)


* Y_ellipse) + Y_center

degree_2 = degree+180
if degree > 180:
degree_2 = degree-180
x_planet_2 = int([Link](degree_2 * 2 * [Link]/360)
* X_ellipse) + X_center

y_planet_2 = int([Link](degree_2 * 2 * [Link]/360)* Y_ellipse) +


Y_center

16
[Link](surface=screen, color=red, center=[
X_center, Y_center], radius=60)

[Link](surface=screen, color=green,
rect=[100, 75, 800, 450], width=1)

[Link](surface=screen, color=blue, center=[


x_planet_1, y_planet_1], radius=40)
[Link](surface=screen, color=cyan, center=[
x_planet_2, y_planet_2], radius=40)

[Link](5)
[Link]()

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
[Link]()
width = 1000
height = 600
screen_res = (width, height)

[Link].set_caption("GFG Bouncing game")


screen = [Link].set_mode(screen_res)
red = (255, 0, 0)
black = (0, 0, 0)
ball_obj = [Link](
surface=screen, color=red, center=[100, 100], radius=40)
speed = [1, 1]
while True:
for event in [Link]():
if [Link] == [Link]:
19
exit()
[Link](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]
[Link](surface=screen, color=red,
center=ball_obj.center, radius=40)
[Link]()

20
Output:

21

Common questions

Powered by AI

The process of multiplying two matrices involves iterating over each element of the result matrix, where for each element at position (i, j), the sum of the products of the elements of the ith row of the first matrix and the jth column of the second matrix is calculated. The resulting matrix for A = [[1, 2, 3], [4, 5, 6]] and B = [[7, 8], [9, 10], [11, 12]] is [[58, 64], [139, 154]]. This is done by performing the following operations: for the first element 58, ((1*7) + (2*9) + (3*11)), and so on for each element in the result matrix .

The program simulates an elliptical orbit using Pygame by calculating x and y positions of elliptical paths for planetary objects using trigonometric functions. The key variables include X_center and Y_center for the center of the ellipse, X_ellipse and Y_ellipse for the radii of the ellipse in x and y directions, and degree to iterate through angles 0 to 360 to plot points around the ellipse. It uses the cos and sin functions to determine x and y positions of the orbiting objects, adjusts for the screen coordinates, and dynamically updates the graphics in the game window with each loop iteration .

The code structure creates a class named 'A' and defines a method named 'view' within it, which simply prints a message. An object of this class is instantiated with the statement 'b = A()'. Then, the 'view' method is called on this object using 'b.view()' to execute and print the message "This is a simple class!" to the console .

The function is defined to accept a single string parameter and prints that parameter. During invocation, the user is prompted to input a string, which is passed to the function. The function then prints the input string to demonstrate that it can dynamically handle various input values and return exactly what was entered. This allows any string given as input to be directly printed, showcasing basic string handling within a function in Python .

The simulation uses Pygame to draw a red ball that moves across the screen. The key components include defining the screen size, the ball’s initial position, its speed, and the mechanism to detect collisions with the screen edges. As the ball moves, its position is updated based on the speed. When it hits the boundary, the direction of its velocity is reversed using conditional statements, simulating a bounce by inverting the corresponding speed component. These mechanics ensure realistic movement within the defined space .

The program dynamically handles strings by first asking the user how many strings they want to enter. It then uses a loop to input each string and appends each string to a list named 'a'. After all strings are collected, the program iterates through the list and prints each string to the console. This approach allows for dynamic input and storage in Python lists, facilitating easy manipulation and access to the stored data .

The method used in the program involves reading the text from a file, preprocessing the text to remove punctuation and convert all words to lowercase, and then splitting the text into individual words. The program uses Python's Counter class from the collections module to count the frequency of each word. It then identifies the top n most frequent words using the most_common method, which returns the n most common elements and their counts from the most common to the least. This list is printed to the console .

The program identifies and prints the first n prime numbers by using a while loop that continues until n prime numbers are found. It checks each number starting from 2 to see if it can be divided evenly by any number up to its square root. If it cannot be divided by any of these, it is prime and added to the list of primes. The program uses a counter to keep track of how many primes have been found and increments the counter each time a prime is added to the list .

The recommended approach involves prompting the user to input an ID and a password, which are then compared against predefined values stored in variables (e.g., 'VS' for ID and 'hello@123' for password). If the input matches these values, the program prints a welcome message. While this offers basic authentication, the example lacks security practices such as password hashing or the use of secure storage mechanisms, making it unsuitable for real-world applications without further enhancement .

The program prompts the user to input a line of text, splits the input string into words using the split() method, which divides the string by spaces, and then counts the total number of elements in the resulting list using len(). This count reflects the number of words in the input string, which is printed out to the user as the word count result .

You might also like