Home  >  Article  >  Backend Development  >  Python conditional statements

Python conditional statements

高洛峰
高洛峰Original
2016-11-23 10:57:461447browse

Python conditional statements are code blocks that are executed based on the execution results (True or False) of one or more statements.

You can simply understand the execution process of conditional statements through the following figure:

Python conditional statements

The Python programming language specifies that any non-0 and non-null value is true, and 0 or null is false.程Python programming IF statement is used to control the execution of the program. The basic form is:

IF judgment conditions:

execute statement ...

Lse:

execute statement ...


🎜🎜 🎜🎜🎜🎜 🎜🎜🎜🎜 When the "judgment condition" is true (non-zero), the following statements will be executed, and the execution content can be multiple lines, and the same range is represented by indentation. 🎜🎜else is an optional statement. When you need to execute content when the condition is not true, you can execute related statements. The specific examples are as follows: 🎜🎜🎜🎜🎜🎜# Example 1: Basic usage of if 🎜
# coding = gb2312
 
flag = False
name = 'luren'     
if name == 'python':         # 判断变量否为'python'
    flag = True      # 条件成立时设置标志为真
    print 'welcome boss'    # 并输出欢迎信息
else:
    print name              # 条件不成立时输出变量名称
>>> luren          # 输出结果
🎜 🎜🎜🎜🎜if statement judgment Conditions can use > (greater than), = (greater than or equal to), (greater than),
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:Python nested loopsNext article:Python nested loops