Now that we know how to express “true or false” questions in Python, it's time to put them to use in programs. We'll take different actions depending on whether a condition is true or false with conditional statements.
The simplest form of a conditional statement is the if statement:
The first line is the header line. The boolean expression after the if keyword is called the condition. We end the header line with a colon character (:), and the line(s) after the if statement are indented.
If the logical condition is true, then the indented statement gets executed. If the logical condition is false, the indented statement is skipped. The flowchart is shown in Figure 4.3.1
The general form of an if statement consists of a header line (the word if followed by a condition and ending with a colon character :). The header is followed by an indented block. That block is the body of the if statement. The statement consists of a header line that ends with the colon character (:) followed by an indented block. Statements like this are called compound statements because they stretch across more than one line.
Note4.3.2.
if statements have the same structure as function definitions or for loops. [We will learn about functions in Chapter 4 5 and loops in Chapter 5 6.].
There is no limit on the number of statements that can appear in the body of an if, but there must be at least one. Occasionally, it is useful to have a body with no statements (usually as a place holder for code you haven't written yet). In that case, you can use the pass statement, which does nothing.
If you enter an if statement in the Python interpreter, the prompt will change from three chevrons to three dots to indicate you are in the middle of a block of statements, as shown below:
>>> x = 3
>>> if x < 10:
... print('Small')
...
Small
>>>
When using the Python interpreter, you must leave a blank line at the end of a block; otherwise, Python will return an error:
>>> x = 3
>>> if x < 10:
... print('Small')
... print('Done')
File "<stdin>", line 3
print('Done')
^
SyntaxError: invalid syntax
A blank line at the end of a block of statements is not necessary when writing and executing a script, but it may improve readability of your code.
Checkpoint4.3.3.
Q-3: Given the code below, what line executes after line 2 executes?
1) x = 4
2) if x < 3:
3) print ("x is less than 3")
4) print ("All done")
line 3
Line 3 will only execute when x is less than 3.
line 4
Execution continues at the next statement beyond the block following the if when the logical expression is false.
Checkpoint4.3.4.
Q-4: Given the code below, what describes the values of x that will cause the code to print “condition true”?
if x > 0 and x < 10:
print ("condition true")
print ("All done")
1 to 10
Try again. This would be true if the second expression was x <= 10.
0 to 9
Try again. This would be true if the first logical expression was x >= 0.
1 to 9
The "condition true" will only be printed when x is greater than 0 and less than 10, so this is the range from 1 to 9.
all values of x
Try again. This would be true if the conditional was x > 0 or x < 10.
Checkpoint4.3.5.
Q-5: Given the code below, what describes the values of x that will cause the code to print “condition true”?
if x > 0 or x < 10:
print ("condition true")
print ("All done")
all values of x
This will be true if x is greater than 0 or less than 10. This covers all possible values of x.
1 to 9
Try again. This would be true if the boolean expressions were joined with and instead of or.
0 to 9
Try again. This would be true if the boolean expressions were joined with and instead of or and if the first boolean expression was x >= 0.
1 to 10
Try again. This would be true if the boolean expressions were joined with and instead of or and if the first logical expression was x >= 0 and the second expression was x <= 10.