python基础入门----条件控制

原文链接:https://www.programiz.com/python-programming/if-elif-else

Python if...else Statement

Table of Contents

if...else语句

表格内容

  • 什么是if...else语句
  1. if 语句语法
  2. if语句流程图
  3. 例子:if语句
  • if...else语句
  1. if...else 语句语法
  2. if...else语句流程图
  3. 例子:if...else语句
  • if...elis...else语句
  1. if...elis...else 语句语法
  2. if...elis...else语句流程图
  3. 例子:if...elis...else语句
  • if语句嵌套

What are if...else statement in Python?

Decision making is required when we want to execute a code only if a certain condition is satisfied.

The if…elif…else statement is used in Python for decision making.

什么是if...语句?

决定一个需求,只有在确认一个条件被满足时,我们才去执行一段代码。

在python中这个if...eslif...esle语句被用于做决定。


Python if Statement Syntax

if test expression:
    statement(s)

Here, the program evaluates the test expression and will execute statement(s) only if the text expression is True.

If the text expression is False, the statement(s) is not executed.

In Python, the body of the if statement is indicated by the indentation. Body starts with an indentation and the first unindented line marks the end.

Python interprets non-zero values as TrueNone and 0 are interpreted as False.

if语句语法

if test expression:
   statement(s)

这里,这个程序评估这个test expression,再将执行语法如果这个文本表达式正确。

如果这个文本表达式是错误的,这个语句不执行。

在python,这if语句是的主体被缩进表示,缩进是主体开始了,首行不缩进标志结束。

python解释器非零值是True。None和0在解释器是False。


Python if Statement Flowchart

if语句流程图

Flowchart of if statement in Python programming


Example: Python if Statement

# If the number is positive, we print an appropriate message

num = 3
if num > 0:
    print(num, "is a positive number.")
print("This is always printed.")

num = -1
if num > 0:
    print(num, "is a positive number.")
print("This is also always printed.")

When you run the program, the output will be:

3 is a positive number
This is always printed
This is also always printed.

例子:if语法

当你运行程序是,结果输出:

>>> num = 3
>>> if num > 0:
...    print(num,"is a positive number.")
...
3 is a positive number.   
>>> print("this is always printed.")
this is always printed.
>>>
>>>
>>> num = -1
>>> if num > 0:
...    print(num,"is a positive number.")
...
>>> print("This is also always printed.")
This is also always printed.

In the above example, num > 0 is the test expression.

The body of if is executed only if this evaluates to True.

When variable num is equal to 3, test expression is true and body inside body of if is executed.

If variable num is equal to -1, test expression is false and body inside body of if is skipped.

The print() statement falls outside of the if block (unindented). Hence, it is executed regardless of the test expression.

 以上程序,num>0是测试条件(test expression)。

只有被估算是True,if的主体被执行。

当变量是等于3,测试条件是正确的,if主体里边的主体被执行。

如果变量等于-1,测试条件是错误的,if主体里边的主体被跳过。

这print()语句不在if块(没有缩进),因此,它被执行。测试条件不用管。


Python if...else Statement

Syntax of if...else

if test expression:
    Body of if
else:
    Body of else

The if..else statement evaluates test expression and will execute body of if only when test condition is True.

If the condition is False, body of else is executed. Indentation is used to separate the blocks.

if...else 语句

if...else的语法

if test expression:
    Body of if
else:
    Body of else

这if...else语句评估test expression,当只有测试条件是True时,if的主体被执行。

如果条件是False的,else的主体被执行, 缩进用于分离代码块。


Python if..else Flowchart

if..else流程图

Flowchart of if...else statement in Python Programming

Example of if...else

# Program checks if the number is positive or negative
# And displays an appropriate message

num = 3

# Try these two variations as well. 
# num = -5
# num = 0

if num >= 0:
    print("Positive or Zero")
else:
    print("Negative number")

In the above example, when num is equal to 3, the test expression is true and body of if is executed and body of else is skipped.

If num is equal to -5, the test expression is false and body of else is executed and body ofif is skipped.

If num is equal to 0, the test expression is true and body of if is executed and body of else is skipped.

 if...else例子

>>> num = 3
>>> if num >= 0:
...    print("positive or zero")
... else:
...    print("Negative number")
...
positive or zero

在以上程序,当数字是等于3,这个测试表达式是正确的,if的主体被执行,eles的主体被跳过。

如果数字是等于-5,这个测试表达式是错误的,else的主体被执行,if的主体被跳过。

如果数字是等于0,这个测试表达式是正确的,if的主体被执行,else的主体被跳过。


Python if...elif...else Statement

Syntax of if...elif...else

if test expression:
    Body of if
elif test expression:
    Body of elif
else: 
    Body of else

The elif is short for else if. It allows us to check for multiple expressions.

If the condition for if is False, it checks the condition of the next elif block and so on.

If all the conditions are False, body of else is executed.

Only one block among the several if...elif...else blocks is executed according to the condition.

The if block can have only one else block. But it can have multiple elif blocks.

if...elif...else 语句

if...elif...else 语法

if test expression:
    Body of if
elif test expression:
    Body of elif
else: 
    Body of else

这个elif是else if的缩写,它预先我们去检查多个表达式。

如果if这个条件是False,它检查条件下一个elif代码块等等。

如果所有的条件是False的,else的主体被执行。

几个在if...elif..else模块只有根据条件仅仅执行一个代码块。

这个if模块可以有且仅有一个esle模块,但是可以有多个elif模块。


Flowchart of if...elif...else

if...elif...else 流程图

Flowchart of if...elif....else in python programming

Example of if...elif...else

# In this program, 
# we check if the number is positive or
# negative or zero and 
# display an appropriate message

num = 3.4

# Try these two variations as well:
# num = 0
# num = -4.5

if num > 0:
    print("Positive number")
elif num == 0:
    print("Zero")
else:
    print("Negative number")

When variable num is positive, Positive number is printed.

If num is equal to 0, Zero is printed.

If num is negative, Negative number is printed

if...elif...else例子

>>> num = 3.4
>>>
>>> if num > 0:
...    print("positive number")
... elif num == 0:
...    print("zero")
... else:
...    print("Negative number")
...
positive number

 当变量是正数,positive number被打印。

 如果数字等于0,zero被打印。

如果数字等于负数,negative number 被打印


Python Nested if statements

We can have a if...elif...else statement inside another if...elif...else statement. This is called nesting in computer programming.

Any number of these statements can be nested inside one another. Indentation is the only way to figure out the level of nesting. This can get confusing, so must be avoided if we can.

if内嵌语法

当我们有一个if...elif...else语句里面有另外一个if...elif...else语句时。在电脑程序里叫做内嵌。

这些语句的任何数字都可以内嵌到另外一个。缩进是解决多层内嵌的唯一方式。


Python Nested if Example

# In this program, we input a number
# check if the number is positive or
# negative or zero and display
# an appropriate message
# This time we use nested if

num = float(input("Enter a number: "))
if num >= 0:
    if num == 0:
        print("Zero")
    else:
        print("Positive number")
else:
    print("Negative number")

Output 1

Enter a number: 5
Positive number

Output 2

Enter a number: -1
Negative number

Output 3

Enter a number: 0
Zero

if内嵌的例子

输出1

 num = float(input("Enter a number: "))
Enter a number: 5 //输入数据
>>> if num >= 0:
...   if num == 0:
...      print("zero")
...   else:
...      print("positive number")
... else:
...    print("negative number")
...
positive number   //输出结果

输出2

 num = float(input("Enter a number: "))
Enter a number: -1 //输入数据
>>> if num >= 0:
...   if num == 0:
...      print("zero")
...   else:
...      print("positive number")
... else:
...    print("negative number")
...
negative number   //输出结果

输出3 

 num = float(input("Enter a number: "))
Enter a number: 0 //输入数据
>>> if num >= 0:
...   if num == 0:
...      print("zero")
...   else:
...      print("positive number")
... else:
...    print("negative number")
...
zero   //输出结果

Check out these examples to learn more:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值