[go: up one dir, main page]

0% found this document useful (0 votes)
10 views24 pages

L04. Booleans and Conditional Statements

The document provides an overview of Booleans, comparison operators, and logical operators in Python, explaining how they return True or False values. It introduces conditional statements, including if, else, and elif, detailing their syntax and usage with examples. The document also emphasizes the importance of indentation in Python code and includes exercises for practical understanding.

Uploaded by

derekhaozhe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views24 pages

L04. Booleans and Conditional Statements

The document provides an overview of Booleans, comparison operators, and logical operators in Python, explaining how they return True or False values. It introduces conditional statements, including if, else, and elif, detailing their syntax and usage with examples. The document also emphasizes the importance of indentation in Python code and includes exercises for practical understanding.

Uploaded by

derekhaozhe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Booleans and Conditional

Statements
Python 1
Booleans
● bool for short
● Is either of: True or False
Comparison Operators
● Like in math class, you can compare two numbers using comparison
operators
● Using comparison operators would return a bool (either True or False)
Comparison Operator Description

> Greater than

< Less than

>= Greater than or equal to

<= Less than or equal to

== Equal to

!= Not equal to
Examples
>>> x = 3 >>> a = 2
>>> x < 5 >>> a < 6 - a
True True
>>> 4 < x
False >>> b = 4
>>> 3 * x >= 4 + x >>> a == b // 2
True True
>>> 2 * x <= x + 3 >>> a == b / 2
True True
>>> y = 5
>>> b + 2 < a + 4
>>> x == y - 2
True False
>>> x * 2 != y - 1 >>> b + 2 <= a + 4
True True
Logical Operators
● Logical Operators take one or two boolean expressions and returns
another boolean value.
● The three logical operators in Python are: and, or, not
Logical Operators: and
● Returns True if the two boolean >>> s = True
expressions evaluate to True >>> t = False
>>> s and t
False
>>> x = 1
>>> y = 3
b1 b2 b1 and b2 >>> x < y and y < 5
True
True True True
>>> x < 0 and y < 2
True False False False
>>> x == 1 and y > 8
False True False False
False False False
Exercise
Given that you have variables a and b such that both are numbers (you don't
know what exactly their values are), write an expression that returns True if
both a and b are positive.
Solution
Incorrect Solution:

a and b > 0

Correct Solution:

a > 0 and b > 0


Logical Operators: or
● Returns True if at least one of the two >>> a = 4
booleans evaluate to True >>> b = 0
>>> a > b or b < -1
True
>>> a == b or b >= 0
True
b1 b2 b1 or b2 >>> a > b or b == 10
True
True True True
>>> a == 30 or b > 1
True False True False

False True True

False False False


Logical Operators: not
● Returns the negation of a boolean >>> m = 20
>>> n = 2
>>> not m < 0
True
b not b >>> not m > n
False
True False >>> not m == n
True
False True
Introduction to Conditional Statements
● In the past, you have always written code that simply executes each and
every single line of code.
● However, that may not always be the case; you may want to execute
certain lines only if a certain condition passes.
● Therefore, we use conditional statements to do such thing
The if statement
● Using the if keyword, you can run certain lines of Python code only if a
certain condition passes (i.e. an expression evaluates to True)
● If the condition fails (i.e. the expression evaluates to False), then the
lines inside the statement are skipped and the program continues below
● Notice how the lines inside a conditional statement is indented.

if condition:
# Do something here...
# Notice the indent
Example
a = int(input())

if a > 1000:
print("That is a big integer! Decreasing...")
a -= 1000

print("Your number is: " + str(a))


Example: Sample Interactions
1006 2

That is a big a big integer! Decreasing... Your number is: 2

Your number is: 6


Indentation Rules
● You may have multiple lines of code within a conditional statement
● If you do so, they must all be indented in a consistent manner
● You may get a syntax error for failure to do so
● By default, on Thonny, one indent is equal to 4 spaces

a = int(input())

if a > 1000:
print("That is a big integer! Decreasing...")
a -= 1000 # INCORRECT SYNTAX!

print("Your number is: " + str(a))


else
● Use an else statement if you want to run code only if an above if
statement's condition fails
● If the above if statement passes, you would skip this
● As usual, consistent indentation is required in such statements.

if condition:
# Do something here...
else:
# Do something else here...
Example
a = int(input())

if a > 1000:
print("That is a big integer! Decreasing...")
a -= 1000
else:
print("That's a better number.")

print("Your number is: " + str(a))


Sample Interactions
1006 2

That is a big a big integer! Decreasing... That's a better number.

Your number is: 6 Your number is: 2


DMOJ Exercise
Dog Treats: https://dmoj.ca/problem/ccc20j1
elif
● Short for "else if"
● Use an elif statement underneath an if statement or another elif
statement to say "if the above condition fails, but another given condition
passes"
● May have an else statement afterwards
Examples
a = int(input())

if a > 1000:
print("That is a big integer! Decreasing...")
a -= 1000
elif a <= 0:
print("That's a negative number! Increasing...")
a *= -1
else:
print("That's a better number.")

print("Your number is: " + str(a))


DMOJ Exercise
Water Classification: https://dmoj.ca/problem/wossoly21j1

Math Assistance: https://dmoj.ca/problem/othscc1p1


Multiple elif statements
● You may have multiple a = int(input())

elif statements one after if a > 2000:


another. print("That is a VERY big integer! Decreasing...")
● Remember that you check a -= 2000
elif a > 1000:
the condition of an elif print("That is a big integer! Decreasing...")
statement only if all of the a -= 1000
elif a < -2000:
above if or elif print("That is a VERY low integer! Increasing...")
conditions have failed a += 2000
elif a < -1000:
print("That is a low integer! Increasing...")
a += 1000
else:
print("That's a better number.")

print("Your number is: " + str(a))


DMOJ Example
Cross Country: https://dmoj.ca/problem/p100ex4

Quadrant Selection: https://dmoj.ca/problem/ccc17j1

You might also like