Sunday, January 12, 2025
HomeComputer ScienceComputer sciencePython If Else Statements - Conditional Statements

Python If Else Statements – Conditional Statements

In Python, if-else statements are used to execute certain blocks of code based on whether a condition is true or false. These are essential for decision-making in programs.

Syntax of if-else statement:

python
if condition:
# Block of code executed if the condition is true
else:
# Block of code executed if the condition is false

Basic Example:

python
age = 18

if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")

Output:

sql
You are an adult.

In this case, since age is 18, the condition age >= 18 evaluates to true, and the code inside the if block is executed.

Using elif (Else If) for multiple conditions:

You can also check multiple conditions using elif (short for “else if”). The first condition that’s true will execute, and the others will be skipped.

python
age = 16

if age >= 18:
print("You are an adult.")
elif age >= 13:
print("You are a teenager.")
else:
print("You are a child.")

Output:

css
You are a teenager.

In this example, the first condition age >= 18 is false, so it checks the elif condition age >= 13, which is true.

Nested if Statements:

You can nest if-else statements inside each other for more complex decision-making.

python
age = 20
has_license = True

if age >= 18:
if has_license:
print("You can drive.")
else:
print("You cannot drive because you don't have a license.")
else:
print("You cannot drive because you're under 18.")

Output:

You can drive.

Comparison Operators:

You can use comparison operators to check conditions:

  • == : Equal to
  • != : Not equal to
  • > : Greater than
  • < : Less than
  • >= : Greater than or equal to
  • <= : Less than or equal to

Logical Operators:

You can combine multiple conditions using logical operators:

  • and: Both conditions must be true.
  • or: At least one condition must be true.
  • not: Negates a condition (i.e., if it’s true, it becomes false and vice versa).

Examples:

Using and:

python
age = 25
has_license = True

if age >= 18 and has_license:
print("You can drive.")
else:
print("You cannot drive.")

Output:

You can drive.

Using or:

python
age = 15
has_license = False

if age >= 18 or has_license:
print("You can drive.")
else:
print("You cannot drive.")

Output:

You cannot drive.

Using not:

python
is_raining = False

if not is_raining:
print("You can go outside!")
else:
print("Better stay indoors.")

Output:

go
You can go outside!

Summary:

  • if checks if a condition is true and executes the associated block.
  • elif checks additional conditions if the previous conditions are false.
  • else executes when all previous conditions are false.
  • Logical operators (and, or, not) help combine conditions.
  • You can nest if-else statements to handle complex logic.

These statements allow you to implement conditional logic and control the flow of your Python programs effectively.

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x