0% found this document useful (0 votes)
10 views7 pages

Python Lab

Uploaded by

amanp96884
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)
10 views7 pages

Python Lab

Uploaded by

amanp96884
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
You are on page 1/ 7

Experiment No:- 12 To write a python program for Command Line Arguments.

import sys

def main():
# sys.argv[0] is the script name, arguments start from sys.argv[1]
if len(sys.argv) < 2:
print("Usage: python script.py <arg1> <arg2> ...")
return

print("Script Name:", sys.argv[0])


print("Arguments:", sys.argv[1:])

# Example: Summing two numbers passed as arguments


if len(sys.argv) >= 3:
try:
num1 = float(sys.argv[1])
num2 = float(sys.argv[2])
print(f"Sum of {num1} and {num2} is {num1 + num2}")
except ValueError:
print("Please provide valid numbers for addition.")

if __name__ == "__main__":
main()
Experiment No:- 13 To write a python program to find the most frequent words
in a text read from a file.

from collections import Counter


import string

def find_most_frequent_words(file_path, top_n=5):


try:
# Read the file
with open(file_path, 'r') as file:
text = file.read()

# Preprocess the text: Remove punctuation and convert to lowercase


translator = str.maketrans('', '', string.punctuation)
text = text.translate(translator).lower()

# Tokenize the text into words


words = text.split()

# Count the occurrences of each word


word_counts = Counter(words)

# Find the most frequent words


most_common_words = word_counts.most_common(top_n)

print(f"Top {top_n} most frequent words:")


for word, count in most_common_words:
print(f"{word}: {count}")

except FileNotFoundError:
print(f"Error: The file '{file_path}' does not exist.")
except Exception as e:
print(f"An error occurred: {e}")

# Specify the file path and top N words


file_path = "sample.txt" # Replace with your file path
top_n = 5
find_most_frequent_words(file_path, top_n)
Experiment No:- 14 To write a python program to simulate elliptical orbits in
Pygame.
import math
import pygame
import sys

# Initialize Pygame
pygame.init()

# Screen dimensions
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Elliptical Orbit Simulation")

# Colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
YELLOW = (255, 255, 0)
BLUE = (0, 0, 255)

# Clock for frame rate control


clock = pygame.time.Clock()

# Ellipse properties
center_x, center_y = WIDTH // 2, HEIGHT // 2 # Center of the ellipse
semi_major_axis = 200 # Length of the semi-major axis
semi_minor_axis = 100 # Length of the semi-minor axis
orbit_speed = 0.02 # Speed of the orbit

# Angle for orbiting body


angle = 0

# Game loop
while True:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()

# Update angle
angle += orbit_speed
if angle >= 2 * math.pi:
angle -= 2 * math.pi

# Calculate planet position


planet_x = center_x + semi_major_axis * math.cos(angle)
planet_y = center_y + semi_minor_axis * math.sin(angle)

# Clear the screen


screen.fill(BLACK)

# Draw the elliptical orbit


pygame.draw.ellipse(screen, WHITE,
(center_x - semi_major_axis, center_y - semi_minor_axis,
2 * semi_major_axis, 2 * semi_minor_axis), 1)

# Draw the central object (e.g., the Sun)


pygame.draw.circle(screen, YELLOW, (center_x, center_y), 10)

# Draw the orbiting object (e.g., the planet)


pygame.draw.circle(screen, BLUE, (int(planet_x), int(planet_y)), 8)

# Update the display


pygame.display.flip()

# Control the frame rate


clock.tick(60)
Experiment No:- 15 To write a python program to bouncing Ball in Pygame.
import pygame
import sys

# Initialize pygame
pygame.init()

# Screen dimensions
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Bouncing Ball")

# Colors
WHITE = (255, 255, 255)
RED = (255, 0, 0)

# Ball properties
ball_radius = 20
ball_x, ball_y = WIDTH // 2, HEIGHT // 2
ball_speed_x, ball_speed_y = 4, 4

# Clock for controlling the frame rate


clock = pygame.time.Clock()

# Game loop
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()

# Update ball position


ball_x += ball_speed_x
ball_y += ball_speed_y

# Bounce off walls


if ball_x - ball_radius < 0 or ball_x + ball_radius > WIDTH:
ball_speed_x = -ball_speed_x
if ball_y - ball_radius < 0 or ball_y + ball_radius > HEIGHT:
ball_speed_y = -ball_speed_y

# Clear the screen


screen.fill(WHITE)
# Draw the ball
pygame.draw.circle(screen, RED, (ball_x, ball_y), ball_radius)

# Update the display


pygame.display.flip()

# Control the frame rate


clock.tick(60)

You might also like