Welcome to
Python & AI
Bootcamp
Batch 3
Instructor: Muntaha Khalid
Logical Operators
Lastly, there are times where we want to write code that requires
multiple or more complex conditions.
Going over the logical operators again, we have:
● and operator; only runs code if both conditions joined by it evaluate
to True. If either is False, the entire statement is False!
● or operator; runs code if either of the conditions joined by it
evaluate to True. The entire statement is only ever False if both
conditions are False!
● not operator; only takes in one condition! It then swaps the
condition’s evaluation. This means if a condition previously
evaluated to True it becomes False and vice-versa.
Comparison Operators
When you’re working in Python (especially with numbers) there are
many times where you need to compare two different values.
In the context of numbers, the main comparison operators you’ll see are:
● == operator; evaluates to True if both sides are equivalent
● > operator; evaluates to True if the left side is larger than the right
● < operator; evaluates to True if the left side is smaller than the right
● != operator; evaluates to True if both sides are NOT equivalent
< = True
Control Flow
Let’s take a look at control flow! We have:
For-loop (definite loop):
● Performs some block of code, a specific amount of times.
While-loop (indefinite loop):
● Continuously perform a block of code until what’s being tested is evaluated to false.
If-statement:
● Tests for truth. Performs a block of code only when evaluated to true.
If/Elif/Else Statements
As a quick reminder:
● If-Statements take a condition and only runs a block of code if the
condition evaluates to True.
● Elif-Statements take a condition and only tests it if the prior If-
Statement (and all prior Elif-Statements) evaluate to False.
● Else-Statements take no condition and runs a block of code if the
prior If-Statement (and Elif-Statements, if there are any) evaluate
to False.
Still confused? Don’t worry! We’ll practice on these next slides! :)
If/Elif/Else Statements
You can think of these as a forked road!
Let’s run through some examples.
If/Elif/Else Statements
x = 0
Which path would we take: left, middle, or right?
If/Elif/Else Statements
x = 0
Which path would we take: left, middle, or right?
If/Elif/Else Statements
x = -5
Which path would we take: left, middle, or right?
If/Elif/Else Statements
x = -5
Which path would we take: left, middle, or right?
If/Elif/Else Statements
x = -5
Which path would we take: left, middle, or right?
If/Elif/Else Statements
x = 8
Which path would we take: left, middle, or right?