What to do if there are multiple conditions in Python

coldplay.xixi
Release: 2020-10-21 14:09:37
Original
26393 people have browsed it

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: 执行语句……
Copy after login

The judgment condition of the if statement can be > (greater than), < (less than), == (equal to), >= (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……
Copy after login

Example

1. Use and for multi-condition judgment:

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

2. Use or for multi-condition judgment:

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

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;)
Copy after login

Example extension:

.Basic usage of if conditional statement:

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

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;)
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template