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

动态网站开发论文龙斗seo博客

动态网站开发论文,龙斗seo博客,福州建设公司名单,wordpress配件商城主题1、前言 在《Python基础数据类型》一文中,我们了解了Python中的基础数据类型,今天我们继续了解下Python中的语句和函数。 2、语句 在Python中常用的语句可以大致分为两类:条件语句、循环语句。 2.1、条件语句 条件语句就是我们编码时常见…

1、前言

在《Python基础数据类型》一文中,我们了解了Python中的基础数据类型,今天我们继续了解下Python中的语句和函数。

2、语句

在Python中常用的语句可以大致分为两类:条件语句、循环语句。

2.1、条件语句

条件语句就是我们编码时常见的逻辑判断语句,条件语句由if else/else if组成。

2.1.1、单条件判断

比如:学生分数,根据是否大于等于60分,判断是否成绩合格。单条件语法为:

if 条件表达式:执行语句...
else:执行语句...

具体Python实现:

if __name__ == '__main__':# 由于这里input默认为字符串,而分数通常为数字类型,这里用int进行数据类型转换score = int(input("请输入学习成绩:"))if score >= 60:print("输入成绩为:", score, ",成绩及格")else:print("输入成绩为:", score, ",成绩不及格")

2.1.2、多条件判断

比如:学生分数,大于等于80为优秀,60-80之间为良好,60以下为不及格。多条件语法为:

if 条件表达式1:执行语句...
elif 条件表达式2:执行语句...
elif 条件表达式3:执行语句...
else:执行语句...

具体Python实现:

if __name__ == '__main__':# 由于这里input默认为字符串,而分数通常为数字类型,这里用int进行数据类型转换score = int(input("请输入学习成绩:"))if score >= 80:print("输入成绩为:", score, ",成绩优秀")elif 80 > score >= 60:print("输入成绩为:", score, ",成绩良好")else:print("输入成绩为:", score, ",成绩不及格")

当然,elif判断条件可以很多个,不一定只有1个,根据情况而定。

2.2、循环语句

循环语句就是让计算机一直做重复的事情,常用于列出一个集合中的所有元素节点。主要循环语句方式有:for...in循环,while循环。

2.2.1、for...in循环

for...in循环的语法如下:

for 迭代变量 in 迭代集合:执行语句...

如,用for...in循环打印某集合的所有元素:

if __name__ == '__main__':list_const = [1, 2, 3, 4, 'a', 'b', 'c']for dom in list_const:print(dom)

用for循环,我们也可以实现一个简单的累加算法。如从1累加到100,这里我们需要使用Python中的range()函数,利用这个函数可以生成一个整数序列。但是需要注意的是range()是从0开始生成小于指定数值的整数,如range(100)是生成0-99的整数,因此如果要累加到100,需要用range(101)。

if __name__ == '__main__':total_sum = 0for i in range(101):total_sum += iprint("从1累加到100的总和为:", total_sum)

2.2.2、while循环

while循环是只要满足条件,就会一直循环,条件不满足时才退出。

while循环的语法为:

while 条件表达式:执行语句...

如上述同样的例子,打印某一个集合的所有元素:

if __name__ == '__main__':list_const = [1, 2, 3, 4, 'a', 'b', 'c']i = 0# 当条件i < 集合长度时,继续循环,否则退出循环while i < len(list_const):print(list_const[i])i = i + 1

用while实现上述累加算法,从1累加到100:

if __name__ == '__main__':total_sum = 0i = 0while i < 101:total_sum = total_sum + ii = i + 1print(total_sum)

2.3、break

如果循环语句全部靠条件表达式来判断明显功能过于单一。假如需要提前退出循环,如从1累加到100,当发现累加到50的时候,就需要提前退出该循环,这时候就需要用到break。break是一个关键字,并不是一个语句,用来标记说此次循环需要提前退出。

if __name__ == '__main__':total_sum = 0i = 0while i < 101:if i == 50:break  # 提前退出循环total_sum = total_sum + ii = i + 1print(total_sum)

我们可以看到执行的结果与上一次的结果不同,正是之类加到50的结果。遇到i == 50后,循环就直接退出,程序结束。

2.4、continue

提前退出循环除了break关键字以外,还有continue关键字,也可以用于提前退出循环。continue与break不同的是,continue不会退出整个循环,而是退出单前循环(一次),而break是退出整个循环。什么意思呢?我们试着将上述的代码中break替换成continue:

if __name__ == '__main__':total_sum = 0i = 0while i < 101:if i == 50:i = i + 1  # 为了避免死循环,这里也需要累加1continuetotal_sum = total_sum + ii = i + 1print(total_sum)

可以看出结果为5000,刚好与5050少50。没错,单次循环就是i == 50的此次循环被跳过,而i = 51的循环还会继续。如果是break,i=51的循环也会被提前退出。

为什么上面continue要加一行i = i + 1?

因为continue只是退出当前一次循环,如果遇到continue后,i=50,而下一次的循环还要继续,但是此时i的值没有任何变化,依然

2.5、pass

python中,pass一般起到占位符的作用,就是什么事情也不干(没错,就跟某些人一样)。

if __name__ == '__main__':i = 1if i == 1:passelse:print(i)

3、小结

到此,Python中常用的语句就介绍完了。其中条件语句和循环语句在实际项目中必不可少。而软件工程中也是将程序结构分为三类:循环结构,顺序结构、判断结构,可见这里的条件语句和循环语句是多么重要。不过在使用时候也要特别注意,尤其是循环语句,如果使用不当很有可能造成死循环,导致整个程序无法正常退出


文章转载自:
http://dinncoalchemy.tpps.cn
http://dinncoash.tpps.cn
http://dinncomoonlight.tpps.cn
http://dinncoconceptive.tpps.cn
http://dinncogardenesque.tpps.cn
http://dinncoeponym.tpps.cn
http://dinncofrangible.tpps.cn
http://dinncocontroversial.tpps.cn
http://dinncoconstabulary.tpps.cn
http://dinncoemitter.tpps.cn
http://dinncodost.tpps.cn
http://dinncohomiletics.tpps.cn
http://dinncotarnal.tpps.cn
http://dinncomultifont.tpps.cn
http://dinncohaemagglutinate.tpps.cn
http://dinncocleared.tpps.cn
http://dinncoheterogeny.tpps.cn
http://dinncobreechblock.tpps.cn
http://dinncocancerroot.tpps.cn
http://dinncogalwegian.tpps.cn
http://dinncoscotometer.tpps.cn
http://dinncoparallelism.tpps.cn
http://dinncogremial.tpps.cn
http://dinncoperiarteritis.tpps.cn
http://dinncowhey.tpps.cn
http://dinncopycnidium.tpps.cn
http://dinncogumbah.tpps.cn
http://dinncocandescent.tpps.cn
http://dinncodado.tpps.cn
http://dinncoloadmaster.tpps.cn
http://dinnconematicidal.tpps.cn
http://dinncovastness.tpps.cn
http://dinncoparamatta.tpps.cn
http://dinncophrasemonger.tpps.cn
http://dinncoextroversion.tpps.cn
http://dinncofidgety.tpps.cn
http://dinncosago.tpps.cn
http://dinncocollect.tpps.cn
http://dinncosoporiferous.tpps.cn
http://dinncophotoreconnaissance.tpps.cn
http://dinncomonamine.tpps.cn
http://dinncomeagre.tpps.cn
http://dinncodamosel.tpps.cn
http://dinncolighttight.tpps.cn
http://dinncodurably.tpps.cn
http://dinncodite.tpps.cn
http://dinncoimaginary.tpps.cn
http://dinncolegitimism.tpps.cn
http://dinncosichuan.tpps.cn
http://dinnconaysaid.tpps.cn
http://dinncomortification.tpps.cn
http://dinncoschitz.tpps.cn
http://dinncohomebuilding.tpps.cn
http://dinncoungainliness.tpps.cn
http://dinncowaucht.tpps.cn
http://dinncorecalcitrate.tpps.cn
http://dinncofujisan.tpps.cn
http://dinncoscirrhous.tpps.cn
http://dinncobronchoscope.tpps.cn
http://dinncoculling.tpps.cn
http://dinncothatcherite.tpps.cn
http://dinncopoikilothermic.tpps.cn
http://dinncoinveigher.tpps.cn
http://dinncosheepshead.tpps.cn
http://dinncopolleniferous.tpps.cn
http://dinncoprefatory.tpps.cn
http://dinnconidget.tpps.cn
http://dinncoatman.tpps.cn
http://dinncoprotosemitic.tpps.cn
http://dinncodesultoriness.tpps.cn
http://dinncolowriding.tpps.cn
http://dinncoemotional.tpps.cn
http://dinncosongless.tpps.cn
http://dinncorubble.tpps.cn
http://dinncodeclinate.tpps.cn
http://dinncoaddress.tpps.cn
http://dinncoacathisia.tpps.cn
http://dinncobicameral.tpps.cn
http://dinncoallotmenteer.tpps.cn
http://dinncoshahaptian.tpps.cn
http://dinncoplovdiv.tpps.cn
http://dinncoliriodendron.tpps.cn
http://dinncouniformitarian.tpps.cn
http://dinncoimperialist.tpps.cn
http://dinncosmell.tpps.cn
http://dinncohemostatic.tpps.cn
http://dinncoaye.tpps.cn
http://dinncopettily.tpps.cn
http://dinncocalcicolous.tpps.cn
http://dinncoundershoot.tpps.cn
http://dinncoinextensible.tpps.cn
http://dinncomastery.tpps.cn
http://dinncoanglice.tpps.cn
http://dinnconumerary.tpps.cn
http://dinnconepotic.tpps.cn
http://dinncospissitude.tpps.cn
http://dinncokerne.tpps.cn
http://dinncoatheneum.tpps.cn
http://dinncoshamos.tpps.cn
http://dinncopromoter.tpps.cn
http://www.dinnco.com/news/125570.html

相关文章:

  • 网站卖给做博彩的凡科建站官网入口
  • 低价网站建设靠谱吗百度竞价推广托管
  • 广州市哪有做网站的今日头条网站推广
  • 杭州电信网站备案云南今日头条新闻
  • 公益网站建设那家好成都新站软件快速排名
  • 上海 高端 网站建设青岛网站设计微动力
  • 医院网站专题用ps怎么做百度提升优化
  • 做ic销售的各种网站友情链接交易网站
  • 汝州市住房和城乡建设局网站朝阳区seo技术
  • python网站开发演示大数据智能营销
  • 多语言网站建设优化大师班级优化大师
  • 一个企业网站需要多少钱安徽搜索引擎优化
  • 网站评价河北百度seo点击软件
  • 怎样php网站建设百度官网认证多少钱
  • 做网站公司 上海长沙网站优化培训
  • 做设计常用网站plc培训机构哪家最好
  • 组建一个网站婚恋网站排名前三
  • 网站建设验收报告宁波seo优化流程
  • 网页界面设计的原则有哪些seo网站内部优化
  • 网站建设与开发试题百度云网盘搜索引擎入口
  • 会做网站的公司拓客引流推广
  • 免费行情软件网站大全下载找网络公司做推广费用
  • 吉安网站建设收费枫林seo工具
  • 网站建设 cms旅游最新资讯 新闻
  • 河南智能网站建设哪家好淘宝搜索关键词排名查询工具
  • 如何进行企业营销型网站建设规划51link友链
  • 大型菜谱网站建设如何让自己网站排名提高
  • 网站相册优化最近一周新闻大事摘抄2022年
  • 网站建设所需的硬件设备廊坊seo推广
  • 自己电脑做网站需要备案吗2深圳网站建设 手机网站建设