Python · Syntax · Beginner
Conditions
Practical exercises with conditional statements in Python: comparing numbers, checking ranges, working with strings, passwords, percentages, and basic if, elif, else logic.
Quick topic start and explanations before exercises (exercises below):
Comparing values and combining conditions
#Reading input and common patterns
#Conditions quick reference
#Exercises:
Checking the sign of a number.
#number = float(input("Enter a number: "))
Solution
number = float(input("Enter a number: "))
if number > 0:
print("The number is positive")
elif number < 0:
print("The number is negative")
else:
print("The number is zero")
# Or you can do this: first prepare the message, and then print it once
number = float(input("Enter a number: "))
if number > 0:
result = "The number is positive"
elif number < 0:
result = "The number is negative"
else:
result = "The number is zero"
print(result)
Age category.
#age = int(input("Enter age: "))
Solution
age = int(input("Enter age: "))
if age <= 12:
print("Child")
elif age <= 17:
print("Teenager")
else:
print("Adult")
# second option: the boundaries can be written explicitly
age = int(input("Enter age: "))
if age < 13:
print("Child")
elif age >= 13 and age <= 17:
print("Teenager")
else:
print("Adult")
The larger of two numbers.
#a = float(input("Enter the first number: "))
b = float(input("Enter the second number: "))
Solution
a = float(input("Enter the first number: "))
b = float(input("Enter the second number: "))
if a > b:
print("Larger number:", a)
elif b > a:
print("Larger number:", b)
else:
print("The numbers are equal")
# or you can do this using the result variable
a = float(input("Enter the first number: "))
b = float(input("Enter the second number: "))
if a == b:
result = "The numbers are equal"
elif a > b:
result = f"Larger number: {a}"
else:
result = f"Larger number: {b}"
print(result)
Temperature: cold, warm, hot.
#temp = float(input("Enter temperature: "))
Solution
temp = float(input("Enter temperature: "))
if temp < 10:
print("Cold")
elif temp <= 24:
print("Warm")
else:
print("Hot")
Checking if a number is even.
#num = int(input("Enter a number: "))
Solution
num = int(input("Enter a number: "))
if num % 2 == 0:
print("The number is even")
else:
print("The number is odd")
# or you can do this: store the check in a separate variable
num = int(input("Enter a number: "))
is_even = num % 2 == 0
if is_even:
print("The number is even")
else:
print("The number is odd")
Checking password length.
#password = input("Enter password: ")
Solution
password = input("Enter password: ")
if len(password) < 6:
print("Password is too short")
else:
print("Password accepted")
Grade on the A-F scale.
#score = int(input("Enter grade: "))
Solution
score = int(input("Enter grade: "))
if score >= 90:
print("A")
elif score >= 80:
print("B")
elif score >= 70:
print("C")
elif score >= 60:
print("D")
else:
print("F")
# or you can do this: move from smaller boundaries to larger ones
score = int(input("Enter grade: "))
if score < 60:
print("F")
elif score < 70:
print("D")
elif score < 80:
print("C")
elif score < 90:
print("B")
else:
print("A")
Divisibility of two numbers.
#a = float(input("Enter the first number: "))
b = float(input("Enter the second number: "))
Solution
a = float(input("Enter the first number: "))
b = float(input("Enter the second number: "))
if b == 0:
print("Division by zero is not allowed")
elif a % b == 0:
print("The first number is divisible by the second")
else:
print("The first number is not divisible by the second")
Rounding within the range 0-100.
#num = float(input("Enter a number: "))
Solution
num = float(input("Enter a number: "))
if num < 0 or num > 100:
print("Only from 0 to 100")
else:
print(round(num, 2))
# or you can do this: store the range check in a variable
num = float(input("Enter a number: "))
in_range = 0 <= num <= 100
if in_range:
print(round(num, 2))
else:
print("Only from 0 to 100")
Long or short string.
#text = input("Enter a string: ")
Solution
text = input("Enter a string: ")
if len(text) > 10:
print("Long string")
else:
print("Short string")
# or you can do this using the message variable
text = input("Enter a string: ")
if len(text) > 10:
message = "Long string"
else:
message = "Short string"
print(message)
Percentage of a number.
#a = float(input("Part: "))
b = float(input("Of whole: "))
Solution
a = float(input("Part: "))
b = float(input("Of whole: "))
if b == 0:
print("Division by zero is not allowed! And there is no part of 0.")
else:
result = (a / b) * 100
result = round(result, 2)
print(str(result) + "%")
# or you can do this: round immediately when creating result
a = float(input("Part: "))
b = float(input("Of whole: "))
if b == 0:
print("Division by zero is not allowed! And there is no part of 0.")
else:
result = round((a / b) * 100, 2)
print(str(result) + "%")
Rounding to the required decimal place.
#num = float(input("Enter a number: "))
rounding = int(input("How many decimal places to round to: "))
Solution
num = float(input("Enter a number: "))
rounding = int(input("How many decimal places to round to: "))
if rounding < 0:
print("The value for the number of decimal places cannot be less than 0.")
# Although if you pass a negative number to round as the number of digits,
# the result of rounding will simply be 0. This was done intentionally so there are no errors.
else:
print(round(num, rounding))
Choosing int or float.
#num = input("Enter a number: ")
Solution
num = input("Enter a number: ")
if "." in num:
num = float(num)
else:
num = int(num)
print(num)
# second option: you can also check for a comma if the user enters the number in that format
num = input("Enter a number: ")
if "." in num or "," in num:
num = num.replace(",", ".")
num = float(num)
else:
num = int(num)
print(num)
Division without a zero error.
#a = float(input("Enter a number: "))
b = float(input("Enter the 2nd number: "))
Solution
a = float(input("Enter a number: "))
b = float(input("Enter the 2nd number: "))
if b == 0:
print("Division by zero is not allowed! Even if you really want to!")
else:
print("Result:", a / b)
# OR
if b == 0:
print("Division by zero is not allowed! Even if you really want to!")
else:
result = a / b
print("Result:", result)
# OR
if b == 0:
print("Division by zero is not allowed! Even if you really want to!")
else:
result = a / b
print("Result:", round(result, 2))
# OR
if b == 0:
result = "Division by zero is not allowed! Even if you really want to!"
else:
result = round(a / b, 2)
print("Result:", result)
# OR other convenient options for this situation, even like this:
if b == 0:
message = "Division by zero is not allowed! Operation error -"
result = False
else:
message = "Result:"
result = round(a / b, 2)
print(message, result)
First name and last name form.
#name = input("Enter First Name: ")
last_name = input("Enter Last Name: ")
Solution
name = input("Enter First Name: ")
last_name = input("Enter Last Name: ")
if " " in name:
print("Fill out the form carefully!! That was the last blank form hahaha)))")
else:
full_name = name + " " + last_name
print(full_name)
# or more carefully using the or operator:
if " " in name:
print("Fill out the form carefully!! That was the last blank form hahaha)))")
elif name == "" or last_name == "":
print("The form must be filled out completely!! (start over for now)")
else:
full_name = name + " " + last_name
print(full_name)