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

50 Python if Condition Questions

The document contains 50 Python if condition questions along with their solutions, covering various scenarios such as checking number properties, string characteristics, and user eligibility. Each question is presented with a code snippet demonstrating the conditional logic used to arrive at the solution. Topics range from basic checks like positive or negative numbers to more complex conditions like checking for prime numbers and validating file extensions.

Uploaded by

AARUNI RAI
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)
20 views8 pages

50 Python if Condition Questions

The document contains 50 Python if condition questions along with their solutions, covering various scenarios such as checking number properties, string characteristics, and user eligibility. Each question is presented with a code snippet demonstrating the conditional logic used to arrive at the solution. Topics range from basic checks like positive or negative numbers to more complex conditions like checking for prime numbers and validating file extensions.

Uploaded by

AARUNI RAI
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
You are on page 1/ 8

50 Python If Condition Questions with Solutions

1. Check if a number is positive


num = 5
if num > 0:
print("Positive number")

2. Check if number is zero


num = 0
if num == 0:
print("Zero")

3. Check if number is negative


num = -4
if num < 0:
print("Negative number")

4. Check if a number is even


num = 8
if num % 2 == 0:
print("Even")

5. Check if a number is odd


num = 9
if num % 2 != 0:
print("Odd")

6. Check if user is adult


age = 18
if age >= 18:
print("Adult")

7. Check if string is empty


s = ""
if s == "":
print("Empty string")

8. Check if string starts with 'A'


s = "Apple"
if s.startswith("A"):
print("Starts with A")

9. Check if list has more than 5 elements


lst = [1, 2, 3, 4, 5, 6]
50 Python If Condition Questions with Solutions

if len(lst) > 5:
print("List has more than 5 items")

10. Check if variable exists in list


x = 3
lst = [1, 2, 3]
if x in lst:
print("Exists")

11. Check if number is even or odd


num = 7
if num % 2 == 0:
print("Even")
else:
print("Odd")

12. Check if person is eligible to vote


age = 16
if age >= 18:
print("Can vote")
else:
print("Cannot vote")

13. Check if number is positive or negative


num = -1
if num > 0:
print("Positive")
else:
print("Negative or Zero")

14. Check if string contains 'hello'


s = "hello world"
if "hello" in s:
print("Found")
else:
print("Not Found")

15. Check password match


password = "abc123"
if password == "abc123":
print("Access granted")
else:
print("Access denied")
50 Python If Condition Questions with Solutions

16. Find larger of two numbers


a, b = 10, 20
if a > b:
print("a is greater")
else:
print("b is greater")

17. Check if number is divisible by 3


num = 9
if num % 3 == 0:
print("Divisible by 3")
else:
print("Not divisible by 3")

18. Check if character is vowel


ch = 'e'
if ch in 'aeiou':
print("Vowel")
else:
print("Consonant")

19. Check if temperature is cold or hot


temp = 30
if temp > 25:
print("Hot")
else:
print("Cold")

20. Check if number is single digit


num = 9
if 0 <= num <= 9:
print("Single digit")
else:
print("More than one digit")

21. Grade system


marks = 85
if marks >= 90:
print("A")
elif marks >= 80:
print("B")
elif marks >= 70:
print("C")
else:
print("Fail")
50 Python If Condition Questions with Solutions

22. Day of week check


day = 3
if day == 1:
print("Monday")
elif day == 2:
print("Tuesday")
elif day == 3:
print("Wednesday")
else:
print("Other day")

23. Check triangle type


a, b, c = 5, 5, 5
if a == b == c:
print("Equilateral")
elif a == b or b == c or a == c:
print("Isosceles")
else:
print("Scalene")

24. Check number sign


num = 0
if num > 0:
print("Positive")
elif num < 0:
print("Negative")
else:
print("Zero")

25. Determine leap year


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

26. Check age category


age = 45
if age < 13:
print("Child")
elif age < 20:
print("Teenager")
elif age < 60:
print("Adult")
else:
print("Senior")
50 Python If Condition Questions with Solutions

27. Find largest of three


a, b, c = 5, 10, 2
if a > b and a > c:
print("a is largest")
elif b > c:
print("b is largest")
else:
print("c is largest")

28. Check password strength


password = "Pass123"
if len(password) < 6:
print("Weak")
elif password.isalpha():
print("Moderate")
else:
print("Strong")

29. Check season by month


month = 12
if month in [12, 1, 2]:
print("Winter")
elif month in [3, 4, 5]:
print("Spring")
elif month in [6, 7, 8]:
print("Summer")
else:
print("Autumn")

30. Check ticket price by age


age = 5
if age < 5:
print("Free")
elif age <= 18:
print("Child ticket")
elif age <= 60:
print("Adult ticket")
else:
print("Senior ticket")

31. Check if number is between 10 and 20


num = 15
if 10 < num < 20:
print("In range")
50 Python If Condition Questions with Solutions

32. Check if number is divisible by 2 and 3


num = 12
if num % 2 == 0 and num % 3 == 0:
print("Divisible by both")

33. Check if number is divisible by 3 or 5


num = 10
if num % 3 == 0 or num % 5 == 0:
print("Divisible")

34. Check if character is alphabet


ch = 'g'
if ch.isalpha():
print("Alphabet")

35. Check if character is uppercase


ch = 'A'
if ch.isupper():
print("Uppercase")

36. Check if file extension is valid


filename = "data.csv"
if filename.endswith(".csv") or filename.endswith(".txt"):
print("Valid file")

37. Check if list is not empty


lst = [1]
if lst:
print("Not empty")

38. Check if username and password match


username = "admin"
password = "admin123"
if username == "admin" and password == "admin123":
print("Login success")

39. Check if number is not divisible by 7


num = 22
if num % 7 != 0:
print("Not divisible by 7")

40. Check if value is not None


50 Python If Condition Questions with Solutions

value = 10
if value is not None:
print("Has value")

41. Check if string is palindrome


s = "madam"
if s == s[::-1]:
print("Palindrome")

42. Check if year is century


year = 1900
if year % 100 == 0:
print("Century year")

43. Check if marks are pass or fail (>=35)


marks = 33
if marks >= 35:
print("Pass")
else:
print("Fail")

44. Check last digit of a number is 5


num = 125
if num % 10 == 5:
print("Ends in 5")

45. Check if string contains a digit


s = "abc3"
if any(ch.isdigit() for ch in s):
print("Contains digit")

46. Check if number is prime (basic)


num = 7
if num > 1:
for i in range(2, num):
if num % i == 0:
print("Not prime")
break
else:
print("Prime")

47. Check if year has 365 or 366 days


year = 2023
if year % 400 == 0 or (year % 100 != 0 and year % 4 == 0):
50 Python If Condition Questions with Solutions

print("366 days")
else:
print("365 days")

48. Check if string has uppercase letter


s = "abcD"
if any(ch.isupper() for ch in s):
print("Has uppercase")

49. Check if list has duplicates


lst = [1, 2, 3, 2]
if len(lst) != len(set(lst)):
print("Has duplicates")

50. Check if name starts and ends with same letter


name = "anna"
if name[0] == name[-1]:
print("Same start and end")

You might also like