Questions on Conditional Statements
1. Write a Python program to check if a given variable x is positive, negative, or zero.
x = -10
if x > 0:
print("x
is positive")
elif x < 0:
print("x
is negative")
else:
print("x
is zero")
Output:- x is
negative
2. Write a Python program that compares two
variables, a and b, and prints which one is greater or if they are equal.
a = 10
b = 20
if a > b:
print("a
is greater than b")
elif a < b:
print("b
is greater than a")
else:
print("a
and b are equal")
Output:- b is greater than a
3. Write a Python program to check if the variable
x is within the range of 1 to 100 (inclusive).
x = 75
if 1 <= x <= 100:
print("x
is within the range")
else:
print("x
is out of range")
Output:- x is within the range
4. Write a Python program to check if a given
variable n is a multiple of 5.
n = 25
if n % 5 == 0:
print(f"{n}
is a multiple of 5")
else:
print(f"{n}
is not a multiple of 5")
Output:- 25 is a multiple of 5
5. Write a Python program to check if a given
variable num is even or odd.
num = 7
if num % 2 == 0:
print(f"{num}
is even")
else:
print(f"{num}
is odd")
Output:- 7 is odd
6. Write a Python program to check if the length
of a string variable s is greater than or equal to 5.
s = "Python"
if len(s) >= 5:
print("The
string is long enough")
else:
print("The
string is too short")
Output:- The string is long enough
7. Write a Python program to check if the variable
s contains the character 'a'.
s = "apple"
if 'a' in s:
print("'a'
is in the string")
else:
print("'a'
is not in the string")
Output:- 'a' is in the string
8. Write a Python program to check if the variable
x is either an integer or a float.
x = 10.5
if isinstance(x, (int, float)):
print(f"{x}
is a number")
else:
print(f"{x}
is not a number")
Output:- 10.5 is a number
9. Write a Python program to check if three given
sides a, b, and c can form a valid triangle (i.e., the sum of any two sides
must be greater than the third side).
a, b, c = 5, 7, 10
if a + b > c and b + c > a and a + c >
b:
print("These
sides form a triangle")
else:
print("These
sides do not form a triangle")
Output:- These sides form a triangle
10. Write a Python program to check if a given
variable n is divisible by both 3 and 7.
n = 21
if n % 3 == 0 and n % 7 == 0:
print(f"{n}
is divisible by both 3 and 7")
else:
print(f"{n}
is not divisible by both 3 and 7")
Output:- 21 is divisible by both 3 and 7
11. Write a Python program to check if the number
10 exists in a list.
numbers = [5, 8, 12, 10, 15]
if 10 in numbers:
print("10
is in the list")
else:
print("10
is not in the list")
Output:- 10 is in the list
12. Write a Python program to check if all
elements in the list are even.
numbers = [2, 4, 6, 8, 10]
if all(num % 2 == 0 for num in numbers):
print("All
numbers are even")
else:
print("Not
all numbers are even")
Output:- All numbers are even
13. Write a Python program to check if the first
and last elements of a tuple are equal.
my_tuple = (5, 9, 3, 7, 5)
if my_tuple[0] == my_tuple[-1]:
print("The
first and last elements are the same")
else:
print("The
first and last elements are different")
Output:- The first and last elements are
the same
14. Write a Python program that checks for the
longest word in a list.
words = ["apple", "banana", "grapefruit",
"pear"]
longest_word = max(words, key=len)
print("The longest word is:",
longest_word)
Output:- The longest word is: grapefruit
15. Write a
Python program to check if the key 'age' exists in a dictionary.
person = {"name": "John", "age":
25, "city": "New York"}
if "age" in person:
print("Key
'age' exists in the dictionary")
else:
print("Key
'age' does not exist in the dictionary")
Output:- Key 'age' exists in the
dictionary
16. Write a Python program to check if two sets
are equal.
set1 = {1, 2, 3, 4}
set2 = {4, 3, 2, 1}
if set1 == set2:
print("The
sets are equal")
else:
print("The
sets are not equal")
Output:- The sets are equal
17. Write a Python program to check if a
dictionary contains a specific value (e.g., check if the value 25 is present).
person = {"name": "John", "age":
25, "city": "New York"}
if 25 in person.values():
print("Value
25 is in the dictionary")
else:
print("Value
25 is not in the dictionary")
Output:- Value 25 is in the dictionary
18. Write a Python program to check if the sum of
the elements in a list is greater than 50.
numbers = [10, 15, 20, 5]
if sum(numbers) > 50:
print("The
sum is greater than 50")
else:
print("The
sum is not greater than 50")
Output:- The sum is not greater than 50
19. Write a Python program to check if one set is
a subset of another set.
set1 = {1, 2, 3}
set2 = {1, 2, 3, 4, 5}
if set1.issubset(set2):
print("set1
is a subset of set2")
else:
print("set1
is not a subset of set2")
Output:- set1 is a subset of set2