Day 3 - Control Flow and Logical Operators


Control Flow with if / else and Conditional Operators
if / else Statement :
If Statement: Checks a condition. If the condition is True
,
the code inside the if
block runs.
Else Statement: Runs if the if
condition is False
Critical Points
The condition
in the if
the statement must be evaluated to True
or False
.
Indentation is crucial in Python; the code inside if
or else
must be indented.
Condition Check
Conditionals in Python to check a condition and tell the computer what to do in each case.
Example
if <this condition is true>:
<then execute this line of code>
What if the condition is false?
The else keyword is used to define a block of code that will execute if the condition checked in the if statement is false.
if cow can fly:
else:
print("This is real life")
Comparator Operators
\> Greater than
< Less than
\>= Greater than or equal to
<= Less than or equal to
\== Equal to
!= Not equal to
Key Points
Conditional operators are used for comparisons.
They are often combined with if-else
statements for decision-making.
They always return True
or False
.
List of Conditional Operators
Operator | Meaning | Example | Result |
== | Equal to | 5 == 5 | True |
!= | Not equal to | 5 != 3 | True |
< | Less than | 3 < 5 | True |
> | Greater than | 5 > 3 | True |
<= | Less than or equal to | 3 <= 3 | True |
>= | Greater than or equal to | 5 >= 4 | True |
Modulo Operator (%
) in Python
The modulo operator (%
) returns the remainder when one number is divided by another.
It’s commonly used in programming for tasks like finding even/odd numbers.
Example
result = a % b
a
: Dividend (number being divided)
b
: Divisor (number dividing)
result
: Remainder after division
If a
is divisible by b
, the result is 0
.
Example: 10 % 5
gives 0
.
Nested if statement and elif statement
A nested if
statement is an if
statement inside another if
statement.
It allows for checking a condition only if a previous condition is True
.
This structure is useful when decisions are dependent on
Example
You can put if/else statements inside other if/else statements. This is known as nesting. e.g.
if maths_score >= 90:
if english_score >= 90:
print("You're good at everything")
else:
print("You're good at maths")
if english_score >= 90:
print("You're good at english)
Output
You are good at everything
In this case, only when a student has over 90 in maths and English, get
You are good at everything.
Key point:
You can also haveif
statements that don't have a paired else
statement.
Elif Statement in Python
The elif
statement (short for "else if") allows you to check multiple conditions.
If one condition is True
, the corresponding block of code runs, and the
remaining conditions are skipped.
Example
marks = 85
if marks >= 90:
print("Grade: A")
elif marks >= 80:
print("Grade: B")
elif marks >= 70:
print("Grade: C")
else:
print("Grade: F")
Output
Grade: B
Key Differences:
Nested if
: Used when one condition depends on another condition being True
.
Elif: Used to check multiple independent conditions.
Combining Both:
You can use both nested if
and elif
in the same code.
age = 25
has_ticket = True
if age >= 18:
if has_ticket:
print("You can enter the event.")
else:
print("You need a ticket.")
elif age >= 13:
print("You can enter with an adult.")
else:
print("You are too young to enter.")
Output
You can enter the event.
Multiple if Statements
A nested if
statement is an if
statement placed inside another if
or else
block.
It allows for checking multiple conditions
age = 25
has_ticket = True
is_vip = False
if age >= 18:
print("You are old enough to enter.")
if has_ticket:
print("You have a ticket.")
if is_vip:
print("You have VIP access.")
Output
You are old enough to enter.
You have a ticket.
Key Points About Multiple if
Statements:
Each if
statement is evaluated independently.
If multiple conditions are True
, all their corresponding blocks will execute.
Order Matters:
Since all if
statements are evaluated, and the order of conditions can impact the flow of the program.
Different from if-elif
:
In if-elif
, only one block of code runs when a condition is True
.
In multiple if
statements, more than one block can run.
When to Use Multiple if
Statements:
When you check multiple conditions Independent.
When multiple conditions can be true simultaneously.
Logical Operators
Logical operators in Python are used to combine or modify
conditional statements. They return a Boolean value
(True
or False
)based on the logic of the conditions provided.
Types of Logical Operators
and
(Logical AND)
Combines two conditions.
Returns True
only if both conditions are True
.
If either condition is False
, it returns False
.
or
(Logical OR)
Combines two conditions.
Returns True
if at least one condition is True
.
Returns False
only if both conditions are False
.
not
(Logical NOT)
Reverses the logical state of a condition.
If the condition is True
, not
makes it False
, and vice versa.
When to Use Logical Operators
Use and
when all conditions need to be True
.
Use or
when at least one condition needs to be True
.
Use not
to reverse the logic of a condition.
Truth Table for Logical Operators
Condition 1 | Condition 2 | and | or | not Condition 1 |
True | True | True | True | False |
True | False | False | True | False |
False | True | False | True | True |
False | False | False | False | True |
Key Highlights
if/else Statements: Control the program's flow by executing code blocks based on conditions.
Comparator Operators: Enable comparisons like ==
, !=
, <
, and >
.
Nested if and elif: Handle dependent and independent conditions effectively.
Logical Operators: Combine conditions using and
, or
, and not
.
What's next in my journey?
On Day 3 of the 100 Days of Python, I will learn about
Randomisation and Python lists.
Subscribe to my newsletter
Read articles from Muniba Amin directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
