Model Python Practical
Model Python Practical
Model Python Practical
---
# Create a list
my_list = [1, 2, 3, 4, 5]
print("Original List:", my_list)
# Append elements
my_list.append(6)
print("After Appending 6:", my_list)
# Remove an element
my_list.remove(3)
print("After Removing 3:", my_list)
---
# Create a tuple
my_tuple = (1, 2, 3, "hello", True)
print("Original Tuple:", my_tuple)
---
# Create a dictionary
my_dict = {"name": "John", "age": 25, "city": "New York"}
# Access value
print("Name:", my_dict["name"])
---
---
def convert_temperature():
choice = input("Convert to (F)ahrenheit or (C)elsius? ").lower()
if choice == 'f':
celsius = float(input("Enter temperature in Celsius: "))
fahrenheit = (celsius * 9/5) + 32
print("Temperature in Fahrenheit:", fahrenheit)
elif choice == 'c':
fahrenheit = float(input("Enter temperature in Fahrenheit: "))
celsius = (fahrenheit - 32) * 5/9
print("Temperature in Celsius:", celsius)
else:
print("Invalid choice")
convert_temperature()
---
# Electricity Billing
units = int(input("Enter the number of units consumed: "))
---
a=5
b = 10
# Swap values
a, b = b, a
print("Exchanged values: a =", a, ", b =", b)
---
def circulate_list(lst):
return lst[-1:] + lst[:-1]
my_list = [1, 2, 3, 4, 5]
print("Original List:", my_list)
print("After Circulation:", circulate_list(my_list))
---
rows = 5
for i in range(1, rows + 1):
print("*" * i)
---
rows = 5
for i in range(1, rows + 1):
print(" " * (rows - i) + "*" * (2 * i - 1))
---
---
print("Components of a Car:")
for component in car_components:
print("-", component)
---
---
---
import pygame
pygame.init()
screen = pygame.display.set_mode((400, 300))
pygame.display.set_caption("Explore Pygame")
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
---
def int_to_roman(num):
roman = {1: 'I', 4: 'IV', 5: 'V', 9: 'IX', 10: 'X', 40: 'XL', 50: 'L',
90: 'XC', 100: 'C', 400: 'CD', 500: 'D', 900: 'CM', 1000: 'M'}
result = ''
for value, symbol in sorted(roman.items(), reverse=True):
while num >= value:
result += symbol
num -= value
return result
---
import pygame
pygame.init()
# Screen setup
screen = pygame.display.set_mode((500, 400))
pygame.display.set_caption("Bouncing Ball")
clock = pygame.time.Clock()
# Ball setup
x, y = 250, 200
dx, dy = 5, 5
radius = 20
running = True
while running:
screen.fill((0, 0, 0)) # Black background
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
x += dx
y += dy
pygame.quit()
---
import pygame
pygame.init()
# Screen setup
screen = pygame.display.set_mode((500, 600))
pygame.display.set_caption("Car Race")
clock = pygame.time.Clock()
running = True
while running:
screen.fill((0, 255, 0)) # Green background
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and car_x > 0:
car_x -= car_speed
if keys[pygame.K_RIGHT] and car_x < 450:
car_x += car_speed
pygame.quit()