Home >Backend Development >Python Tutorial >What is a python conditional statement? What is the general format of a conditional statement?

What is a python conditional statement? What is the general format of a conditional statement?

乌拉乌拉~
乌拉乌拉~Original
2018-08-21 16:01:592356browse

For those who have just come into contact with the programming language python, when they first started learning python, they did not know much about the conditional statements in python. In this article, we will explain about python Conditional statements and Conditional statements general formatknow this aspect.

python conditional statements

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

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:

if 判断条件:
    执行语句……
else:
    执行语句……

(the above is the general format of python conditional statements)

where" 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 the content when the condition is not met, you can execute the relevant statement. The specific example is as follows:

# !/usr/bin/python
# -*- coding: UTF-8 -*-

# 例1:if 基本用法

flag = False
name = 'luren'
if name == 'python':  # 判断变量否为'python'
    flag = True  # 条件成立时设置标志为真
    print 'welcome boss'  # 并输出欢迎信息
else:
    print name  # 条件不成立时输出变量名称

The result is as follows:

luren            # 输出结果

The judgment conditions of the if statement can be > (greater than), 2342cb0436bd835a7369fdb5de0d7456= (greater than or equal to), <= (less than or equal to ) to express their relationship.

When the judgment condition is multiple values, you can use the following form:

if 判断条件1:
    执行语句1……
elif 判断条件2:
    执行语句2……
elif 判断条件3:
    执行语句3……
else:
    执行语句4……

The example is as follows:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 例2:elif用法
 
num = 5     
if num == 3:            # 判断num的值
    print &#39;boss&#39;        
elif num == 2:
    print &#39;user&#39;
elif num == 1:
    print &#39;worker&#39;
elif num < 0:           # 值小于零时输出
    print &#39;error&#39;
else:
    print &#39;roadman&#39;     # 条件均不成立时输出

The output result is:

roadman        # 输出结果

(Since python does not support switch statements, multiple condition judgments can only be implemented using elif. If the judgment requires multiple conditions to be judged at the same time, you can use or (or), which means that two conditions have The judgment condition is successful when one is true; when using and (and), it means that the judgment condition is successful only when two conditions are true at the same time.)

The above is the detailed content of What is a python conditional statement? What is the general format of a conditional 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