Loading...

Nested if statement in Python

Nested If

You can have if statements inside other if statements, which is known as nested if statements.

Example:

python
7 lines
|
35/ 500 tokens
1
2
3
4
5
6
7
x = 41
if x > 10:
    print("Above ten,")
    if x > 20:
        print("and also above 20!")
    else:
        print("but not above 20.")
Code Tools

The Pass Statement

If an if statement cannot be empty, but you want to write an if statement with no content, use the pass statement to avoid getting an error.

Example:

python
4 lines
|
17/ 500 tokens
1
2
3
4
a = 33
b = 200
if b > a:
    pass  # This will not produce an error
Code Tools