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.
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 ratings0% 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.
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")