[go: up one dir, main page]

0% found this document useful (0 votes)
8 views13 pages

L05. Booleans and Conditional Statements - Part 2

This document covers Booleans and conditional statements in Python, including the use of if, elif, and else statements, as well as nested conditions and truthiness. It provides examples to illustrate how conditions are evaluated and how logical operators work. Additionally, it emphasizes the importance of using explicit boolean values in conditional statements to avoid potential errors.

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)
8 views13 pages

L05. Booleans and Conditional Statements - Part 2

This document covers Booleans and conditional statements in Python, including the use of if, elif, and else statements, as well as nested conditions and truthiness. It provides examples to illustrate how conditions are evaluated and how logical operators work. Additionally, it emphasizes the importance of using explicit boolean values in conditional statements to avoid potential errors.

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/ 13

Booleans and Conditional

Statements: Part 2
Python 1
Exercise: What is the output?
n = 5 Output:

A
if n < 20:
print('A')
elif n < 10:
print('B')
We do not go to the elif statement because
else:
we passed the first if statement's condition
print('C') where n < 20.

Question: Can we change the value of n in


the beginning to anything else such that this
code prints B instead?
Multiple Logical Operators
You can use multiple logical operators in the >>> a = True
same boolean expression. >>> b = True
>>> c = False
The order of operations apply and are as
>>> d = False
follows:
>>> a and b and c
● B: Brackets False
● N: not >>> a or b or c
● A: and True
● O: or >>> a and b or not c and d
True
Where there are multiple of the same logical >>> a and (not b or c)
operator, we evaluate from left to right False
>>> a and b and not (c or d)
True
DMOJ Example
Telemarketer or not?: https://dmoj.ca/problem/ccc18j1
More ways to use conditional statements
● An if statement does not always need to have an elif or else
statement right below it
● You can put an if statement below another if statement
● if statements will not be skipped, even if you pass an if statement's
condition above it

value = 30 Output

if value > 10: over 10


print('over 10') under 40
if value < 40:
print('under 40')
Exercise: What's the difference between the two?
value = 30 value = 30

if value > 10: if value > 10:


value += 5 value += 5
if value < 40: elif value < 40:
value -= 5 value -= 5
print(value) print(value)

Output Output
30 35
Nested conditions
● You can have conditional statements value = int(input())
inside other conditional statements.
These are called nested conditions.
if value > 10:
● Nested conditions are checked only if
if value % 2 == 0:
you enter the outer conditional
statement print('large even')
● Notice how statements inside a nested else:
condition is indented even farther print('large odd')
than before else:
if value % 2 == 0:
print('small even')
else:
print('small odd')
DMOJ Example
Speed fines are not fine!: https://dmoj.ca/problem/ccc12j1
What's the difference between these two?
x = 10 x = 10
if x < 5: if x < 5:
print('small') print('small')
if x % 2 == 0: if x % 2 == 0:
print('even') print('even')

Output Output
even
Truthiness
● Truthiness refers to how every value in Python has a "boolean
equivalent".
● Some values are considered to be truthy (equivalent to True), and others
are falsy (equivalent to False)
● You can use the built-in function bool(x) to determine if x is truthy

Truthy Values Falsy Values

● Any number other than 0 (this includes ● 0 (or 0.0)


negative numbers) ● The empty string ""
● Any string other than the empty string "" ● False
● True
Truthiness
● Truthiness can be used in boolean >>> a = 3
expressions >>> b = 0
● If non-boolean values are used in >>> bool(a)
boolean expressions, we use their True
truthiness values instead
>>> bool(b)
False
>>> a or b
True
>>> a - 3 or b
False
>>> a and b
False
Truthiness in Conditional Statements
● You can take advantage of truthiness in conditional statements.
● The two code snippets mean the same thing

a = int(input()) a = int(input())
b = int(input()) b = int(input())

if a and b > 0: if a != 0 and b > 0:


print('yes') print('yes')
elif a > b and b: elif a > b and b != 0:
print('no') print('no')
else: else:
print('maybe') print('maybe')
Using Truthiness in Practice
● Unless you're very confident in coding, avoid taking advantage of
truthiness
● Your boolean expressions should use only explicit boolean values in all
your conditional statements to prevent mistakes

You might also like