Home  >  Article  >  Backend Development  >  How to write python judgment statement

How to write python judgment statement

silencement
silencementOriginal
2019-05-16 11:44:3822693browse

We have learned Python's data types and related knowledge before. Now we can start writing Python programs, starting with the if conditional judgment.

How to write python judgment statement

#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:

How to write python judgment statement

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

How to write python judgment statement

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

How to write python judgment statement

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:How to install ipythonNext article:How to install ipython