0% found this document useful (0 votes)
4 views

Python Programs With Explanations

Uploaded by

mhmdmfsr07
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Python Programs With Explanations

Uploaded by

mhmdmfsr07
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Python Programs with Explanations

1. Calculator Program
This program allows users to perform basic arithmetic operations (addition, subtraction,
multiplication, and division) on two numbers. It also includes error handling to manage
invalid inputs and ensures division by zero does not crash the program.

Code:

# Calculator Program in Python by using input() and format() functions

# Prompting input from the user with error handling


try:
n1 = float(input("Enter the First Number: "))
n2 = float(input("Enter the Second Number: "))

# Addition
print("{} + {} = ".format(n1, n2))
print(n1 + n2)

# Subtraction
print("{} - {} = ".format(n1, n2))
print(n1 - n2)

# Multiplication
print("{} * {} = ".format(n1, n2))
print(n1 * n2)

# Division with zero check


if n2 != 0:
print("{} / {} = ".format(n1, n2))
print(n1 / n2)
else:
print("Division by zero is not allowed.")

except ValueError:
print("Invalid input! Please enter numeric values.")
2. Student Data Processing
This program processes student data, calculates total marks and averages for each student,
and displays the results along with class averages. It uses lists to store and manipulate data,
and iterates through the sample data provided.

Code:

# Create empty lists to store student details


names = []
math_marks = []
english_marks = []
it_marks = []
totals = []
averages = []

# Sample data
test_data = [
["John", 85, 90, 88],
["Emma", 92, 88, 95],
["Mike", 78, 85, 80],
["Sarah", 95, 92, 91],
["Tom", 88, 84, 89],
["Lisa", 90, 91, 87],
["David", 82, 88, 85],
["Anna", 94, 89, 92],
["James", 87, 83, 86],
["Mary", 91, 90, 88]
]

# Process student data


for student in test_data:
# Get student details from test data
name, math, english, it = student

# Store in lists
names.append(name)
math_marks.append(math)
english_marks.append(english)
it_marks.append(it)

# Calculate and store total and average


total = math + english + it
average = total / 3
totals.append(total)
averages.append(average)

# Display results
print("\n--- Student Details ---")
print("Name\t\tMath\tEnglish\tIT\tTotal\tAverage")
print("-" * 50)

for i in range(len(names)):
print(f"{names[i]}\t\t{math_marks[i]}\t{english_marks[i]}\t{it_marks[i]}\t{totals[i]}\
t{averages[i]:.1f}")

# Display class averages


print("\n--- Class Averages ---")
print(f"Math Average: {sum(math_marks)/len(math_marks):.1f}")
print(f"English Average: {sum(english_marks)/len(english_marks):.1f}")
print(f"IT Average: {sum(it_marks)/len(it_marks):.1f}")

3. Displaying Topper
This program identifies the student with the highest total marks from a list of students and
displays their details. It uses a loop to iterate through the student data and keeps track of
the highest total marks encountered.

Code:

# Sample data
students = [
["John", 85, 90, 88],
["Emma", 92, 88, 95],
["Mike", 78, 85, 80],
["Sarah", 95, 92, 91],
["Tom", 88, 84, 89],
["Lisa", 90, 91, 87],
["David", 82, 88, 85],
["Anna", 94, 89, 92],
["James", 87, 83, 86],
["Mary", 91, 90, 88]
]

print("--- Student Details ---")


print("Name\t\tMath\tEnglish\tIT\tTotal")
print("-" * 50)
highest_total = 0
topper_name = ""

# Display all students and find topper


for student in students:
name, math, english, it = student
total = math + english + it

print(f"{name}\t\t{math}\t{english}\t{it}\t{total}")

# Check if this student has the highest marks


if total > highest_total:
highest_total = total
topper_name = name

print("\n=== First Rank ===")


print(f"Name: {topper_name}")
print(f"Total Marks: {highest_total}")

You might also like