Python Practical File with Code
1. Check whether the given number is Positive, Negative or Zero
num = int(input("Enter a number: "))
if num > 0:
print("Positive")
elif num < 0:
print("Negative")
else:
print("Zero")
2. Print even numbers from a list using for loop
l1 = [1, 2, 3, 4, 5, 6]
for num in l1:
if num % 2 == 0:
print(num)
3. Find average and grade for marks given in a list
marks = [88, 92, 76, 81, 95]
average = sum(marks) / len(marks)
if average >= 90:
grade = 'A'
elif average >= 80:
grade = 'B'
elif average >= 70:
grade = 'C'
elif average >= 60:
grade = 'D'
else:
grade = 'F'
print("Average:", average)
print("Grade:", grade)
4. Age category based on user input
age = int(input("Enter your age: "))
if age < 13:
print("Child")
elif age < 20:
print("Teenager")
elif age < 60:
print("Adult")
else:
print("Senior Citizen")
5. Find sale price with discount
cost = float(input("Enter cost price: "))
discount = float(input("Enter discount percentage: "))
sale_price = cost - (discount / 100) * cost
print("Sale Price:", sale_price)
6. Perimeter and area of triangle, rectangle, square, and circle
import math
# Triangle
a, b, c = 3, 4, 5
s = (a + b + c) / 2
area_triangle = math.sqrt(s * (s - a) * (s - b) * (s - c))
print("Triangle Area:", area_triangle, "Perimeter:", a + b + c)
# Rectangle
l, w = 5, 3
print("Rectangle Area:", l * w, "Perimeter:", 2 * (l + w))
# Square
side = 4
print("Square Area:", side**2, "Perimeter:", 4 * side)
# Circle
r = 3
print("Circle Area:", math.pi * r**2, "Circumference:", 2 *
math.pi * r)
7. Simple and Compound Interest
p = float(input("Enter principal: "))
r = float(input("Enter rate of interest: "))
t = float(input("Enter time in years: "))
si = (p * r * t) / 100
ci = p * ((1 + r / 100)**t - 1)
print("Simple Interest:", si)
print("Compound Interest:", ci)
8. Profit and Loss
cp = float(input("Enter cost price: "))
sp = float(input("Enter selling price: "))
if sp > cp:
print("Profit:", sp - cp)
elif sp < cp:
print("Loss:", cp - sp)
else:
print("No Profit No Loss")
9. Calculate EMI
P = float(input("Enter loan amount: "))
R = float(input("Enter annual interest rate: "))
T = int(input("Enter loan duration in months: "))
monthly_rate = R / (12 * 100)
emi = P * monthly_rate * ((1 + monthly_rate) ** T) / ((1 +
monthly_rate) ** T - 1)
print("EMI:", emi)
10. Calculate GST and Income Tax
amount = float(input("Enter amount: "))
gst = amount * 0.18
income_tax = amount * 0.10
print("GST:", gst)
print("Income Tax:", income_tax)
11. Find largest and smallest in list
l = [4, 1, 9, 2, 7]
print("Largest:", max(l))
print("Smallest:", min(l))
12. Third largest and third smallest in list
l = [10, 20, 5, 8, 30, 20]
unique_sorted = sorted(set(l))
print("Third Largest:", unique_sorted[-3])
print("Third Smallest:", unique_sorted[2])
13. Sum of squares of first 100 natural numbers
total = sum([i**2 for i in range(1, 101)])
print("Sum of squares:", total)
14. First 'n' multiples of a number
n = int(input("Enter a number: "))
count = int(input("Enter how many multiples: "))
for i in range(1, count+1):
print(n * i)
15. Count vowels in a string
s = input("Enter a string: ")
vowels = "aeiouAEIOU"
count = sum(1 for char in s if char in vowels)
print("Vowel count:", count)
16. Print words starting with an alphabet
s = input("Enter a string: ")
alphabet = input("Enter a starting alphabet: ")
words = s.split()
for word in words:
if word.lower().startswith(alphabet.lower()):
print(word)
17. Count occurrences of an alphabet in each word
s = input("Enter a string: ")
alphabet = input("Enter an alphabet to count: ")
words = s.split()
for word in words:
print(f"{word}: {word.lower().count(alphabet.lower())}")
18. Dictionary of States and Capitals
states = {
"Rajasthan": "Jaipur",
"Maharashtra": "Mumbai",
"UP": "Lucknow"
}
print(states)
19. Dictionary of Students and their Marks
students = {
"Amit": [85, 92, 78, 88, 90],
"Priya": [90, 91, 89, 95, 94]
}
print(students)
20. Highest and Lowest Total Marks in Dictionary
students = {
"Amit": [85, 92, 78, 88, 90],
"Priya": [90, 91, 89, 95, 94]
}
totals = {name: sum(marks) for name, marks in students.items()}
highest = max(totals, key=totals.get)
lowest = min(totals, key=totals.get)
print("Highest:", highest, totals[highest])
print("Lowest:", lowest, totals[lowest])