In Python conditionals, a code block is the indented group of statements that runs when its condition is true. If the condition is false, the program skips that group and continues. section 13.2
Indentation shows which statements belong together:
- A conditional block follows an if statement and is indented beneath it. section 13.2
- Blocks can contain nested conditionals, each with its own indented block. Separate conditionals in a series are checked one after another. section 13.3
Listing 13.2. A general way to write a simple if conditional
<some code before> #1
if <conditional>: #2
<do something> #3
<some code after> #1
A general if-statement outline shows how the indented block sits between code before and after the condition. section 13.2
Listing 13.4. Combining conditionals by nesting or by putting them in series
Nested code Unnested code
num_a = int(input("Number? ")) num_a = int(input("Number? "))
num_b = int(input("Number? ")) num_b = int(input("Number? "))
if num_a < 0: #1 if num_a < 0: #1
print("num_a: is negative") print("num_a is negative")
if num_b < 0: #2 if num_b < 0: #2
print("num_b is negative") print("num_b is negative")
print("Finished") #3 print("Finished") #3
Compare nested and unnested conditionals to see how a block controls whether a second decision is reached. section 13.3




