As mentioned above, the form of "expression" after if and elif is very free. As long as the expression has a result, no matter what type of result, Python can judge whether it is "true" or "false".
The bool type has only two values, True and False. Python treats True as "true" and False as "false".
For numbers, Python treats 0 and 0.0 as "true" and other values as "false".
For other types, when objects are empty or None, Python treats them as "false", and otherwise treats them as true. For example, the following expressions are not valid:
"" #Empty string
[] #Empty list
() #Empty tuple
{} #Empty dictionary
None #null
[Example] if elif Judge various types of expressions:
b = False if b: print ('b is True') else: print ('b is False') n = 0 if n: print ('n is not zero') else: print ('n is zero') s = "" if s: print ('s is not an empty string') else: print ('s is an empty string') l = [] if l: print ('l is not an empty list') else: print ('l is an empty list') d = () if d: print ('d is not an empty dictionary') else: print ('d is an empty dictionary') def func (): print ("function is called") if func (): print ('func () return value is not empty') else: print ('func () return value is empty')
The output is:
b is False
n is zero
s is an empty string
l is an empty list
d is an empty dictionary
function is called
func () return value is empty
Explanation: For functions without a return statement, the return value is empty, that is, None.
The previous "Python if else" section showed the three basic forms of selecting a structure and gave examples to demonstrate it, but everyone still have to pay attention to some details when writing the code, especially the indentation of the code block.
Python marks code blocks with indentation. Code blocks must be indented, and those that are not indented are not code blocks. In addition, the same indentation amount of the same code block must be the same, and different indentations do not belong to the same code block.
The code blocks after if, elif, and else must be indented, and they must be indented more than if, elif, and else themselves. For example, the following code is a negative textbook:
age = int (input ("Please enter your age:")) if age <18: print ("Warning: You are underage and access denies!") else: print ("You are an adult, you can enter.")
The output is:
File "[ipython-input-170-1165d5d71182]", line 3
print ("Warning: You are underage and access denies!")
^
IndentationError: expected an indented block
The print function in this example is aligned with the if and else statements. There is no indentation on the same vertical line, so print function is not an if and else block of code, which will cause the Python interpreter to find If the code block of if and else is not found, the error is reported.
In short, the code behind if and else must be indented, otherwise it cannot form the if and else execution body.
Python requires code blocks to be indented but does not require indentation. You can indent n spaces or n tab positions.
However, from the perspective of programming habits, I suggest indenting the position of 1 Tab key or 4 spaces; they are equivalent. Many editors can set the tab key to 4 spaces, such as the default Tab key in IDLE is 4 spaces.
All statements in a code block must be indented, and the indentation must be the same. If a statement is forgotten to be indented, the Python interpreter does not necessarily report an error, but the program's running logic often has problems. Look at the following code:
age = int (input ("Please enter your age:")) if age <18: print ("Warning: You are underage and access denies!") print ("Come again next time after you have above 18.") #forgot to indent
There is no syntax error in this code, but its operation logic is incorrect. For example, the result of entering 16 is as follows:
Please enter your age:16
Warning: You are underage and access denies!
Come again next time after you have above 18.
The user's age is clearly greater than 18, but the "come" prompt appears, and the screen is very awkward, because the second print () statement is not indented, and if does not treat it as the first print () statement The same code block, so it is not part of the if body. Resolving this error is also easy, just indent the second print () by 4 spaces.
Although Python does not limit the amount of indentation of a code block, you can indent n spaces at will, but all statements in the same code block must have the same indentation amount. You cannot indent 2 spaces therefore indent 4 spaces. The following code is a negative example:
age = int (input ("Please enter your age:")) if age <18: print ("Warning: You are underage and access denies!") print ("Come again next time after you have above 18.")
Run this code and the Python interpreter will report a syntax error.
In this code, the first print statement is indented by 4 spaces, and the second print statement is indented by 6 spaces. The indentation differs so that they are not the same code block. Python thinks that the first print statement is the execution of if, and the second print is an accidental existence. I don't know whose code block it should be, so the parsing fails and an error is reported
Another thing to note is that you don't need to indent the code block where you don't need it. Once you indent, it will generate a code block. The following code is a negative example:
info = "The website of the Python tutorial is: www.freelearningpoints.com“ print (info)
These two simple statements don't include branches, loops, functions, classes and other structures, and you shouldn't use indentation.
In the previous chapters, three types of conditional statements were introduced in detail, namely if, if else, and if elif else. These three conditional statements can be nested with each other.
For example, nesting an if else statement in the simplest if statement looks like this:
if expression 1: if expression 2: Code block 1 else: Code block 2
As another example, if else statements are nested inside if else statements, the form is as follows:
if expression 1: if expression 2: Code block 1 else: Code block 2 else: if expression 3: Code block 3 else: Code block 4
In Python, if, if else, and if elif else can be nested within each other. Therefore, when developing a program, you need to choose the appropriate nesting scheme according to the needs of the scenario. It should be noted that when nesting each other, it is necessary to strictly adhere to the indentation specifications of different levels of code blocks.
More Tutorials:
Python Installation - Linux (Ubuntu)More Python Exercises:
Python String Exercises