0% found this document useful (0 votes)
3 views8 pages

Python Assignment Solutions

Uploaded by

aayushyadav1096
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)
3 views8 pages

Python Assignment Solutions

Uploaded by

aayushyadav1096
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

Python Programming Solutions

Comprehensive Solved Assignment • Core Control Flow & Data Structures

Program 1: Number Sign Checker

Write a Python program to check whether a number entered by the user is positive, negative, or zero using
if-else.

# Input from the user


num = float(input("Enter a number: "))

# Checking conditions using if-elif-else


if num > 0:
print("The number is Positive")
elif num < 0:
print("The number is Negative")
else:
print("The number is Zero")

Example Output:

Enter a number: -5.5


The number is Negative

Program 2: Even or Odd Checker

Write a Python program to check whether a given number is even or odd.

# Input from the user


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

# A number is even if it is perfectly divisible by 2


if num % 2 == 0:
print(f"{num} is Even")
else:
print(f"{num} is Odd")

Example Output:

Enter an integer: 47
47 is Odd

Python Programming Foundation Assignment Page 1 of 8


Program 3: Greatest of Three Numbers

Write a Python program to find the greatest among three numbers using if-elif-else.

# Taking three numbers as input


num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
num3 = float(input("Enter third number: "))

# Finding the greatest number


if num1 >= num2 and num1 >= num3:
greatest = num1
elif num2 >= num1 and num2 >= num3:
greatest = num2
else:
greatest = num3

print(f"The greatest number is: {greatest}")

Example Output:

Enter first number: 12


Enter second number: 45
Enter third number: 32
The greatest number is: 45.0

Program 4: Student Result Status

Write a Python program to check whether a student has passed or failed. (Passing marks = 33)

# Input marks from user


marks = float(input("Enter the marks obtained: "))

# Checking pass/fail threshold


if marks >= 33:
print("Result: PASSED")
else:
print("Result: FAILED")

Example Output:

Enter the marks obtained: 32.5


Result: FAILED

Python Programming Foundation Assignment Page 2 of 8


Program 5: Electricity Bill Calculator

Write a Python program to calculate the electricity bill based on units consumed:
• Units ≤ 100 → ₹5/unit | • Units > 100 and ≤ 300 → ₹7/unit | • Units > 300 → ₹10/unit

# Input total units consumed


units = float(input("Enter electricity units consumed: "))

# Calculation based on slab rates


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(f"Total Electricity Bill: ₹{bill:.2f}")

Example Output:

Enter electricity units consumed: 350


Total Electricity Bill: ₹2400.00

Python Programming Foundation Assignment Page 3 of 8


Program 6: Extremums in a List

Write a Python program to input a list of 10 numbers and print the largest and smallest numbers.

# Initializing an empty list


numbers = []

# Gathering 10 numbers from the user


print("Enter 10 numbers sequence:")
for i in range(10):
num = float(input(f"Enter number {i+1}: "))
[Link](num)

# Finding max and min values


largest = max(numbers)
smallest = min(numbers)

print(f"
Complete List: {numbers}")
print(f"Largest number: {largest}")
print(f"Smallest number: {smallest}")

Example Output:

Enter number 1 to 10...


Complete List: [12.0, 5.0, 89.0, 4.0, 55.0, 23.0, 7.0, 67.0, 91.0, 10.0]
Largest number: 91.0
Smallest number: 4.0

Python Programming Foundation Assignment Page 4 of 8


Program 7: Fruit List Manipulations

Write a Python program to create a list of five fruits and print the list, first/last fruit, and add one more fruit.

# 1. Create a list of 5 fruits


fruits = ["Apple", "Banana", "Mango", "Orange", "Grapes"]

# 2. Print the complete list


print("Complete fruit list:", fruits)

# 3. Print the first and last fruit


print("First fruit:", fruits[0])
print("Last fruit:", fruits[-1])

# 4. Add one more fruit to the list


new_fruit = "Pineapple"
[Link](new_fruit)
print(f"List after adding '{new_fruit}':", fruits)

Example Output:

Complete fruit list: ['Apple', 'Banana', 'Mango', 'Orange', 'Grapes']


First fruit: Apple
Last fruit: Grapes
List after adding 'Pineapple': ['Apple', 'Banana', 'Mango', 'Orange', 'Grapes',
'Pineapple']

Python Programming Foundation Assignment Page 5 of 8


Program 8: Even and Odd Counter inside List

Write a Python program to count how many even and odd numbers are present in a list.

# Sample list of numbers


numbers_list = [23, 44, 57, 68, 90, 11, 4, 76, 33]

even_count = 0
odd_count = 0

# Iterate through the list to classify


for num in numbers_list:
if num % 2 == 0:
even_count += 1
else:
odd_count += 1

print("Target List:", numbers_list)


print("Count of Even numbers:", even_count)
print("Count of Odd numbers:", odd_count)

Example Output:

Target List: [23, 44, 57, 68, 90, 11, 4, 76, 33]
Count of Even numbers: 5
Count of Odd numbers: 4

Python Programming Foundation Assignment Page 6 of 8


Program 9: Marks Report & Grading System

Write a Python program to accept marks of 5 subjects in a list and calculate total, percentage, and grade.

# Accepting marks for 5 subjects


marks_list = []
for i in range(5):
score = float(input(f"Enter marks for Subject {i+1} (out of 100): "))
marks_list.append(score)

# Calculations
total_marks = sum(marks_list)
percentage = (total_marks / 500) * 100

# Grade Determination
if percentage >= 90:
grade = "A+"
elif percentage >= 80:
grade = "A"
elif percentage >= 70:
grade = "B"
elif percentage >= 60:
grade = "C"
elif percentage >= 33:
grade = "D"
else:
grade = "Fail"

print(f"
--- Performance Summary ---")
print(f"Total Marks: {total_marks} / 500")
print(f"Percentage: {percentage:.2f}%")
print(f"Final Grade: {grade}")

Example Output:

Total Marks: 412.0 / 500


Percentage: 82.40%
Final Grade: A

Python Programming Foundation Assignment Page 7 of 8


Program 10: Linear Search in List

Write a Python program to search for a number in a list. If found, print "Number Found" otherwise print
"Number Not Found".

# Define a collection list


data_source = [10, 25, 45, 60, 75, 90, 115]

# Accept value to search


search_key = int(input("Enter the number to search: "))

# Membership test search


if search_key in data_source:
print("Number Found")
else:
print("Number Not Found")

Example Output:

Enter the number to search: 60


Number Found

Python Programming Foundation Assignment Page 8 of 8

You might also like