Conditions and Loop Exercise in Python

🐍 Python Conditions & Loops – 30 Exercises with Solutions


✅ Part A: Conditions (if, elif, else)

1. Positive, Negative, or Zero

num = int(input("Enter a number: "))
if num > 0:
    print("Positive")
elif num < 0:
    print("Negative")
else:
    print("Zero")

2. Even or Odd

num = int(input("Enter a number: "))
if num % 2 == 0:
    print("Even")
else:
    print("Odd")

3. Eligible to Vote

age = int(input("Enter age: "))
if age >= 18:
    print("Eligible to vote")
else:
    print("Not eligible to vote")

4. Largest of Three Numbers

a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))

if a >= b and a >= c:
    print("Largest:", a)
elif b >= a and b >= c:
    print("Largest:", b)
else:
    print("Largest:", c)

5. Leap Year

year = int(input("Enter year: "))
if (year % 400 == 0) or (year % 100 != 0 and year % 4 == 0):
    print("Leap Year")
else:
    print("Not a Leap Year")

6. Vowel or Consonant

ch = input("Enter a character: ").lower()
if ch in "aeiou":
    print("Vowel")
else:
    print("Consonant")

7. Grading System

marks = int(input("Enter marks: "))
if marks >= 90:
    print("Grade A")
elif marks >= 75:
    print("Grade B")
elif marks >= 50:
    print("Grade C")
else:
    print("Fail")

8. Divisible by 3 and 5

num = int(input("Enter number: "))
if num % 3 == 0 and num % 5 == 0:
    print("Divisible by 3 and 5")
else:
    print("Not divisible by both")

9. Character Check

ch = input("Enter a character: ")
if ch.isupper():
    print("Uppercase Letter")
elif ch.islower():
    print("Lowercase Letter")
elif ch.isdigit():
    print("Digit")
else:
    print("Special Character")

10. Compare Two Numbers

a = int(input("Enter first number: "))
b = int(input("Enter second number: "))

if a == b:
    print("Both are equal")
elif a > b:
    print("First is greater")
else:
    print("Second is greater")

✅ Part B: For Loops

11. Print 1 to 10

for i in range(1, 11):
    print(i)

12. Multiplication Table

n = int(input("Enter a number: "))
for i in range(1, 11):
    print(n, "x", i, "=", n*i)

13. Even Numbers (1–50)

for i in range(2, 51, 2):
    print(i, end=" ")

14. Sum of First N Numbers

n = int(input("Enter N: "))
total = 0
for i in range(1, n+1):
    total += i
print("Sum =", total)

15. Factorial

n = int(input("Enter number: "))
fact = 1
for i in range(1, n+1):
    fact *= i
print("Factorial =", fact)

16. Fibonacci Series

n = int(input("Enter terms: "))
a, b = 0, 1
for i in range(n):
    print(a, end=" ")
    a, b = b, a+b

17. Numbers Divisible by 7

for i in range(1, 101):
    if i % 7 == 0:
        print(i, end=" ")

18. Sum of Digits

num = int(input("Enter number: "))
s = 0
for digit in str(num):
    s += int(digit)
print("Sum of digits:", s)

19. Reverse a Number

num = input("Enter number: ")
rev = ""
for digit in num:
    rev = digit + rev
print("Reverse:", rev)

20. Star Pattern

n = 5
for i in range(1, n+1):
    print("*" * i)

✅ Part C: While Loops

21. Print 1 to 10

i = 1
while i <= 10:
    print(i)
    i += 1

22. Multiplication Table of 5

i = 1
while i <= 10:
    print("5 x", i, "=", 5*i)
    i += 1

23. Sum of Even Numbers

n = int(input("Enter N: "))
total = 0
i = 2
while i <= n:
    total += i
    i += 2
print("Sum of even numbers =", total)

24. Sum Until 0

total = 0
num = int(input("Enter number (0 to stop): "))
while num != 0:
    total += num
    num = int(input("Enter number (0 to stop): "))
print("Total Sum =", total)

25. Reverse a Number

num = int(input("Enter number: "))
rev = 0
while num > 0:
    rev = rev*10 + num%10
    num //= 10
print("Reverse =", rev)

26. Count Digits

num = int(input("Enter number: "))
count = 0
while num > 0:
    count += 1
    num //= 10
print("Number of digits:", count)

27. Palindrome Check

num = input("Enter number: ")
if num == num[::-1]:
    print("Palindrome")
else:
    print("Not Palindrome")

28. GCD of Two Numbers

a = int(input("Enter first number: "))
b = int(input("Enter second number: "))

while b != 0:
    a, b = b, a % b
print("GCD =", a)

29. Number Pattern

n = 5
i = 1
while i <= n:
    print(str(i) * i)
    i += 1

30. Password Check

password = "python123"
user_input = ""

while user_input != password:
    user_input = input("Enter password: ")
print("Access Granted!")

Give feedback motivation to write article like that
Thank You!

0 Comments