0% found this document useful (0 votes)
29 views

Python Conditions

Uploaded by

Rahul Singh
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)
29 views

Python Conditions

Uploaded by

Rahul Singh
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/ 23

CONDITIONAL STATEMENTS

IN PYTHON
Peter Larsson-Green
Jönköping University
Autumn 2018
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> on
Examples the computed values.
3 < 5 3< 5 → 3 < 5 → 3 < 5 → True
3 > 5 3> 5 → 3 > 5 → 3 > 5 → False
3 == 2 3 == 2 → 3 == 2 → 3 == 2 → False
3 != 2 3 != 2 → 3 != 2 → 3 != 2 → True
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 if. How it is executed
• Optional continuation 1. Evaluate <expr-a>.
of an if/elif statement. True False
Syntax
if <expr-a> : 2. Execute 2. Evaluate
Statements-a <statements-a>. <expr-b>.
3. Go to next True False
elif <expr-b> :
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 to
Syntax 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 True else:
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 False return 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): def max(number_a, number_b):
if number_a < 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)
if number_a < number_b:
nine = max(9, 6)
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 = <expr3> return <expr2>
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 True → not True → False
2. Invert that value.
not False → not False → True
THE AND EXPRESSION
Syntax: <expr1> and <expr2>

How it is computed
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 and True → True and True → True and True →
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
Syntax: <expr1> or <expr2>

How it is computed
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 or False → False or False → False or False →


False
False or True → False or True → False or True →
True
True or False → True or False → True
True or True → True or True → 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)

You might also like