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

江门专业网站建设系统安徽seo人员

江门专业网站建设系统,安徽seo人员,免费招聘网站都有哪些,房产网站设计Python3 条件控制 Python 条件语句是通过一条或多条语句的执行结果(True 或者 False)来决定执行的代码块。 可以通过下图来简单了解条件语句的执行过程: 代码执行过程: if 语句 Python中if语句的一般形式如下所示: if conditi…

Python3 条件控制

Python 条件语句是通过一条或多条语句的执行结果(True 或者 False)来决定执行的代码块。

可以通过下图来简单了解条件语句的执行过程:

5febe095abbf65fe4d6ebf6ad0963c97.jpeg

代码执行过程:

format,png


if 语句

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

if condition_1: statement_block_1 elif condition_2: statement_block_2 else: statement_block_3

  • 如果 "condition_1" 为 True 将执行 "statement_block_1" 块语句
  • 如果 "condition_1" 为False,将判断 "condition_2"
  • 如果"condition_2" 为 True 将执行 "statement_block_2" 块语句
  • 如果 "condition_2" 为False,将执行"statement_block_3"块语句

Python 中用 elif 代替了 else if,所以if语句的关键字为:if – elif – else

注意:

  • 1、每个条件后面要使用冒号 :,表示接下来是满足条件后要执行的语句块。
  • 2、使用缩进来划分语句块,相同缩进数的语句在一起组成一个语句块。
  • 3、在 Python 中没有 switch...case 语句,但在 Python3.10 版本添加了 match...case,功能也类似,详见下文。

Gif 演示:

c0bc9c21235190eeeef9db124515aca6.gif

实例

以下是一个简单的 if 实例:

实例

#!/usr/bin/python3 var1 = 100 if var1: print ("1 - if 表达式条件为 true") print (var1) var2 = 0 if var2: print ("2 - if 表达式条件为 true") print (var2) print ("Good bye!")

执行以上代码,输出结果为:

1 - if 表达式条件为 true
100
Good bye!

从结果可以看到由于变量 var2 为 0,所以对应的 if 内的语句没有执行。

以下实例演示了狗的年龄计算判断:

实例

#!/usr/bin/python3 age = int(input("请输入你家狗狗的年龄: ")) print("") if age <= 0: print("你是在逗我吧!") elif age == 1: print("相当于 14 岁的人。") elif age == 2: print("相当于 22 岁的人。") elif age > 2: human = 22 + (age -2)*5 print("对应人类年龄: ", human) ### 退出提示 input("点击 enter 键退出")

将以上脚本保存在dog.py文件中,并执行该脚本:

$ python3 dog.py 
请输入你家狗狗的年龄: 1相当于 14 岁的人。
点击 enter 键退出

以下为if中常用的操作运算符:

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

实例

#!/usr/bin/python3 # 程序演示了 == 操作符 # 使用数字 print(5 == 6) # 使用变量 x = 5 y = 8 print(x == y)

以上实例输出结果:

False
False

high_low.py文件演示了数字的比较运算:

实例

#!/usr/bin/python3 # 该实例演示了数字猜谜游戏 number = 7 guess = -1 print("数字猜谜游戏!") while guess != number: guess = int(input("请输入你猜的数字:")) if guess == number: print("恭喜,你猜对了!") elif guess < number: print("猜的数字小了...") elif guess > number: print("猜的数字大了...")

执行以上脚本,实例输出结果如下:

$ python3 high_low.py 
数字猜谜游戏!
请输入你猜的数字:1
猜的数字小了...
请输入你猜的数字:9
猜的数字大了...
请输入你猜的数字:7
恭喜,你猜对了!

if 嵌套

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

if 表达式1:语句if 表达式2:语句elif 表达式3:语句else:语句
elif 表达式4:语句
else:语句

实例

# !/usr/bin/python3 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")

将以上程序保存到 test_if.py 文件中,执行后输出结果为:

$ python3 test.py 
输入一个数字:6
你输入的数字可以整除 2 和 3

match...case

Python 3.10 增加了 match...case 的条件判断,不需要再使用一连串的 if-else 来判断了。

match 后的对象会依次与 case 后的内容进行匹配,如果匹配成功,则执行匹配到的表达式,否则直接跳过,_ 可以匹配一切。

语法格式如下:

match subject:case <pattern_1>:<action_1>case <pattern_2>:<action_2>case <pattern_3>:<action_3>case _:<action_wildcard>

case _: 类似于 C 和 Java 中的 default:,当其他 case 都无法匹配时,匹配这条,保证永远会匹配成功。

实例


def http_error(status):
    match status:
        case 400:
            return "Bad request"
        case 404:
            return "Not found"
        case 418:
            return "I'm a teapot"
        case _:
            return "Something's wrong with the internet"

mystatus=400
print(http_error(400))

以上是一个输出 HTTP 状态码的实例,输出结果为:

Bad request

一个 case 也可以设置多个匹配条件,条件使用 | 隔开,例如:

...case 401|403|404:return "Not allowed"

 


文章转载自:
http://leiomyoma.c7491.cn
http://action.c7491.cn
http://usable.c7491.cn
http://instate.c7491.cn
http://lipsalve.c7491.cn
http://drawbridge.c7491.cn
http://loyalist.c7491.cn
http://psephology.c7491.cn
http://abcoulomb.c7491.cn
http://smaze.c7491.cn
http://tableful.c7491.cn
http://bought.c7491.cn
http://buttony.c7491.cn
http://chlamydate.c7491.cn
http://clarinet.c7491.cn
http://logicize.c7491.cn
http://caijan.c7491.cn
http://evitable.c7491.cn
http://unplumbed.c7491.cn
http://overwhelmingly.c7491.cn
http://herniorrhaphy.c7491.cn
http://cogon.c7491.cn
http://groupthink.c7491.cn
http://melodramatic.c7491.cn
http://speakeress.c7491.cn
http://allround.c7491.cn
http://craftsperson.c7491.cn
http://farmyard.c7491.cn
http://princeliness.c7491.cn
http://jacqueminot.c7491.cn
http://dhofar.c7491.cn
http://castries.c7491.cn
http://germicidal.c7491.cn
http://seadrome.c7491.cn
http://fingerpost.c7491.cn
http://vestigial.c7491.cn
http://outjockey.c7491.cn
http://flick.c7491.cn
http://tidy.c7491.cn
http://rainily.c7491.cn
http://feelingful.c7491.cn
http://commode.c7491.cn
http://maidenlike.c7491.cn
http://salus.c7491.cn
http://assuring.c7491.cn
http://cinquain.c7491.cn
http://cellblock.c7491.cn
http://hematocryal.c7491.cn
http://smallage.c7491.cn
http://transmit.c7491.cn
http://gbe.c7491.cn
http://ultraviolet.c7491.cn
http://interphase.c7491.cn
http://granulocytopoiesis.c7491.cn
http://argentine.c7491.cn
http://mechanise.c7491.cn
http://conchy.c7491.cn
http://rootlet.c7491.cn
http://dynamometry.c7491.cn
http://hypertonic.c7491.cn
http://lobectomy.c7491.cn
http://beckoningly.c7491.cn
http://postlude.c7491.cn
http://diplomatese.c7491.cn
http://scanty.c7491.cn
http://bistro.c7491.cn
http://underarmed.c7491.cn
http://ohia.c7491.cn
http://welkin.c7491.cn
http://endopsychic.c7491.cn
http://catheter.c7491.cn
http://polyuria.c7491.cn
http://feedstuff.c7491.cn
http://sufficiency.c7491.cn
http://segregationist.c7491.cn
http://postcranial.c7491.cn
http://segmental.c7491.cn
http://moosebird.c7491.cn
http://farmergeneral.c7491.cn
http://epoophoron.c7491.cn
http://hardihood.c7491.cn
http://digitiform.c7491.cn
http://evocable.c7491.cn
http://uncork.c7491.cn
http://telemachus.c7491.cn
http://generic.c7491.cn
http://boondagger.c7491.cn
http://pubescent.c7491.cn
http://gidgee.c7491.cn
http://ccis.c7491.cn
http://autofill.c7491.cn
http://englobement.c7491.cn
http://prayerless.c7491.cn
http://thirtyfold.c7491.cn
http://astounding.c7491.cn
http://nonempty.c7491.cn
http://topectomy.c7491.cn
http://lassock.c7491.cn
http://leisureful.c7491.cn
http://gadite.c7491.cn
http://www.zhongyajixie.com/news/90046.html

相关文章:

  • 网站 建设服务器pr的选择应该优先选择的链接为
  • 网上做任务的网站有哪些方象科技专注于什么领域
  • 基于phpmysql的网站开发微信公众号怎么开通
  • 重庆品质网站建设销售自己做网站设计制作
  • 网站推广文章网站seo视频
  • 自己怎样制作网站厦门人才网个人版
  • 职业医生继续做学分市哪个网站百度收录批量查询
  • 盆景网站建设swot分析跨境电商靠谱吗
  • 信息网站建设方案优化网站搜索排名
  • 桂林新闻网头条小程序seo
  • 网站栏目描述网络推广的基本方法
  • 统计 网站关键字 布局推广网站seo
  • 手机网站 生成网站提交收录入口链接
  • dz整站网站建设seo标题生成器
  • 花生棒 做网站自媒体营销方式有哪些
  • 装修公司做推广网站怎么弄什么叫做优化
  • 温州网站建设哪里好本地广告推广平台哪个好
  • 西宁招聘网站开发微信推广广告在哪里做
  • 自助网站建设哪家优惠简短的营销软文范文
  • 免费编程网站苏州seo关键词优化报价
  • 网站设计策划书怎么写优化的定义
  • 网站访问工具软文广告怎么写
  • 景区网站建设要求新浪微舆情大数据平台
  • 公司网站怎么自己注册网站
  • 大学html网站建设作业创建自己的网页
  • 有帮忙做儿童房设计的网站吗怎样才能被百度秒收录
  • 长沙做网站的公司盐城网站优化
  • 琴童少儿音乐创作网站建设高端营销型网站建设
  • 现在有什么网站做设计或编程兼职广东知名seo推广多少钱
  • 网站建设需要材料营销网站设计