What is a Conditional Statement In Python?
A Conditional Statement In Python programming language is a feature that allows the execution of different code blocks based on certain conditions. It helps the program make decisions and execute particular code sections depending on whether a condition is true or false.
Conditional statements typically involve comparison operators (like ==
, >
, <
, !=
) and logical operators (and
, or
, not
).
Types of Conditional Statement Python:
if
Statement:
if-else
Statement:
elif
Statement (short for “else if”):
if Conditional Statement Python:
In Python, the if
conditional statement executes a block of code if a specific condition is true. It allows the program to make decisions based on certain conditions. If the condition evaluates to True
, the indented code block under the if
statement is executed. If the condition is False
, the program skips the code inside the if
block.
Syntax of an if
Statement:
if condition:
—-# Code to execute if the condition is True
Example of an if
Statement:
temperature = 30
if temperature > 25:
—-print(“It’s a hot day.”)
In this example, if the value of temperature
is greater than 25, the message "It's a hot day."
will be printed.
Key Points:
- The
condition
is any expression that evaluates to True
or False
.
- Python uses indentation (spaces or tabs) to define the block of code inside the
if
statement.
- If the condition is false, the block of code inside the
if
statement is skipped.
Example with Input:
age = int(input(“Enter your age: “))
if age >= 18:
print(“You are eligible to vote.”)
In this case, if the user enters the age of 18 or older, the message "You are eligible to vote."
is displayed. If the age is less than 18, nothing happens.
if else Condition Statement Python:
In Python, the if-else
statement executes one block of code if a condition is true, and another block of code if the condition is false. The else
part ensures that when the if
condition is not met, an alternative action is taken.
Syntax of an if-else
Statement:
if condition:
—-# Code to execute if the condition is True
else:
—-# Code to execute if the condition is False
Example of an if-else
Statement:
age = 16
if age >= 18:
—-print(“You are an adult.”)
else:
—-print(“You are not an adult.”)
In this example:
- If
age
is 18 or more, the program prints "You are an adult."
- If
age
is less than 18, the program prints "You are not an adult."
Key Points:
- The
if
condition is checked first. If it’s True
, the indented block under the if
statement runs.
- If the
if
condition is False
, the block under else
is executed.
- Only one of the blocks (either
if
or else
) is executed based on the condition.
Example with Input:
number = int(input(“Enter a number: “))
if number % 2 == 0:
—-print(“The number is even.”)
else:
—-print(“The number is odd.”)
In this case:
- If the number is divisible by 2 (even), it prints
"The number is even."
- Otherwise, it prints
"The number is odd."
The if-else
structure is useful when you need to handle two possibilities based on a single condition.
Video Content:
Here are some programming questions based on the if
statement:
- Check Positive or Negative Number:
Write a program that asks the user to input a number and checks if the number is positive.
- Check if the Number is Even:
Write a program that takes a number from the user and checks if the number is even.
- Check Voting Eligibility:
Write a program that takes a person’s age as input and prints if the person is eligible to vote (18 years or older).
- Check Leap Year:
Write a program that asks the user for a year and checks if it is a leap year (divisible by 4).
- Check If the Number is Greater Than 10:
Write a program that takes a number from the user and checks if the number is greater than 10.
- Temperature Check:
Write a program that takes the temperature as input and prints if it’s a hot day (temperature greater than 30°C).
- Check If Character is a Vowel:
Write a program that takes a letter as input and checks if it is a vowel (a, e, i, o, u).
- Check Divisibility by 5:
Write a program that asks for a number and checks if it is divisible by 5.
- Check If User Input is a Digit:
Write a program that takes input from the user and checks if the input is a digit.
- Check If Age is Above 65:
Write a program that takes a person’s age and checks if the person is a senior citizen (age greater than or equal to 65).
These questions focus solely on the use of the if
statement for simple conditional checks.
Conclusion:
Conditional statements in Python such as if
, elif
, and else
are essential for controlling the flow of a program. Using these conditional statements allows you to make decisions based on specific conditions, allowing for more dynamic and responsive code. Understanding and mastering conditional statements in Python is crucial for writing efficient, logical programs and is a fundamental skill for any programmer.