Python基本语法:if-else条件语句使用指南

2021年3月13日15:36:23 发表评论 1,136 次浏览

在现实生活中, 有时候需要做出一些决定, 然后根据这些决定来决定下一步该做什么。在编程中也会出现类似的情况, 我们需要做出一些决策, 然后基于这些决策, 我们将执行下一段代码。

编程语言的决策陈述决定了程序执行流程的方向。 python中可用的决策语句为:

  • if声明
  • if..else语句
  • 嵌套if语句
  • if-elif梯子
  • 简述if
  • 简短的if-else声明

if声明

if语句是最简单的决策语句。它用于决定是否执行某个语句或语句块, 即, 如果某个条件为真, 则执行语句块, 否则不执行。

语法:

if condition:           
   # Statements to execute if
   # condition is true

在此, 评估后的条件为真或假。 if语句接受布尔值-如果该值为true, 则它将执行其下面的语句块, 否则不执行。我们可以用健康)状况加上方括号"(‘’)"。

众所周知, python使用缩进来识别块。因此, 将确定if语句下的块, 如以下示例所示:

if condition:
   statement1
statement2

# Here if the condition is true, if block 
# will consider only statement1 to be inside 
# its block.

流程图:-

Java中的if语句
# python program to illustrate If statement
  
i = 10
if (i > 15 ):
    print ( "10 is less than 15" )
print ( "I am Not in if" )

输出如下:

I am Not in if

由于if语句中存在的条件为false。因此, 不执行if语句下面的块。

如果别的

仅if语句就告诉我们, 如果条件为true, 则将执行语句块;如果条件为false, 则不会。但是如果条件为假, 我们想做其他事情怎么办。某物(人)到了

其他

声明。我们可以使用

其他

声明

if

条件为false时执行代码块的语句。

语法

:

if (condition):
    # Executes this block if
    # condition is true
else:
    # Executes this block if
    # condition is false

流程图:-

if-else语句
# python program to illustrate If else statement
#!/usr/bin/python
  
i = 20 ;
if (i < 15 ):
     print ( "i is smaller than 15" )
     print ( "i'm in if Block" )
else :
     print ( "i is greater than 15" )
     print ( "i'm in else Block" )
print ( "i'm not in if and not in else Block" )

输出如下:

i is greater than 15
i'm in else Block
i'm not in if and not in else Block

在调用不在块中(无空格)的语句后, 如果if语句中的条件为false, 则执行else语句之后的代码块。

嵌套如果

嵌套的if是if语句, 它是另一个if语句的目标。嵌套的if语句表示另一个if语句内的if语句。是的, Python允许我们在if语句中嵌套if语句。也就是说, 我们可以将if语句放在另一个if语句中。

语法:

if (condition1):
   # Executes when condition1 is true
   if (condition2): 
      # Executes when condition2 is true
   # if Block is end here
# if Block is end here

流程图:-

Nested_if
# python program to illustrate nested If statement
#!/usr/bin/python
i = 10
if (i = = 10 ):
     #  First if statement
     if (i < 15 ):
         print ( "i is smaller than 15" )
     # Nested - if statement
     # Will only be executed if statement above
     # it is true
     if (i < 12 ):
         print ( "i is smaller than 12 too" )
     else :
         print ( "i is greater than 15" )

输出如下:

i is smaller than 15
i is smaller than 12 too

if-elif-else梯子

在这里, 用户可以在多个选项中进行选择。 if语句从上至下执行。一旦控制if的条件之一为true, 则将执行与if关联的语句, 并跳过其余的阶梯。如果没有一个条件为真, 则将执行最终的else语句。

语法:-

if (condition):
    statement
elif (condition):
    statement
.
.
else:
    statement

流程图:-

if-elseif梯子

例:-

# Python program to illustrate if-elif-else ladder
#!/usr/bin/python
   
i = 20
if (i = = 10 ):
     print ( "i is 10" )
elif (i = = 15 ):
     print ( "i is 15" )
elif (i = = 20 ):
     print ( "i is 20" )
else :
     print ( "i is not present" )

输出如下:

i is 20

简述if

只要在if块中只有一条语句要执行, 则可以使用简写if。该语句可以与if语句放在同一行。

语法如下:

if condition: statement

例子:

# Python program to illustrate short hand if
i = 10
if i < 15 : print ( "i is less than 15" )

输出如下:

i is less than 15

简短的if-else声明

这可以用于在单行中编写if-else语句, 在if和else块中只有一个语句要执行。

语法如下:

statement_when_True if condition else statement_when_False

例子:

# Python program to illustrate short hand if-else
i = 10
print ( True ) if i < 15 else print ( False )

输出如下:

True

注意怪胎!巩固你的基础Python编程基础课程和学习基础知识。

首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。


木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: