Python - Chapter 2: Flow Control
Binary Boolean Operators
-and -or -not
how to code a program to exit early
By using the sys.exit() function. You must first import it from the sys module though.
How to terminate a program that is stuck in an infinite loop?
CTRL-C
break
If the execution reaches a break statement, it immediately exits the while loop's clause. Simply just type the break keyword
Blocks
Lines of code that are grouped together with indentations. Blocks can contain blocks
When can you use *break* and *continue* statements?
Only inside of *while* and *for* loops. The *continue* statement will continue to the next value of the *for* loop's counter
elif
Tests a second (or third, or fourth...) condition after the if. When there is a chain of elif statements, only one or none of the clauses will be executed. Once one of the statements' conditions is found to be True, the rest of the elif clauses are automatically skipped. Consists of: -The elif keyword -A condition (that is, an expression that evaluates to True or False) -A colon -Starting on the next line, an indented block of code
else
The else clause is executed only when the if statement's condition is False. consists of the following: -The else keyword -A colon -Starting on the next line, an indented block of code
Boolean Values
True and False
continue
When the program execution reaches a continue statement, the program execution immediately jumps back to the start of the loop and reevaluates the loop's condition. (This is also what happens when the execution reaches the end of the loop.)
What are values in other data types that conditions will consider equivalent to True and False?
When used in conditions, 0, 0.0, and '' (the empty string) are considered False, while all other values are considered True. EX: >>> name = '' while not name: name = input() #This will always ask for a name until one is given
Conditions
a more specific name in the context of flow control statements. Conditions always evaluate down to a Boolean value
and
evaluates an expression to True if both Boolean values are True; otherwise, it evaluates to False True and True True True and False False False and True False False and False False
or
evaluates an expression to True if either of the two Boolean values is True. If both are False, it evaluates to False False or True True False or False False
*for* loop statement and the *range()* function
execute a block of code only a certain number of times based on the *i* Consists of: -The *for* keyword -A variable name -The *in* keyword -A call to the *range()* method with up to three integers passed to it -A colon -Starting on the next line, an indented block of code
==
means "equal to", and returns a Boolean (true/false)
>=
means "greater than or equal to", and returns a Boolean (true/false)
>
means "greater than", and returns a Boolean (true/false)
<=
means "less than or equal to", and returns a Boolean (true/false)
<
means "less than", and returns a Boolean (true/false)
!=
means "not equal to", and returns a Boolean (true/false)
not
operates on only one Boolean value (or expression). This makes it a unary operator. The not operator simply evaluates to the opposite Boolean value. not True False not not not not True True
while
the condition is always checked at the start of each iteration (that is, each time the loop is executed). If the condition is True, then the clause is executed, and afterward, the condition is checked again. The first time the condition is found to be False, the while clause is skipped. Consists of: -The while keyword -A condition -A colon -Starting on the next line, an indented block of code
The Starting, Stopping, and Stepping Arguments to *range()*
the first integer inside of *range()* tells it where to start, the second will be up to, but not including, the number to stop at. The third integer is the amount that the variable is increased/decreased by after each iteration. EX: range(0, 10, 2) will produce 0, 2, 4, 6, 8
if
will execute if the statement's condition is True. The clause is skipped if the condition is False. Consist of: -The if keyword A condition (that is, an expression that evaluates to True or False) -A colon -Starting on the next line, an indented block of code
How can you guarantee that at least one (and only one) of the clauses will be executed in an *if* statement?
you can have an else statement after the last elif statement
How to use the import keyword to use functions in different modules
you first need to import using: -import keyword -name of the module -Optionally, more module names, as long as they are separated by commas Whenever you want to use an imported function now, you must type "(module name).(function name)" with no parentheses or quotes EX: >>>import random, ... random.(randint(...))