Describes the nested use of if Statements in Python.
This article mainly introduces the nested usage of if Statements in Python, which is the basic knowledge in getting started with Python. For more information, see
This may be the case when you want to check that the next condition is parsed as true. In this case, the nested if structure can be used.
In the nested if statement structure, you can have another if... elif... else structure in the if... else structure.
Syntax:
Syntax for nesting the if... elif... else structure can be:
?
1 2 3 4 5 6 7 8 9 10 11 12 |
If expression1: Statement (s) If expression2: Statement (s) Elif expression3: Statement (s) Else Statement (s) Elif expression4: Statement (s) Else: Statement (s) |
Restrictions. Please confirm. The source must be specified for reprinting.
This may be the case when you want to check that the next condition is parsed as true. In this case, the nested if structure can be used.
In the nested if statement structure, you can have another if... elif... else structure in the if... else structure.
Syntax:
Syntax for nesting the if... elif... else structure can be:
?
1 2 3 4 5 6 7 8 9 10 11 12 |
If expression1: Statement (s) If expression2: Statement (s) Elif expression3: Statement (s) Else Statement (s) Elif expression4: Statement (s) Else: Statement (s) |
For example:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#! /Usr/bin/python Var = 100. If var & lt; 200: Print "Expression value is less than 200" If var = 150: Print "Which is 150" Elif var == 100: Print "Which is 100" Elif var = 50: Print "Which is 50" Elif var <50: Print "Expression value is less than 50" Else: Print "cocould not find true expression" Print "Good bye! " |
When the above code is executed, it will produce the following results:
?
1 2 3 |
Expression value is less than 200 Which is 100 Good bye! |