Class 18 — Monday February 28
Logical values and decision
May the light of peace — For you and your families — Always shine brightly
Look both ways
Agenda
- Check out the Pantone Color of the Year
- More color fun
- Being logical
if
statement
Downloads
- Program intentional_colors.py
- Program one_random_color.py
- 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
Pillow discussion
- The primary Python package to support graphics and image manipulation is Pillow. Pillow is not part of the standard Python install; you have to add it yourself. To install Pillow on your laptop go here. The parts of Pillow of interest to us are:
- Image : Pillow representation of an image.
- ImageDraw: Pillow image drawing commands.
- To gain access to Pillow functionality the module
PIL
needs to be imported. The typical way to gain access toImage
andImageDraw
is statement
from PIL import Image, ImageDraw
Program intentional_colors.py
- The Image function that we care about for now is its
new()
function — through it we can construct a new image.
- Function invocation
Image.new( m, size, color='Black' )
returns a modem
new image, whose dimensions are given bysize
, and whose background is filled-in usingcolor
.
- Parameter
color
is optional with a default value of black.
- The units of measure for
size
are in pixels.
- Parameter
size
will always be an ordered pair of the form( w, h )
, wherew
is the width of the image andh
is the height of the image.
- For us,
m
will always be the string'RGB'
; this mode uses differents levels of red, green, and blue to produce other colors.
- The following code segment defines a new image object called
im
. The nameim
is what many Pillow programmers call their image. Our image is 480 pixels wide and 400 pixels high and its background color is Black.
im_width = 480
im_height = 400
dimensions = ( im_width, im_height )
im = Image.new( 'RGB', dimensions, color='Black' )
- The color names that Pillow knows about are the standard web colors. A list of them is available. There are more than 150 predefined colors. The names are all in camel case — the first letter of each word is capitalized.
- An
Image
object has a method functionshow()
that will create a pop-up window displaying its image.
- To see our new image do
im.show()
- To save a copy of an image use its method function
save()
.
im.save( 'canvas.jpg' )
Function
save()
expects a string parameter giving the name for the picture file. The file will be located in the same folder as the program that did the saving.
Program one_random_color.py
- Review Pillow drawing module documentation
- Check out the Pantone Color of the Year
- How could we generate a random color using Pillow?
- How could we all generate the same "random" color using Pillow?
Transitioning
- Some problem-solving requires the ability to react — depending upon the circumstances different conditions need different actions to be performed. Python provides the
if
statement for computational decision-making.
- The
if
statement uses logical test expressions to determine which action to perform. You can think of a logical test expression like a True/False question on a quiz or test.
- There are also variants of the
if
statement to make decisions based on one, two, or several possibilities. But today, we will most likely just focus on one or two possibilities. #babysteps
To do
- Review artifacts
- Read epistle on decision making
Program can_you_relate.py
- Introduces the comparison operators
- Introduces new primitive type
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
<class 'bool'>
<class 'bool'>
##### 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 code to act differently, depending upon the circumstances.
- To support this kind of problem solving, Python provides the
if
statement.
- 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 sequences of statements (ie lines of code).
- The actions are indented one-level further than the start of the
if
statement.
- Note that the
for
loop allowed us to execute the same lines of code more than one time. Theif
andelse
statements allows us to completely skip lines of code depending on our inputs.
- That is, we choose whether or not to execute
action
1 based on some logical expression (think True/False test question).
- 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
🦆 © 2022 Jim Cohoon | Resources from previous semesters are available. |