Python Assignment Solutions
1) Product ≤ 1000 → Product else Sum
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
product = num1 * num2
if product <= 1000:
print("Result:", product)
else:
print("Result:", num1 + num2)
Sample Output:
Enter first number: 20
Enter second number: 30
Result: 600
2) Swap without third variable
a = int(input("Enter a: "))
b = int(input("Enter b: "))
a, b = b, a
print("a:", a)
print("b:", b)
Sample Output:
Enter a: 5
Enter b: 10
a: 10
b: 5
3) Reverse String
text = input("Enter string: ")
print("Reversed:", text[::-1])
Sample Output:
Enter string: Python
Reversed: nohtyP
4) Count 'Emma'
str_x = "Emma is good developer. Emma is a writer"
count = str_x.count("Emma")
print("Emma appears:", count, "times")
Sample Output:
Emma appears: 2 times
5) Palindrome Number
num = input("Enter number: ")
if num == num[::-1]:
print("Palindrome")
else:
print("Not Palindrome")
Sample Output:
Enter number: 121
Palindrome
6) Income Tax
income = float(input("Enter income: "))
tax = 0
if income <= 10000:
tax = 0
elif income <= 20000:
tax = (income - 10000) * 0.10
else:
tax = 10000 * 0.10 + (income - 20000) * 0.20
print("Tax:", tax)
Sample Output:
Enter income: 25000
Tax: 3000.0
7) Leap Year
year = int(input("Enter year: "))
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print("Leap Year")
else:
print("Not Leap Year")
Sample Output:
Enter year: 2024
Leap Year
8) Product of Two Numbers
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
print("Product:", num1 * num2)
Sample Output:
Enter first number: 7
Enter second number: 8
Product: 56
9) Three Names Single Input
name1, name2, name3 = input("Enter three names: ").split()
print(name1)
print(name2)
print(name3)
Sample Output:
Enter three names: Ali Ahmed Sara
Ali
Ahmed
Sara
10) Percentage (Denominator Check)
num = float(input("Enter numerator: "))
den = float(input("Enter denominator: "))
if den == 0:
print("Denominator can't be zero")
else:
percentage = (num / den) * 100
print("Percentage: {:.2f}%".format(percentage))
Sample Output:
Enter numerator: 75
Enter denominator: 100
Percentage: 75.00%