Home  >  Article  >  Backend Development  >  What to do if there are multiple conditions in Python

What to do if there are multiple conditions in Python

coldplay.xixi
coldplay.xixiOriginal
2020-10-21 14:09:3726324browse

How to handle if in Python has multiple conditions: if in python has multiple conditions, you can use the and, or, elif keywords to connect, the code is [if name == 'zs' and age = = 18:print('name: zs, age: 18')].

What to do if there are multiple conditions in Python

How to handle multiple conditions if if in Python:

If there are multiple conditions in python, you can Use the and, or, elif keywords to connect.

The if statement in Python programming is used to control the execution of the program. The basic form is:

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

The judgment condition of the if statement can be > (greater than), 2342cb0436bd835a7369fdb5de0d7456= (greater than or equal to), <= (less than or equal to) to express the 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……

Example

1. Use and for multi-condition judgment:

if name == &#39;zs&#39; and age == 18:
 
  print(&#39;name: zs, age: 18&#39;)

2. Use or for multi-condition judgment:

if passwd == &#39;123456&#39; or passwd == &#39;abcdef&#39;
 
  print(&#39;welcome!&#39;)

3. Use elif for multi-condition judgment:

if user == &#39;zs&#39;;
  print(&#39;hi zs&#39;)
elif user == &#39;ls&#39;:
  print(&#39;hi li&#39;)

Example extension:

.Basic usage of if conditional statement:

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

When the "judgment condition" is true (non-zero), the following statements will be executed, and the execution content can be multiple lines, and they are indented to indicate the same range.

else is an optional statement. When you need to execute content when the condition is not true, you can execute related statements.

Example:

if实例:
age = 18
if age >= 18:
  print(&#39;你已成年!&#39;)
else:
  print(&#39;你未成年!&#39;)

A large number of free learning recommendations, please visitpython tutorial(Video)

The above is the detailed content of What to do if there are multiple conditions in Python. 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