Lesson 4 Conditionals and Control Flow

Download Report

Transcript Lesson 4 Conditionals and Control Flow

Python Programming Language
Control Flow - Comparators
There are 6 Comparators: • Equal to (==)
• Not equal to (!=)
• Less than (<)
• Less than or equal to (<=)
• Greater than (>)
• Greater than or equal to (>=
Note that == is used to compare whether two things are
equal, and = is used to assign a value to a variable
Control Flow - Comparators
•
bool_one = True
•
bool_two = True
•
bool_three = False
•
bool_four = False
•
bool_five = False
Boolean operators. and, or, not
•
•
•
•
•
Boolean operators (or logical operators) are words used
to connect Python statements in a grammatically correct
way—almost exactly as in regular English. There are three
boolean operators in Python:
and, which means the same as it does in English;
or, which means "one or the other or BOTH" (it's
not exclusively one or the other);
not, which means the same as it does in English.
Boolean operators result in Boolean values—
True or False.
The AND boolean operator
•
•
•
The boolean operator AND only results in True when
the expressions on either side of AND are both true. An
expression is any statement involving one or more
variables and operators (arithmetic, logical, or boolean).
For instance:
1 < 2 and 2 < 3 results in True because it is true that one
is less than two and that two is less than three.
1 < 2 and 2 > 3 results in False because it is not true that
both statements are true—one is less than two, but two
is not greater than three.
The OR boolean operator
•
•
•
The boolean operator OR only returns
True when either (meaning one, the other or both!) of the
expressions on each side of OR are true. (It's only
False when both expressions are False.) For example:
1 < 2 or 2 > 3 is True, even though two is not greater than
three;
1 > 2 or 2 > 3 is False, because it is neither the case that
one is greater than two nor that two is greater than three.
The NOT boolean operator
•
•
•
•
The boolean operator NOT returns True for false
statements and False for true statements. Remember, the
only two boolean values are True and False!
For example:
not False will evaluate to True, as will not 40 > 41.
Applying not to expressions that would otherwise be true
makes them False
This and That (or This, But Not That!)
•
•
•
•
Boolean operators can be chained together!
It's important to know that boolean operators are not
evaluated straight across from left to right all the time; just
like with arithmetic operators, where / and * are evaluated
before + and There is an order of precedence or order of
operations for boolean operators. The order is as follows:
– not is evaluated first;
– and is evaluated next;
– or is evaluated last.
This order can be changed by including parentheses (()).
Anything in parentheses is evaluated as its own unit.
Conditional Statement Syntax
•
•
•
•
•
•
Remember whitespace in Python is significant?
In JavaScript, the block of code an ifstatement executes is
bound by curly braces ({}). In Python, whitespace (tabs or
spaces) does this work for us.
Here's an example of if statement syntax in Python:
if 8 < 9: print "Eight is less than nine!" if is always followed
by an expression, which is followed by a colon (:).
The code block (the code to be executed if the expression
evaluates to True) is indented four spaces.
This is also true for elif and else(which we'll get to in a
moment). The full syntax would look something like this:
if 8 < 9: print "I get printed!" elif 8 > 9: print "I don't get
printed." else: print "I also don't get printed”
Else conditional statement
•
•
•
The else statement in Python is the complement to
the if statement. While an if statement will return control of
the program to the next line of code after the if code block
even if there's no else statement, it's considered a good
habit to pair each if with an else.
An if/else pair says to Python: "If this expression is true,
run this indented code block; otherwise, run this code after
the else statement."
else is always written alone. Unlike if, else should have
nothing after it except a colon.
Elif conditional statement
•
"Elif" is short for "else if." It means exactly what it sounds
like: "otherwise, if the following code is true, do this!"
Here's what you've learned in this unit:
•
•
•
•
Basics of control flow;
Comparators (such as >, <, and==);
Boolean operators (and, or, and not);
And conditional statements (if, else, and elif).
And finally
•
•
•
•
•
def teachers_question():
print "You've just spent some time learning Conditionals
and Control Flow in Python"
print "Did you enjoy it and learn a lot?"
answer = raw_input("Type Yes or No and hit
'Enter'.").lower()
if answer == "yes" or answer == "y":
print "Brilliant, I'm glad you enjoyed it"
elif answer == "no" or answer == "n":
print "I'm sorry to hear that. Perhaps you'll need to start
again and have another go!"
else:
print "You didn't pick Yes or No! Try again."
teachers_question()
•
teachers_question()
•
•
•
•
•
•