In Python, conditional statements allow your programs to make decisions and execute different blocks of code based on specific conditions. These decisions are made using the if
, elif
, and else
statements. Think of them as a set of rules that guide your program's behavior.
Chapter 1: The Basics of if
Statement
The if
statement checks if a condition is True
. If it is, the block of code under the if
statement is executed.
Syntax:
if condition:
# Code to run if the condition is True
Example:
age = 18
if age >= 18:
print("You are an adult.")
In this example, the condition age >= 18
is True
, so the program prints "You are an adult."
.
Chapter 2: Using else
– Handling the False Condition
The else
statement allows you to run code when the condition in the if
statement is False
.
Syntax:
if condition:
# Code to run if the condition is True
else:
# Code to run if the condition is False
Example:
age = 16
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")
Here, the condition age >= 18
is False
, so the else
block is executed, and the output is "You are a minor."
.
Chapter 3: Introducing elif
– Multiple Conditions
What if you need to check more than two conditions? That’s where elif
(short for "else if") comes in. It lets you add additional conditions to test after the initial if
condition.
Syntax:
if condition1:
# Code to run if condition1 is True
elif condition2:
# Code to run if condition1 is False and condition2 is True
else:
# Code to run if all previous conditions are False
Example:
score = 85
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B")
elif score >= 70:
print("Grade: C")
else:
print("Grade: F")
In this example, the elif
condition score >= 80
is True
, so "Grade: B"
is printed.
Chapter 4: Working with Comparison Operators
Conditions in if
, elif
, and else
statements often involve comparison operators. Here are the most commonly used ones:
==
: Equal to!=
: Not equal to>
: Greater than<
: Less than>=
: Greater than or equal to<=
: Less than or equal to
Example:
x = 10
y = 5
if x == y:
print("x and y are equal.")
elif x > y:
print("x is greater than y.")
else:
print("x is less than y.")
Here, x > y
is True
, so "x is greater than y."
is printed.
Chapter 5: Combining Conditions with and
, or
, and not
You can combine multiple conditions in an if
statement using logical operators: and
, or
, and not
.
and
: Both conditions must beTrue
.or
: At least one condition must beTrue
.not
: Inverts the condition (i.e.,True
becomesFalse
).
Example: Using and
age = 25
has_id = True
if age >= 18 and has_id:
print("You can enter the club.")
else:
print("You cannot enter the club.")
Both conditions (age >= 18
and has_id
) are True
, so "You can enter the club."
is printed.
Example: Using or
is_student = True
is_senior = False
if is_student or is_senior:
print("You get a discount.")
else:
print("No discount available.")
Since is_student
is True
, the program prints "You get a discount."
.
Example: Using not
is_raining = False
if not is_raining:
print("Go outside!")
else:
print("Stay indoors!")
Since is_raining
is False
, not is_raining
becomes True
, so "Go outside!"
is printed.
Chapter 6: Nesting if
Statements – Checking Conditions Within Conditions
You can also place an if
statement inside another if
statement. This is known as nesting, and it lets you check multiple conditions in a sequence.
Example:
age = 22
has_id = True
if age >= 18:
if has_id:
print("You can enter the club.")
else:
print("You need an ID to enter.")
else:
print("You are too young to enter.")
Here, the program checks if age >= 18
. If this is True
, it then checks whether the person has an ID before allowing entry.
Chapter 7: Practical Example – Grading System
Let’s put everything together with a simple program that assigns letter grades based on a student's score.
Example:
score = int(input("Enter your score: "))
if score >= 90:
print("You got an A!")
elif score >= 80:
print("You got a B!")
elif score >= 70:
print("You got a C!")
elif score >= 60:
print("You got a D!")
else:
print("You got an F.")
This program takes a score from the user and assigns a letter grade based on the conditions.
Chapter 8: Key Takeaways
- The
if
statement checks a condition and executes code if the condition isTrue
. - The
else
statement runs code when theif
condition isFalse
. elif
allows checking multiple conditions in sequence.- Comparison operators like
==
,!=
,>
,<
,>=
, and<=
help in forming conditions. - Logical operators (
and
,or
,not
) allow combining multiple conditions. - You can nest
if
statements for more complex decision-making.
Conclusion:
Conditional statements are a powerful feature in Python, allowing your programs to make decisions and act accordingly. By mastering if
, elif
, and else
, you can write code that is both flexible and responsive to user input. Keep practicing these concepts to get a solid foundation in decision-making within your Python programs!
No comments:
Post a Comment