0% found this document useful (0 votes)
66 views3 pages

Python Decision Making

This document contains a series of Python if-else questions and solutions, ranging from basic to slightly tricky logical problems. Each question includes a code snippet that demonstrates how to implement the solution. Topics covered include number classification, range checking, leap year determination, password validation, and string length comparison.

Uploaded by

veena.katiyar
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)
66 views3 pages

Python Decision Making

This document contains a series of Python if-else questions and solutions, ranging from basic to slightly tricky logical problems. Each question includes a code snippet that demonstrates how to implement the solution. Topics covered include number classification, range checking, leap year determination, password validation, and string length comparison.

Uploaded by

veena.katiyar
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/ 3

Python if else Questions and Solutions

Basic Questions
1. Check if a number is positive or negative
num = float(input("Enter a number: "))
if num > 0:
print("Positive")
else:
print("Negative")

2. Check whether a given number is even or odd


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

3. Given two numbers, print the larger number


a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
if a > b:
print("Larger number:", a)
else:
print("Larger number:", b)

4. Check whether a number is divisible by both 3 and 5


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

5. Find whether a character entered is a vowel or consonant


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

Intermediate Questions
6. Check if a number is within the range 1-100 (inclusive)
num = int(input("Enter a number: "))
if 1 <= num <= 100:
print("Number is in range 1-100")
else:
print("Number is out of range")

7. Given three numbers, print the largest one


a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
c = float(input("Enter third number: "))
if a >= b and a >= c:
print("Largest number:", a)
elif b >= a and b >= c:
print("Largest number:", b)
else:
print("Largest number:", c)

8. Check if a year is a leap year


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

9. Password check
stored_password = "secret123"
password = input("Enter password: ")
if password == stored_password:
print("Access granted")
else:
print("Access denied")

10. Determine if a number is positive, negative, or zero


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

Slightly Tricky / Logical Questions


11. Age group classification
age = int(input("Enter age: "))
if age < 13:
print("Child")
elif 13 <= age <= 19:
print("Teenager")
else:
print("Adult")

12. Number divisible by 2, 3, and 5


num = int(input("Enter a number: "))
if num % 2 == 0 and num % 3 == 0 and num % 5 == 0:
print("Divisible by 2, 3, and 5")
else:
if num % 2 == 0:
print("Divisible by 2")
if num % 3 == 0:
print("Divisible by 3")
if num % 5 == 0:
print("Divisible by 5")
if num % 2 != 0 and num % 3 != 0 and num % 5 != 0:
print("Not divisible by 2, 3, or 5")

13. Check if sum of two numbers is even or odd


a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
if (a + b) % 2 == 0:
print("Sum is even")
else:
print("Sum is odd")

14. Check if the length of a string is greater than 5


text = input("Enter a string: ")
if len(text) > 5:
print("Length is greater than 5")
else:
print("Length is 5 or less")

15. Compare lengths of two strings


str1 = input("Enter first string: ")
str2 = input("Enter second string: ")
if len(str1) > len(str2):
print("First string is longer")
elif len(str2) > len(str1):
print("Second string is longer")
else:
print("Equal length")

You might also like