CONDITIONAL STATEMENTS IN
PYTHON
CONDITIONAL STATEMENTS
Enter a number: 12 Enter a number: -12
That is a positive number! That is a negative number!
BOOLEANS
• Used to represent something's correctness.
•Possible values: True and
False. Examples
True True True
False False False
RELATIONAL EXPRESSIONS
Syntax: <expr1> <operator> <expr2> How it is computed
1. Evaluate <expr1>.
2. Evaluate <expr2>.
3. Apply <operator>
Examples on the computed
values.
3 < 5 3 < 5 3 <
5 3 < 5 True
3 > 5 3 > 5
3 > 5 3 > 5 False
3 == 2 3 ==
3
3 ==
!= 2 2
2
3 != 2 3 != 3 != 22
3 == False
2
RELATIONAL EXPRESSIONS
Examples
5 <= 3 5 <= 3 5 <= 3 5 <= 3
False
9 >= 5 9 >= 2 9 >= 2 9 >= 2
True
THE IF STATEMENT
Conditionally executes How it is executed
statements. 1. Evaluate <expr>.
Syntax True False
if <expr> :
Statement 1 2. Execute 2. Go to next
Statement 2 <statementX>. statement.
Statement ... 3. Go to next
statement.
THE ELIF STATEMENT
• elif is short for else How it is executed
if. 1. Evaluate <expr-a>.
• Optional continuation True
of an if/elif
Syntax
statement. 2. Execute False 2. Evaluate
if <expr-a> :
Statements-a <statements-a>. <expr-b>.
elif <expr-b> :
3. Go to next True False
statement.
Statements-b 3. Execute 3. Go to next
<statements-b>. statement.
4. Go to next
statement.
THE ELIF STATEMENT
if <expr-a> : if <expr-a> : if <expr-a> :
Statements-a Statements-a Statements-a
elif <expr-b> : elif <expr-b> : elif <expr-b> :
Statements-b Statements-b Statements-b
elif <expr-c> : elif <expr-c> :
Statements-c Statements-c
elif <expr-d> :
Statements-d
THE ELSE STATEMENT
Optional tail to an How it is executed
if/elif statement. If all <expr-X> evaluates
Syntax to False, execute the else
statements.
if <expr-a> : if <expr-a> :
Statements-a Statements-a
else: elif <expr-b> :
Statements-b Statements-b
else:
Statements-c
EXAMPLE
def is_between_5_8(x): def is_between_5_8(x):
if x < 5: if 5 <= x:
return False if x <= 8:
elif 8 < x: return True
return False else:
else: return False
return else:
True return
False
is_between_5_8(4) False is_between_5_8(8) True
is_between_5_8(5) True is_between_5_8(9) False
EXAMPLE
def is_between_5_8(x): def is_between_5_8(x):
if x < 5: if 5 <= x:
return False if x <= 8:
if 8 < x: return True
return return False
False
return True
is_between_5_8(4) False is_between_5_8(8) True
is_between_5_8(5) True is_between_5_8(9) False
EXAMPLE
• def max(number_a, number_b): if number_a <
def max(number_a, number_b):
number_b: if number_a < number_b:
return number_b
•return number_b else:
return number_a
• return number_a
def max(number_a, number_b):
biggest = number_a
•four = max(3, 4) nine = max(9, 6)
if number_a < number_b:
biggest = number_b
return biggest
THE IF-ELSE EXPRESSION
Syntax
<expr1> if <expr2> else
<expr3>
How it is evaluated
1. Evaluate <expr2>.
True False
2. Evaluate 2. Evaluate
<expr1> and <expr3> and
yield the result. yield the result.
THE IF-ELSE EXPRESSION
variable = <expr2> if <expr1> else <expr3>
if <expr1>:
variable = <expr2> def func():
else: if <expr1>:
variable = return <expr2>
<expr3> else:
return <expr3>
def func():
return <expr2> if <expr1> else <expr3>
EXAMPLE
def max(number_a, number_b):
return number_b if number_a < number_b else number_a
THE NOT EXPRESSION
Inverts boolean values.
Syntax: not <expr>
How it is computed Examples
1. Evaluate <expr>. not not True False
2. Invert that value. True
not False True
not False
THE AND EXPRESSION
• How it is computed
Syntax: <expr1> and <expr2>
1. Evaluate <expr1>.
• False True
2. Yield False. 2. Evaluate <expr2>.
False True
3. Yield False. 3. Yield True.
THE AND EXPRESSION
Syntax: <expr1> and <expr2>
False and False False and False False
False and True False and True False
True and False True and False True and False
False
True True True and True True and True
and
True
EXAMPLE
def is_between_2_5(x):
return 2 < x and x < 5
yes = is_between_2_5(4.95)
no = is_between_2_5(0)
THE OR EXPRESSION
• How it is computed
Syntax: <expr1> or <expr2>
1. Evaluate <expr1>.
• True False
2. Yield True. 2. Evaluate <expr2>.
True False
3. Yield True. 3. Yield False.
THE OR EXPRESSION
Syntax: <expr1> or <expr2>
False False False or False False or False
or
False True False or True False or True
False
or
True False True or False
True
True True True or True
or True
EXAMPLE
def is_not_between_2_5(x):
return x < 2 or 5 < x
yes = is_not_between_2_5(0)
no = is_not_between_2_5(4.95)