0% found this document useful (0 votes)
17 views9 pages

High Priority Programs

Uploaded by

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

High Priority Programs

Uploaded by

rijodev06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

1.

Python program to create, append, and remove lists:


PYTHON

# Create a list
my_list = [1, 2, 3]
print("Original list:", my_list)

# Append an element to the list


my_list.append(4)
print("List after appending 4:", my_list)

# Remove an element from the list


my_list.remove(2)
print("List after removing 2:", my_list)

2. Program to demonstrate working with tuples in Python:


PYTHON

# Create a tuple
my_tuple = (10, 20, 30)
print("Original tuple:", my_tuple)

# Access elements in a tuple


print("First element:", my_tuple[0])

# Tuple concatenation
new_tuple = my_tuple + (40, 50)
print("Concatenated tuple:", new_tuple)

3. Program to demonstrate working with dictionaries in Python:


PYTHON

# Create a dictionary
my_dict = {"name": "Alice", "age": 25, "city": "New York"}
print("Original dictionary:", my_dict)

# Add a new key-value pair


my_dict["country"] = "USA"
print("After adding 'country':", my_dict)

# Update a value
my_dict["age"] = 26
print("After updating 'age':", my_dict)

# Remove a key-value pair


del my_dict["city"]
print("After removing 'city':", my_dict)
4. Python program to find the largest of three numbers:
PYTHON

def find_largest(a, b, c):


return max(a, b, c)

num1 = 10
num2 = 20
num3 = 15
largest = find_largest(num1, num2, num3)
print("The largest number is:", largest)

5. Python program to convert temperature to and from Celsius and Fahrenheit:


PYTHON

def celsius_to_fahrenheit(celsius):
return (celsius * 9/5) + 32

def fahrenheit_to_celsius(fahrenheit):
return (fahrenheit - 32) * 5/9

temp_in_celsius = 25
temp_in_fahrenheit = 77

print(f"{temp_in_celsius}°C to Fahrenheit:", celsius_to_fahrenheit(temp_in_celsius))


print(f"{temp_in_fahrenheit}°F to Celsius:", fahrenheit_to_celsius(temp_in_fahrenheit))

6. Compute Electrical Current in a Three-Phase AC Circuit


Program to calculate current in a three-phase circuit based on power and voltage:

PYTHON

def calculate_current(power, voltage, power_factor=0.8):


current = power / (voltage * power_factor * (3**0.5))
return current

power = 10000 # in watts


voltage = 400 # in volts
current = calculate_current(power, voltage)
print(f"Electrical current in the circuit: {current:.2f} A")

7. Exchange the Values of Two Variables


Program to swap the values of two variables:
PYTHON

a = 10
b = 20
print("Before swapping: a =", a, ", b =", b)

# Swapping values
a, b = b, a
print("After swapping: a =", a, ", b =", b)

8. Distance Between Two Points


Program to calculate the distance between two points in 2D space:

PYTHON

import math

def distance_between_points(x1, y1, x2, y2):


return math.sqrt((x2 - x1)**2 + (y2 - y1)**2)

x1, y1 = 1, 2
x2, y2 = 4, 6
distance = distance_between_points(x1, y1, x2, y2)
print(f"Distance between points ({x1}, {y1}) and ({x2}, {y2}): {distance:.2f}")

9. Circulate the Values of N Variables


Program to circulate the values of a list of variables:

PYTHON

def circulate_values(values):
# Shift the last element to the first position
values.insert(0, values.pop())
return values

values = [10, 20, 30, 40]


print("Before circulation:", values)
values = circulate_values(values)
print("After circulation:", values)

10. Distance Between Two Points (Alternative)


Program to calculate distance for a 3D space:
PYTHON

def distance_3d(x1, y1, z1, x2, y2, z2):


return math.sqrt((x2 - x1)**2 + (y2 - y1)**2 + (z2 - z1)**2)

x1, y1, z1 = 1, 2, 3
x2, y2, z2 = 4, 6, 8
distance = distance_3d(x1, y1, z1, x2, y2, z2)
print(f"Distance between 3D points: {distance:.2f}")

11. Number Series (Fibonacci Sequence)


Program to generate a Fibonacci series using conditionals and iterative loops:

PYTHON

def generate_fibonacci(n):
series = [0, 1]
while len(series) < n:
series.append(series[-1] + series[-2])
return series

n = 10 # Number of terms
fibonacci_series = generate_fibonacci(n)
print("Fibonacci series:", fibonacci_series)

12. Number Patterns


Program to display a number pattern using loops:

PYTHON

def number_pattern(rows):
for i in range(1, rows + 1):
for j in range(1, i + 1):
print(j, end=" ")
print()

rows = 5
number_pattern(rows)

13. Pyramid Pattern


Program to display a pyramid pattern:

PYTHON

def pyramid_pattern(rows):
for i in range(1, rows + 1):
print(" " * (rows - i) + "* " * i)

rows = 5
pyramid_pattern(rows)
14. Items Present in a Library Using Lists and Tuples
Program to manage a library's inventory:

PYTHON

# Using a list to store books


library_books = ["Python Programming", "Data Structures", "Machine Learning"]
print("Library books:", library_books)

# Adding a new book


library_books.append("Distributed Systems")
print("Updated library books:", library_books)

# Using a tuple to represent immutable details


book_details = ("Python Programming", "John Doe", "2023")
print("Book details:", book_details)

15. Components of a Car Using Lists and Tuples


Program to list components of a car:

PYTHON

# List for mutable components


car_components = ["Engine", "Tires", "Battery", "Steering"]
print("Car components:", car_components)

# Adding a new component


car_components.append("Brakes")
print("Updated car components:", car_components)

# Tuple for fixed details


engine_details = ("V8 Engine", "500 HP", "Petrol")
print("Engine details:", engine_details)

16. Materials Required for Construction of a Building (Operations on Lists and Tuples)
Program to manage building materials:
PYTHON

# List for materials


materials_list = ["Bricks", "Cement", "Sand", "Steel"]
print("Building materials:", materials_list)

# Adding a new material


materials_list.append("Concrete")
print("Updated materials list:", materials_list)

# Tuple for fixed material details


steel_details = ("Steel Rod", "12mm", "500kg")
print("Steel details:", steel_details)

16. Word Count and Longest Word Using Lists


Program to count words in a sentence and find the longest word:

PYTHON

def word_count_and_longest(sentence):
words = sentence.split()
word_count = len(words)
longest_word = max(words, key=len)
return word_count, longest_word

sentence = "Python programming is both powerful and fun"


word_count, longest_word = word_count_and_longest(sentence)
print("Word count:", word_count)
print("Longest word:", longest_word)

17. Explore Pygame Tool


Program to set up a simple Pygame window:
PYTHON

import pygame

# Initialize Pygame
pygame.init()

# Set up the display


screen = pygame.display.set_mode((500, 400))
pygame.display.set_caption("Pygame Exploration")

# Main loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False

# Quit Pygame
pygame.quit()

18. Convert Integer to Roman Numerals


Python class to convert an integer to a Roman numeral:

PYTHON

class IntegerToRoman:
def __init__(self):
self.numerals = {
1000: 'M', 900: 'CM', 500: 'D', 400: 'CD',
100: 'C', 90: 'XC', 50: 'L', 40: 'XL',
10: 'X', 9: 'IX', 5: 'V', 4: 'IV', 1: 'I'
}

def int_to_roman(self, num):


result = ""
for value, numeral in self.numerals.items():
while num >= value:
result += numeral
num -= value
return result

converter = IntegerToRoman()
number = 3549
roman_numeral = converter.int_to_roman(number)
print(f"{number} in Roman numeral is {roman_numeral}")

19. Bouncing Ball Game Using Pygame


Program to develop a simple bouncing ball game:
PYTHON

import pygame

# Initialize Pygame
pygame.init()

# Screen settings
screen = pygame.display.set_mode((600, 400))
pygame.display.set_caption("Bouncing Ball")

# Colors and ball settings


white = (255, 255, 255)
red = (255, 0, 0)
ball_pos = [300, 200]
ball_radius = 20
ball_speed = [2, 2]

# Main loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False

# Update ball position


ball_pos[0] += ball_speed[0]
ball_pos[1] += ball_speed[1]

# Bounce off walls


if ball_pos[0] - ball_radius <= 0 or ball_pos[0] + ball_radius >= 600:
ball_speed[0] = -ball_speed[0]
if ball_pos[1] - ball_radius <= 0 or ball_pos[1] + ball_radius >= 400:
ball_speed[1] = -ball_speed[1]

# Draw everything
screen.fill(white)
pygame.draw.circle(screen, red, ball_pos, ball_radius)
pygame.display.flip()
pygame.time.delay(10)

pygame.quit()

20. Car Race Game Using Pygame


Program to develop a simple car race game:
PYTHON

import pygame

# Initialize Pygame
pygame.init()

# Screen settings
screen = pygame.display.set_mode((600, 400))
pygame.display.set_caption("Car Race")

# Colors and car settings


black = (0, 0, 0)
blue = (0, 0, 255)
car_pos = [280, 300]
car_speed = 5

# Main loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False

# Move the car


keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and car_pos[0] > 0:
car_pos[0] -= car_speed
if keys[pygame.K_RIGHT] and car_pos[0] < 540:
car_pos[0] += car_speed

# Draw the screen


screen.fill(black)
pygame.draw.rect(screen, blue, (*car_pos, 60, 100)) # Car rectangle
pygame.display.flip()
pygame.time.delay(30)

pygame.quit()

You might also like