We have learned Python's data types and related knowledge before. Now we can start writing Python programs, starting with the if conditional judgment.
#The Python programming language specifies that any non-zero and non-empty (null) value is true, and 0 or null is false.
The if statement in Python programming is used to control the execution of the program. The basic form is:
For example, we define a variable age, its value is 30, then use if to determine whether it is >= 18. At this time, the expression of the if statement is true and the else statement is omitted. As follows
In the Python language, the conditional structure consists of if-else statements, and the else statements can also be omitted. The if statement consists of three parts: the keyword itself, the conditional expression used to determine whether the result is true or false, and the code block that is executed when the expression is true or non-zero. The else statement is used in conjunction with the if statement. If the value of the conditional expression of the if statement is true, the statement block after the if statement is executed; if the value of the conditional expression of the if statement is false, the statement block after the else is executed.
For example
It can be seen from the above output that when the age variable does not meet the if condition, the else statement is executed and the output result is teenager
The above is the detailed content of How to write python judgment statement. For more information, please follow other related articles on the PHP Chinese website!