Automate the Boring Stuff with Python Ch.2
What are the 3 rules for blocks?
- Begin when the indentation increases - Can contain other blocks - End when the indentation decreases to zero or to a containing block's indentation
What does an elif statement consist of?
- The elif keyword - A condition (an expression that evaluates to True or False) - A colon - Starting on the next line, and indented block of code (called the elif clause)
What does an else statement consist of?
- The else keyword - A colon - Starting on the next line, and indented block of code (call the else clause)
What does a for statement consist 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, and indented block of code (called the for clause)
What does an if statement 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 (called the if clause)
What does an import statement consist of?
- The import keyword - The name of the module - Optionally, more module names, as long as they are separated by commas
What does a while statement consist of?
- The while keyword - A condition (an expression that evaluates to True or False) - A colon - Starting on the next line, and indented block of code (called the while clause)
What are the only two values associated with the Boolean data type?
- True - False
What are the 3 Boolean Operators?
- and - or - not
What are the comparison operators?
== (Equal to) != (Not equal to) < (Less than) > (Greater than) <= (Less than or equal to) >= (Greater than or equal to)
Break Statement
A shortcut to getting the program execution to break out of a while loop's clause early
Program Execution (or simply, execution)
A term for the current instruction being executed
Infinite Loop
A while loop whose condition is always True
Conditions
Always evaluate down to a Boolean value, True or False
elif statement
An "else-if" statement that always follows an if or another elif statement & it provides another condition that is checked only if all of the previous conditions were False
What determines if an if statement's clause (the block following the if statement) will or will not execute?
An if statement's clause will execute if the statement's condition is True. The clause is skipped if the condition is False.
Flow Control Statements
Can decide which Python instructions to execute under which conditions
Comparison Operators
Compare two values and evaluate down to a single Boolean value
What does the random.randint() function call do?
Evaluates to a random integer between the two integers that you pass it
What is the most common type of flow control statement?
If statement
Blocks
Lines of Python code that are grouped together
When can you use break and continue statements?
Only inside while and for loops
When is an else clause executed?
Only when the if statement's condition is False
not Operator
Operates on only one Boolean value (or expression) and simply evaluates to the opposite Boolean value
CTRL + C
Sends a KeyboardInterrupt error to your program and causes it to stop immediately
Binary Operators
The and & or operators always take two Boolean values (or expressions)
What is the difference between a while statement and an if statement?
The difference is how they behave. At the end of an if clause, the program execution continues after the if statement. But at the end of a while clause, the program execution jumps back to the start of the while statement.
Range() Function Arguments
The first argument will be where the for loop's variable starts, and the second argument will be up to, but not including, the number to stop at. The third argument is the step argument, which is the amount that the variable is increased by after each iteration
The code in a while clause will be executed as long as the while statement's condition is ____
True
For Statement
Used when you want to execute a block of code only a certain number of times
Continue Statement
When the program execution reaches this, the program execution immediately jumps back to the start of the loop and reevaluates the loop's condition
What is the while clause often called?
While loop / Loop
Flow control statements often start with a part called the _________, and all are followed by a block of code called the ______
condition, clause
What is an example of how a for statement looks?
for i in range(5):
You can make a block of code execute over and over again with a _____ statement
while