25 Python Problem Statements with Explanations & Solutions
1. Print "Hello, World!"
Explanation: Use the print() function to output text.
Solution:
print("Hello, World!")
2. Add Two Numbers
Explanation: Use input() and int() to read and sum two numbers.
Solution:
a = int(input())
b = int(input())
print("Sum:", a + b)
3. Find the Square of a Number
Explanation: Use the exponent operator ** to find square.
Solution:
n = int(input())
print("Square:", n ** 2)
4. Check Even or Odd
Explanation: Use modulus operator to check if divisible by 2.
Solution:
n = int(input())
print("Even" if n % 2 == 0 else "Odd")
5. Find the Largest of Two Numbers
Explanation: Use conditional expression to compare.
Solution:
a = int(input())
b = int(input())
print("Larger:", a if a > b else b)
6. Swap Two Numbers
Explanation: Python allows multiple assignment to swap.
Solution:
x, y = 5, 10
x, y = y, x
print(x, y)
7. Convert Celsius to Fahrenheit
25 Python Problem Statements with Explanations & Solutions
Explanation: Use formula: F = (C × 9/5) + 32.
Solution:
c = float(input())
print("Fahrenheit:", (c * 9/5) + 32)
8. Calculate Area of a Circle
Explanation: Use math.pi and radius squared.
Solution:
import math
r = float(input())
print("Area:", math.pi * r ** 2)
9. Print Numbers from 1 to 10
Explanation: Use a for loop and range().
Solution:
for i in range(1, 11):
print(i)
10. Sum of First n Natural Numbers
Explanation: Use a loop to accumulate total.
Solution:
n = int(input())
total = 0
for i in range(1, n+1):
total += i
print(total)
11. Find Factorial
Explanation: Multiply numbers from 1 to n using loop.
Solution:
n = int(input())
f=1
for i in range(1, n+1):
f *= i
print(f)
12. Check Leap Year
Explanation: Check if divisible by 4 and not 100 unless 400.
Solution:
y = int(input())
if (y % 4 == 0 and y % 100 != 0) or y % 400 == 0:
25 Python Problem Statements with Explanations & Solutions
print("Leap Year")
else:
print("Not Leap Year")
13. Check Prime Number
Explanation: Check if divisible by any number from 2 to n-1.
Solution:
n = int(input())
if n > 1:
for i in range(2, n):
if n % i == 0:
print("Not Prime")
break
else:
print("Prime")
else:
print("Not Prime")
14. Reverse a String
Explanation: Use string slicing s[::-1] to reverse.
Solution:
s = input()
print(s[::-1])
15. Check Palindrome
Explanation: Check if string equals its reverse.
Solution:
s = input()
print("Palindrome" if s == s[::-1] else "Not Palindrome")
16. Count Vowels
Explanation: Use a loop or comprehension to count vowels.
Solution:
s = input().lower()
v = "aeiou"
print(sum(1 for c in s if c in v))
17. Generate Multiplication Table
Explanation: Use a loop from 1 to 10 to multiply.
Solution:
n = int(input())
for i in range(1, 11):
25 Python Problem Statements with Explanations & Solutions
print(f"{n} x {i} = {n*i}")
18. Print Fibonacci Series
Explanation: Print first n numbers using iterative method.
Solution:
n = int(input())
a, b = 0, 1
for _ in range(n):
print(a, end=" ")
a, b = b, a + b
19. Find Largest in a List
Explanation: Use max() to find the largest element.
Solution:
lst = [5, 10, 3, 22]
print(max(lst))
20. Remove Duplicates from List
Explanation: Convert list to set then back to list.
Solution:
lst = [1, 2, 2, 3, 4]
print(list(set(lst)))
21. Count Words in a Sentence
Explanation: Use split() to separate words and len().
Solution:
s = input()
print(len(s.split()))
22. Check Armstrong Number
Explanation: Sum of each digit cubed should equal original.
Solution:
n = int(input())
print("Armstrong" if sum(int(d)**3 for d in str(n)) == n else "Not Armstrong")
23. Create Dictionary from Two Lists
Explanation: Use zip() and dict() to pair keys/values.
Solution:
k = ["a", "b"]
v = [1, 2]
25 Python Problem Statements with Explanations & Solutions
print(dict(zip(k, v)))
24. Count Frequency in List
Explanation: Use dictionary to store count.
Solution:
lst = [1,2,2,3]
d = {}
for x in lst:
d[x] = d.get(x, 0) + 1
print(d)
25. Check Positive, Negative or Zero
Explanation: Use if-elif-else to check sign.
Solution:
n = float(input())
if n > 0:
print("Positive")
elif n < 0:
print("Negative")
else:
print("Zero")