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')].
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), < (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……
Example
1. Use and for multi-condition judgment:
if name == 'zs' and age == 18: print('name: zs, age: 18')
2. Use or for multi-condition judgment:
if passwd == '123456' or passwd == 'abcdef' print('welcome!')
3. Use elif for multi-condition judgment:
if user == 'zs'; print('hi zs') elif user == 'ls': print('hi li')
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('你已成年!') else: print('你未成年!')
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!