Conditional statements are used in programming to execute certain blocks of code based on certain conditions. In other words, these statements allow you to make decisions in your program based on the value of some variable or input.
In Python, there are two types of conditional statements:
Now, let's take a look at some sample codes that use conditional statements.
In this sample code, we will use an if statement to check whether a number is even or odd.
# Program to check whether a number is even or odd
number = 6
if number % 2 == 0:
print("The number is even.")
Output: The number is even.
Step-by-step instructions:
In this sample code, we will use an if-else statement to check whether a person is old enough to vote.
# Program to check whether a person is old enough to vote
age = 16
if age >= 18:
print("You are old enough to vote.")
else:
print("You are not old enough to vote.")
Output: You are not old enough to vote.
Step-by-step instructions: