Week 3

Overview

The programs we wrote last week were fine for getting started, but they were inherently limited. This week we will extend our skills by learning how to write programs that utilize conditional logic to make decisions. We will also be looking deeper into algorithms.

Objectives

By the end of this week, you should be able to:

Readings & Resources

Activities & Assignments

Lesson

Flow Control Part I

Boolean Introduction Video

Boolean Values

Comparison Operators

Operator Meaning
== Equal to
!= Not equal to
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to

Boolean Operators

and operator

Expression Evaluates to…
True and True True
True and False False
False and True False
False and False False

or operator

Expression Evaluates to…
True or True True
True or False True
False or True True
False or False False

not operator

Expression Evaluates to…
not True False
not False True

Flow Control Part Two

Elements of Flow Control

Conditions

Blocks of Code

  1. Blocks begin when the indentation increases.
  2. Blocks can contain other blocks.
  3. Blocks end when the indentation decreases to zero or to a containing block’s indentation.

Example of a block

name = 'Mary'
password = 'swordfish'
if name == 'Mary':
    print('Hello Mary')
    if password == 'swordfish':
        print('Access granted.')
    else:
        print('Wrong password.')

if Statements

else Statements

elif Statements

Table as chart:

Table as chart zoomed:

Additional Resources

Conditional Expressions from the Hands on Python Tutorial

Conditional Expressions from python-course.eu