Iteration, Bool, Selection
Booleans (values and expressions)
There are only two boolean values. They are True and False. They are called boolean literals. To work in Python must be capitalized. Type <class 'bool'>.
A boolean expression is an expression that evaluates to a boolean value.
3 > 4
>>> False
5 == 5
>>> TrueBoolean operators
x != y # x is not equal to y
x > y # x is greater than y
x < y # x is less than y
x >= y # x is greater than or equal to y
x <= y # x is less than or equal to yThe result of any expression evaluation can be returned by a function (using the return statement), functions can return boolean values aka. boolean functions.
It is common to give boolean functions names that sound like yes/no questions.
def isDivisible(x, y):
return x % y == 0Truthy values
Can be checked with bool()
the is operation
The is operator is rather useless as it checks memory address, but it is very useful for comparing to None.
the in operation
The not keyword ca be used both ways:
Logical operators
Conditional execution
if statement
if statementunary selection
unary selectionthe result of any expression evaluation can be returned by a function (using the return statement), functions can return boolean values
In unary selection the else clause is omitted and nothing happens if the statement is False.
else block cannot be used by itself.
conditionals can be nested
the indentation makes it clear what belongs where, and is thus very important
chained conditionals
Only a single (and optional) final
elsestatement is allowed and it must be the last branch in the statementEach condition is checked in order. If the first is false, the next is checked, and so on.
Even if more than one condition is true, only the first true branch executes.
while statement
while statementThe while statement provides a much more general (than for loop) mechanism for iterating. The body of while will be repeated as long as the controlling boolean expression evaluates to True.
We can use the while loop to create any type of iteration we wish, including anything that we have previously done with a for loop.
The body of the loop should change the value of one or more variables so that eventually the condition becomes False and the loop terminates. Otherwise the loop will repeat forever. This is called an infinite loop.
forloops create definite iteration because they loop through define number of items in a rangewhilestatement creates an indefinite iteration because we do not know how many times it may iterate.
escape character
The string '\t' represents a tab character. The backslash character in '\t' indicates the beginning of an escape sequence. Escape sequences are used to represent invisible characters like tabs and newlines. The sequence '\n' represents a newline.
break me out of jail (or not) keywords
break
You can stop a loop early by using the keyword break.
This loop will print "a" and "b" and then stop because of the break when letter is "c". break can only be used inside of a loop.
continue
Nearly the same loop but this one will print "a", "b", "d", "e", and "f". It skips "c" because of the continue, which causes the loop to immediately move on to the next step in the loop.
Like break, continue can only be used inside of a loop.
Last updated
Was this helpful?