Model Python Practical

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Model python practical

Here are all 1 to 20 Python programs combined for your reference:

---

1. Create, append, and remove lists in Python

# 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)

---

2. Working with tuples

# Create a tuple
my_tuple = (1, 2, 3, "hello", True)
print("Original Tuple:", my_tuple)

# Accessing tuple elements


print("First Element:", my_tuple[0])
print("Last Element:", my_tuple[-1])

---

3. Working with dictionaries

# Create a dictionary
my_dict = {"name": "John", "age": 25, "city": "New York"}

# Access value
print("Name:", my_dict["name"])

# Add a new key-value pair


my_dict["country"] = "USA"
print("Updated Dictionary:", my_dict)

# Remove a key-value pair


del my_dict["age"]
print("After Removing 'age':", my_dict)

---

4. Find the largest of three numbers

# Input three numbers


a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))

# Find the largest


largest = max(a, b, c)
print("The largest number is:", largest)

---

5. Convert temperature to and from Celsius/Fahrenheit

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()

---

6–8. Real-life problems

These programs will need formulas. Here's an Electricity Billing Example:

# Electricity Billing
units = int(input("Enter the number of units consumed: "))

if units <= 100:


cost = units * 1.5
elif units <= 200:
cost = (100 * 1.5) + (units - 100) * 2.5
else:
cost = (100 * 1.5) + (100 * 2.5) + (units - 200) * 4

print("Total Electricity Bill: ₹", cost)

---

9. Exchange values of two variables

a=5
b = 10

# Swap values
a, b = b, a
print("Exchanged values: a =", a, ", b =", b)

---

10. Circulate the values of 'n' variables

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))

---

11. Number series and patterns

Number Series (Natural numbers):

for i in range(1, 11):


print(i, end=" ")

Number Patterns (Triangle):

rows = 5
for i in range(1, rows + 1):
print("*" * i)

---

12. Pyramid pattern

rows = 5
for i in range(1, rows + 1):
print(" " * (rows - i) + "*" * (2 * i - 1))
---

13. Real-time application (Items in Library)

library_books = [("Book1", "Author1"), ("Book2", "Author2"), ("Book3", "Author3")]

print("Books in the Library:")


for book in library_books:
print(f"Title: {book[0]}, Author: {book[1]}")

---

14. Components of a car

car_components = ("Engine", "Transmission", "Wheels", "Brakes", "Suspension")

print("Components of a Car:")
for component in car_components:
print("-", component)

---

15. Building material list

materials = [("Cement", 50), ("Bricks", 1000), ("Sand", 20)]

print("Materials for Construction:")


for item, quantity in materials:
print(f"{item}: {quantity} units")

---

16. Word count and longest word

text = input("Enter a sentence: ")


words = text.split()

print("Number of words:", len(words))


longest_word = max(words, key=len)
print("Longest word:", longest_word)

---

17. Explore Pygame tool

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()

---

18. Convert integer to Roman numeral

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

number = int(input("Enter an integer: "))


print("Roman Numeral:", int_to_roman(number))

---

19. Game activity (Bouncing Ball with Pygame)

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

if x - radius < 0 or x + radius > 500:


dx = -dx
if y - radius < 0 or y + radius > 400:
dy = -dy

pygame.draw.circle(screen, (255, 0, 0), (x, y), radius)


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

pygame.quit()

---

20. Game activity (Car Race with Pygame)

import pygame
pygame.init()

# Screen setup
screen = pygame.display.set_mode((500, 600))
pygame.display.set_caption("Car Race")
clock = pygame.time.Clock()

car_x, car_y = 220, 500


car_speed = 5

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.draw.rect(screen, (255, 0, 0), (car_x, car_y, 50, 100))


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

pygame.quit()

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy