0% found this document useful (0 votes)
4 views5 pages

Python Programming Assignment Solutions

The document outlines a major lab assignment with various programming tasks in Python, including input validation, calculations for leap years, triangle validity, electricity bills, income tax, prime number generation, and SGPA calculation. Each task includes example code snippets and expected outputs. The assignment is structured into three parts: Warm-Up, Core Problems, and Challenge Zone.

Uploaded by

Dipuna Mohanty
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)
4 views5 pages

Python Programming Assignment Solutions

The document outlines a major lab assignment with various programming tasks in Python, including input validation, calculations for leap years, triangle validity, electricity bills, income tax, prime number generation, and SGPA calculation. Each task includes example code snippets and expected outputs. The assignment is structured into three parts: Warm-Up, Core Problems, and Challenge Zone.

Uploaded by

Dipuna Mohanty
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

Assignment 3

Name: Ranveer Singh

Registration No.: 24E119G29

Branch: ECE (24E1T1)

Major Lab Assignment : 3

Part A– Warm-Up
A.1 Input a number and print whether it is positive, negative, or zero

In [1]: num = float(input("Enter a number: "))


if num > 0:
print("The number is positive.")
elif num < 0:
print("The number is negative.")
else:
print("The number is zero.")

The number is positive.

A.2 Input two integers and print the larger one

In [2]: a = int(input("Enter first integer: "))


b = int(input("Enter second integer: "))
print("The larger number is:", a if a > b else b)

The larger number is: 12

Part B– Core Problems


B.1 Odd or Even
Write a Python program that reads an integer from the user and separately
calculates the sum of its odd digits and the sum of its even digits.

In [3]: num = input("Enter an integer: ")


sum_even = 0
sum_odd = 0
for digit in num:
if [Link]():
d = int(digit)
if d % 2 == 0:
sum_even += d
else:
sum_odd += d
print("Sum of even digits:", sum_even)
print("Sum of odd digits:", sum_odd)

Sum of even digits: 12


Sum of odd digits: 9

B.2 Leap Year Check


Input a year and determine whether it is a leap year. standard rule for Leap
Year: divisible by 400 OR divisible by 4 but not by 100.

In [4]: year = int(input("Enter a year: "))


if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0):
print("It is a leap year.")
else:
print("It is not a leap year.")

It is a leap year.

B.3 Triangle Validity


Input three sides of a triangle.

Check whether the sides form a valid triangle.

If valid, check whether it is equilateral, isosceles, or scalene.

For validity: In a triangle sum of any two sides is greater than the third side.–

equilateral: All sides are equal.–

isosceles: Two sides are equal.–

scalene: Three sides have different lengths

In [6]: a = float(input("Enter side a: "))


b = float(input("Enter side b: "))
c = float(input("Enter side c: "))
if a + b > c and a + c > b and b + c > a:
print("Valid triangle.")
if a == b == c:
print("It is an equilateral triangle.")
elif a == b or b == c or a == c:
print("It is an isosceles triangle.")
else:
print("It is a scalene triangle.")
else:
print("Invalid triangle.")

Valid triangle.
It is a scalene triangle.

B.4 Electricity Bill


Input number of units consumed.
Compute bill according to these rules:–

First 100 units: Rs.5 per unit–

Next 200 units: Rs.7 per unit–

Beyond 300 units: Rs.10 per uni

In [7]: units = int(input("Enter number of units consumed: "))


if units <= 100:
bill = units * 5
elif units <= 300:
bill = 100 * 5 + (units - 100) * 7
else:
bill = 100 * 5 + 200 * 7 + (units - 300) * 10
print("Electricity bill: Rs.", bill)

Electricity bill: Rs. 2570

B.5 Income Tax Calculation


Input annual income and compute tax:

Up to 2.5 lakh: no tax

2.5–5 lakh: 5%

5–10 lakh: 20%

Above 10 lakh: 30%

In [8]: income = float(input("Enter your annual income (in ₹):"))


tax = 0.0
if income <= 250000:
tax = 0
elif income <= 500000:
tax = (income - 250000) * 0.05
elif income <= 1000000:
tax = (250000 * 0.05) + (income - 500000) * 0.20
else:
tax = (250000 * 0.05) + (500000 * 0.20) + (income - 1000000) * 0.30
print("Income Tax Payable: ₹", tax)

Income Tax Payable: ₹ 262500.0

Part C– Challenge Zone


C.1 Prime Numbers
Write a Python program to generate and display the first 50 prime numbers.

To optimize, check divisibility only up to the square root of the number.

Use a counter to keep track of how many prime numbers have been generated.

Stop when you have printed the first 50 prime numbers.


Display the numbers separated by commas on a single line

In [9]: import math

def is_prime(n):
if n < 2:
return False
for i in range(2, int([Link](n)) + 1):
if n % i == 0:
return False
return True

count = 0
num = 2
primes = []

while count < 50:


if is_prime(num):
[Link](str(num))
count += 1
num += 1

print(", ".join(primes))

2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79,
83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 17
3, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229

C.2 SGPA Calculation


Input percentage marks of 6 subjects with different credit points (ci). PS→4, IMC→3,
CA→2, CT→4, SCWP→4, MPC→4.

Assign grades to each subject: O (≥ 90), A (80–89), B (70–79), C (60–69), D(50–59), E (<
50).

The grade points (gi) for each grade are as follows: O→10, A→9.5, B→8.5, C→7.5, D→6.5,
E→5.5.

Calculate the SGPA = ∑ (Ci X gi)/∑ ci

In [1]: credits = {
"PS": 4,
"IMC": 3,
"CA": 2,
"CT": 4,
"SCWP": 4,
"MPC": 4
}
def get_grade_and_point(percentage):
if percentage >= 90:
return "O", 10
elif percentage >= 80:
return "A", 9.5
elif percentage >= 70:
return "B", 8.5
elif percentage >= 60:
return "C", 7.5
elif percentage >= 50:
return "D", 6.5
else:
return "E", 5.5
marks = {}
grades = {}
grade_points = {}
for subject in credits:
percent = float(input(f"Enter percentage marks for {subject}: "))
grade, point = get_grade_and_point(percent)
marks[subject] = percent
grades[subject] = grade
grade_points[subject] = point
total_weighted_points = 0
total_credits = 0
print("\nSubject-wise Grade Report:")
print(f"{'Subject':<6} {'Marks':>6} {'Grade':>8} {'Grade Point':>13}")
print("-" * 36)
for subject in credits:
ci = credits[subject]
gi = grade_points[subject]
total_weighted_points += ci * gi
total_credits += ci
print(f"{subject:<6} {marks[subject]:>6.2f} {grades[subject]:>8} {gi:>13.2f}")
sgpa = total_weighted_points / total_credits
print(f"\nYour SGPA is: {sgpa:.2f}")

Subject-wise Grade Report:


Subject Marks Grade Grade Point
------------------------------------
PS 85.00 A 9.50
IMC 72.00 B 8.50
CA 91.00 O 10.00
CT 67.00 C 7.50
SCWP 58.00 D 6.50
MPC 45.00 E 5.50

Your SGPA is: 7.69

In [ ]:

You might also like