Class 18 — Monday October 4
Logical values and decision
May the light of peace — For you and your families — Always shine brightly
Look both ways
Agenda
- Being logical
if
statement
Transitioning
- Some problem solving requires the ability to react — depending upon the circumstances different conditions need actions to be performed. Python provides the
if
statement for decision making.
- The
if
statement uses logical test expressions to determine which action to perform. There are variants of theif
statement to make decisions based on one, two, or several possibilities.
Downloads
- Program logical_operators.py
- Program can_you_relate.py
- Program acid_test.py
- Program begins_with_a_vowel.py
- Program robotomy.py
- Program daisy_daisy.py
- Program spell_check.py
To do
- Review artifacts
- Read epistle on decision making
Program can_you_relate.py
- Introduces the comparison operators
Three program runs
Enter number: 12
Enter number: 11
12 < 11 : False
12 > 11 : True
12 <= 11 : False
12 >= 11 : True
12 == 11 : False
12 != 11 : True
Enter number: 3
Enter number: 14
3 < 14 : True
3 > 14 : False
3 <= 14 : True
3 >= 14 : False
3 == 14 : False
3 != 14 : True
Enter number: 7
Enter number: 7
7 < 7 : False
7 > 7 : False
7 <= 7 : True
7 >= 7 : True
7 == 7 : True
7 != 7 : False
Program logical_operators.py
- Introduces the logical operators
Program run
##### Logical values (bool)
True
False
##### Logical and
True and True = True
True and False = False
False and True = False
False and False = False
##### Logical or
True or True = True
True or False = True
False or True = True
False or False = False
##### Logical negation
not True = False
not False = True
##### Logical includes
1 in [3, 1, 4, 1, 5] = True
2 in [3, 1, 4, 1, 5] = False
##### Logical exclusion
1 not in [3, 1, 4, 1, 5] = False
2 not in [3, 1, 4, 1, 5] = True
The why, what, and how of the basic if
statement
- General problem solving requires depending upon the circumstances, to perform different actions.
- To support this kind of problem solving, Python provides the
if
statement. Python does not provide any other decision statements.
- The
if
statement uses logical test expressions to determine which action to perform.
- The most common form of the
if
statement is
if ( logical expression ) :
action1
else :
action2
- Where
logical expression
is a test expression that evaluates to eitherTrue
orFalse
.
- Both
action
1 andaction
2 are non-empty sequence of statements.
- The actions are indented one-level further than the start of the
if
statement.
- The
else
is indented at the same level as theif
. Bothif
andelse
are keywords:
- The semantics of the
if
statement begins with thelogical expression
being evaluated.
- If the expression evaluates to
True
,action
1 is executed. Otherwise,action
2 is executed.
- No matter what only one of the two actions of an
if-else
statement is executed.
- The
else
part of theif
statement is optional.
if ( logical expression ) :
action
- If the logical expression evaluates to
True
, the action is executed. Otherwise, no action occurs.
Program acid_test.py
- Determine whether its input pH level is acidic; i.e., less than 7.0
Three program runs
Enter pH level: 6.5
True
Enter pH level: 7.0
False
Enter pH level: 7.5
False
Program what_color_will_my_chrysanthemums_be.py
- Uses an
if
statement to help determine the color of a chrysanthemum based on soil pH.
-
Some program runs
Enter soil pH level: 6.5
pink
Enter soil pH level: 7
blue
Enter soil pH level: 7.5
blue