当前位置: 首页 > news >正文

广告企业网站模板广州网站建设工作室

广告企业网站模板,广州网站建设工作室,启东市建设局网站,网站建设和管理制度✍面向读者:所有人 ✍所属专栏:零基础入门Pythonhttps://blog.csdn.net/arthas777/category_12455877.html Python if语句 Python if语句的流程图 Python if语句示例 Python If-Else Statement Python if else语句的流程图 使用Python if-else语句 …

面向读者:所有人

所属专栏:零基础入门Pythonhttps://blog.csdn.net/arthas777/category_12455877.html

Python if语句

Python if语句的流程图

Python if语句示例

Python If-Else Statement

Python if else语句的流程图

使用Python if-else语句

列表理解中的Python if-else语句

Python中的嵌套If语句

Python嵌套if语句的流程图

Python嵌套if语句示例

Python if elif else Ladder

Python if elif else梯形图的流程图

if-elif-else梯形图

Python if elif else梯形图示例

Short Hand if语句

Python if简写示例

Short Hand if else语句

Python if else简写示例

在现实生活中,当我们需要做出一些决定时,我们会根据这些决定决定下一步应该做什么。类似的情况也出现在编程中,我们需要做出一些决定,并根据这些决定执行下一块代码。Python语言中的条件语句决定程序执行流的方向(控制流)。

Python中的控制流类型

Python控制流语句如下:

if语句

if-else语句

嵌套的if语句

if-elif-else梯子

Python if语句

if语句是最简单的决策语句。它用于决定是否执行某个语句或语句块。

语法:

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

这里,评估后的条件将是真或假。如果该语句接受布尔值&如果该值为true,则它将执行下面的语句块,否则不执行。
正如我们所知,python使用缩进来识别块。因此,if语句下的块将被识别,如下例所示:

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

Python if语句的流程图

Flowchart of Python if statement

Python if语句的流程图

Python if语句示例

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

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

Output: 

I am Not in if

Python If-Else Statement

单独的if语句告诉我们,如果条件为真,它将执行语句块,如果条件是假,它将不会执行。但是,如果条件为false,我们想做其他事情,那么当if条件为false时,我们可以将else语句与if语句一起使用来执行代码块。

Python的语法If Else:

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

Python if else语句的流程图

Flowchart of Python is-else statement

Python的流程图是else语句

使用Python if-else语句

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

Output: 

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

列表理解中的Python if-else语句

在这个例子中,我们在列表理解中使用if语句,条件是如果列表的元素是奇数,则其数字和将被存储,否则将不被存储。

# python program to illustrate If else statement#!/usr/bin/pythoni = 20if (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")
# Explicit functiondef digitSum(n):dsum = 0for ele in str(n):dsum += int(ele)return dsum# Initializing listList = [367, 111, 562, 945, 6726, 873]# Using the function on odd elements of the listnewList = [digitSum(i) for i in List if i & 1]# Displaying new listprint(newList)

Output :

[16, 3, 18, 18]

Python中的嵌套If语句

嵌套的if是另一个if语句的目标if语句。嵌套的if语句表示在另一个if语句中的if语句。是的,Python允许我们在if语句中嵌套if语句。即,我们可以将一个if语句放在另一个if声明中。

Syntax

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

Python嵌套if语句的流程图

Flowchart of Python Nested if statement

Python嵌套if语句的流程图

Python嵌套if语句示例

在这个例子中,我们在代码中显示嵌套的if条件,所有的if条件都将逐一执行。

# python program to illustrate nested If statementi = 10if (i == 10):#  First if statementif (i < 15):print("i is smaller than 15")# Nested - if statement# Will only be executed if statement above# it is trueif (i < 12):print("i is smaller than 12 too")else:print("i is greater than 15")

 

Output: 

i is smaller than 15
i is smaller than 12 too

Python if elif else Ladder

在这里,用户可以在多个选项中进行决定。if语句是自上而下执行的。一旦控制if的条件之一为true,则执行与该if相关联的语句,并绕过梯形图的其余部分。如果所有条件都不为真,那么将执行最后的else语句。

Syntax

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

Python if elif else梯形图的流程图

if-elif-else梯形图

Python if elif else梯形图示例

在该示例中,我们显示了单个if条件和多个elif条件,以及单个else条件。

# Python program to illustrate if-elif-else ladder#!/usr/bin/pythoni = 20if (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")

 

Output: 

i is 20

Short Hand if语句

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

Syntax: 

if condition: statement

Python if简写示例

在给定的示例中,我们有一个条件,即如果数字小于15,则将执行进一步的代码。

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

 

Output:

i is less than 15

Short Hand if else语句

这可以用于在单行中编写if-else语句,其中if和else块中都只需要一条语句。

Syntax:

statement_when_True if condition else statement_when_False

Python if else简写示例


在给定的例子中,如果数字是15,我们将打印True,否则将打印False。

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

Output: 

True
http://www.hengruixuexiao.com/news/12948.html

相关文章:

  • 报名窗口网站建设韩国热搜榜
  • 做百度手机网站排名百度大数据平台
  • 做网站是用c 吗旅游seo整站优化
  • 国外网站做任务赚钱的seo排名优化关键词
  • h5个人博客网站模板优秀营销软文范例100字
  • 网站推广搜索全媒体广告加盟
  • 接入服务商网站备案管理系统技术规范要求郑州网站推广优化
  • 广东网站开发收费互联网广告代理
  • 网站怎么查询注册商青岛seo全网营销
  • 深圳企业网站建设哪家专业镇江百度seo
  • 智慧团建网站登陆网上商城推广13种方法
  • 网站建设和微信小程序网络营销方案模板
  • 专业网站建设公司怎么选太原seo建站
  • 网站欣赏 公司网站案例全网搜索引擎
  • 网站整站模板谷歌怎么推广自己的网站
  • 做网站用笔记本电脑微商软文
  • 2015做导航网站抖音seo系统
  • 140平米装修多少钱沈阳seo优化新势力
  • 做网站一定要psd吗网站制作软件免费下载
  • 网站管理助手 二级域名免费二级域名建站
  • 生物科技网站建设 中企动力北京深圳外包网络推广
  • 国外小型网站营销软文
  • 中山手机网站建设报价seo查询在线
  • 超市网站建设爱站长尾关键词挖掘工具
  • 网站标题应怎设置推广方案框架
  • 东营市公司网站建设价格seo快速排名关键词
  • 介休市网站建设公司百度手机端排名
  • 小说网站搭建教程百度人工服务24小时
  • 自己做的网站怎么放到网上去电脑培训学校学费多少
  • 自己做网站要不要钱免费放单平台无需垫付