Python入门示例系列18 条件控制(Getting started with Python example series 18 conditional control)

Python入门示例系列18 条件控制

布尔值:True,False

比较操作运算符:

操作符 描述
< 小于
<= 小于或等于
> 大于
>= 大于或等于
== 等于,比较两个值是否相等
!= 不等于
<
<=
>
>=
==
!=

Python中 if 语句的一般形式如下所示:

if condition_1:
    statement_block_1
elif condition_2:
    statement_block_2
else:
    statement_block_3

if 语句包含零个或多个 子句,及可选的 子句。关键字 ” 是 ‘else if’ 的缩写,适用于避免过多的缩进。可以把 … … … 序列看作是其他语言中 或 语句的替代品。

elif
else
elif
if
elif
elif
switch
case

 注意:    1、每个条件后面要使用冒号 :,表示接下来是满足条件后要执行的语句块。    2、使用缩进来划分语句块,相同缩进数的语句在一起组成一个语句块。    3、在Python中没有switch – case语句。

if 语句, 单分支

x = 90if x >= 60:    print("Pass")

if-else 语句,二分支

x = 90
if x >= 60:
    print("Pass")
else:
    print("Failed")

if-elif-else 语句,多分支

x = 90
if x >= 90:
    print("优秀")
elif x>= 80:
    print("良好")
elif x>= 70:
    print("中等")
elif x>= 60:
    print("及格")
else:
    print("不及格")

if 嵌套

在嵌套 if 语句中,可以把 if…elif…else 结构放在另外一个 if…elif…else 结构中。

if 表达式1:
    语句
    if 表达式2:
        语句
    elif 表达式3:
        语句
    else:
        语句
elif 表达式4:
    语句
else:
    语句
num = int(input("输入一个数字:"))
if num % 2 == 0:
    if num % 3 == 0:
        print("可以整除 2 和 3")
    else:
        print("可以整除 2,但不能整除 3")
else:
    if num % 3 == 0:
        print("可以整除 3,但不能整除 2")
    else:
        print("不能整除 2 和 3")

REF

https://www.runoob.com/python3/python3-conditional-statements.html

————————

Getting started with Python example series 18 conditional control

Boolean: true, false

Comparison operator:

操作符 描述
< 小于
<= 小于或等于
> 大于
>= 大于或等于
== 等于,比较两个值是否相等
!= 不等于
<
<=
>
>=
==
!=

The general form of if statement in Python is as follows:

if condition_1:
    statement_block_1
elif condition_2:
    statement_block_2
else:
    statement_block_3

The if statement contains zero or more clauses and optional clauses. Keyword ” is an abbreviation for ‘else if’ and is intended to avoid excessive indentation. You can Sequences are seen as substitutes for or statements in other languages.

elif
else
elif
if
elif
elif
switch
case

Note: 1. Each condition should be followed by a colon:, which indicates the statement block to be executed after the condition is met. 2. Indent is used to divide statement blocks. Statements with the same number of indents form a statement block. 3. There is no switch – case statement in Python.

< strong > if statement, single branch < / strong >

x = 90if x >= 60:    print("Pass")

< strong > if else statement, two branches < / strong >

x = 90
if x >= 60:
    print("Pass")
else:
    print("Failed")

< strong > if elif else statement, multi branch < / strong >

x = 90
if x >= 90:
    print("优秀")
elif x>= 80:
    print("良好")
elif x>= 70:
    print("中等")
elif x>= 60:
    print("及格")
else:
    print("不及格")

if 嵌套

In a nested if statement, you can put if elif… Else structure in another if elif… Else structure.

if 表达式1:
    语句
    if 表达式2:
        语句
    elif 表达式3:
        语句
    else:
        语句
elif 表达式4:
    语句
else:
    语句
num = int(input("输入一个数字:"))
if num % 2 == 0:
    if num % 3 == 0:
        print("可以整除 2 和 3")
    else:
        print("可以整除 2,但不能整除 3")
else:
    if num % 3 == 0:
        print("可以整除 3,但不能整除 2")
    else:
        print("不能整除 2 和 3")

REF

https://www.runoob.com/python3/python3-conditional-statements.html