浅析Python基础-流程控制


Posted in Python onMarch 18, 2016

Python编程语言的作用非常强大,而且其应用方便的特点也对开发人员起到了非常大的作用。在这里我们就可以先从Python流程控制关键字的相关概念开始了解,从而初步掌握这一语言的特点。

Python是一种解释型、面向对象、动态数据类型的高级程序设计语言。

Python由Guido van Rossum于1989年底发明,第一个公开发行版发行于1991年。

像Perl语言一样, Python 源代码同样遵循 GPL(GNU General Public License)协议。

哪门语言都会有流程控制,即if switch while等语句。

应该是每种语言路程控制的原理、作用都是相近的,只是表达形式有所差异而已。

今天就跟大家分享一个Python中的条件、循环等语句。

这里最主要不再用大括号了,而是代码块。

首先需要注意的是if和else之后的冒号:

if

<code class="hljs lua">name = raw_input("What is your name? ")
if(name.endswith('Gumby')):
print 'Hello, Mr. Gumby'</code>

else

<code class="hljs lua">name = raw_input("What is your name? ")
if(name.endswith('Gumby')):
print 'Hello, Mr. Gumby'
else:
print 'Hello, Stranger'</code>

elif

这里需要注意一下,我们再C++中使用的else if语句,在Python中直接写作:elif

<code class="hljs python">num = input('Enter a number: ')
if num >0 :
print 'The number is positive'
elif num < 0:
print 'The number is negative'
else:
print '0'</code>

while

<code class="hljs lua">x = 1
while x <= 100
print x
x += 1</code>

for循环

能使用for,就尽量避免使用while

<code class="hljs livecodeserver">words = ['this', 'is', 'an', 'ex', 'parrot']
for word in words:
print word</code>

for遍历字典

<code class="hljs lua">d = {'x':1, 'y':2, 'z':3}
for key in d:
print key, 'corresponds to', d[key]</code>

zip并行迭代

<code class="hljs lua">names = ['name', 'beth', 'george', 'damo']
ages = [12, 45, 32, 99]
for name, age in zip(names, ages):
print name, 'is', age, 'years old'</code>

break跳出循环

continue

好了,关于Python基础-流程控制小编就给大家介绍到这里,希望对大家有所帮助!

Python 相关文章推荐
Django中的CACHE_BACKEND参数和站点级Cache设置
Jul 23 Python
实践Python的爬虫框架Scrapy来抓取豆瓣电影TOP250
Jan 20 Python
windows下安装Python的XlsxWriter模块方法
May 03 Python
对python list 遍历删除的正确方法详解
Jun 29 Python
Ubuntu18.04中Python2.7与Python3.6环境切换
Jun 14 Python
python读写csv文件实例代码
Jul 05 Python
pytorch 获取层权重,对特定层注入hook, 提取中间层输出的方法
Aug 17 Python
利用python-docx模块写批量生日邀请函
Aug 26 Python
使用python实现微信小程序自动签到功能
Apr 27 Python
浅谈python处理json和redis hash的坑
Jul 16 Python
python使用正则表达式匹配txt特定字符串(有换行)
Dec 09 Python
python线程优先级队列知识点总结
Feb 28 Python
浅析Python编写函数装饰器
Mar 18 #Python
python正则表达式之作业计算器
Mar 18 #Python
基于python yield机制的异步操作同步化编程模型
Mar 18 #Python
理解Python中的With语句
Mar 18 #Python
简述Python中的进程、线程、协程
Mar 18 #Python
Python实现计算最小编辑距离
Mar 17 #Python
Python引用模块和查找模块路径
Mar 17 #Python
You might like
简单的移动设备检测PHP脚本代码
2011/02/19 PHP
微信自定义菜单的创建/查询/取消php示例代码
2016/08/05 PHP
微信接口生成带参数的二维码
2017/07/31 PHP
关于laravel模板中生成URL的几种模式总结
2019/10/18 PHP
比较全的JS checkbox全选、取消全选、删除功能代码
2008/12/19 Javascript
JavaScript 格式字符串的应用
2010/03/29 Javascript
js脚本实现数据去重
2014/11/27 Javascript
简易的JS计算器实现代码
2016/10/18 Javascript
AngularJS动态绑定HTML的方法分析
2016/11/07 Javascript
JavaScript比较同一天的时间大小实例代码
2018/02/09 Javascript
jquery.param()实现数组或对象的序列化方法
2018/10/08 jQuery
微信小程序实现工作时间段选择
2019/02/15 Javascript
vue语法自动转typescript(解放双手)
2019/09/18 Javascript
使用Python下载歌词并嵌入歌曲文件中的实现代码
2015/11/13 Python
举例讲解Python中的Null模式与桥接模式编程
2016/02/02 Python
详解Python的Flask框架中的signals信号机制
2016/06/13 Python
Python 两个列表的差集、并集和交集实现代码
2016/09/21 Python
Python实现的概率分布运算操作示例
2017/08/14 Python
Python实现的堆排序算法原理与用法实例分析
2017/11/22 Python
python re模块findall()函数实例解析
2018/01/19 Python
python 删除列表里所有空格项的方法总结
2018/04/18 Python
python3学生名片管理v2.0版
2018/11/29 Python
pygame实现俄罗斯方块游戏(基础篇2)
2019/10/29 Python
HTML5实现应用程序缓存(Application Cache)
2020/06/16 HTML / CSS
美国户外生活方式品牌:Eddie Bauer
2016/12/28 全球购物
英国殿堂级有机护肤品牌:Rodial
2017/04/17 全球购物
英国优质鞋类专家:Robinson’s Shoes
2017/12/08 全球购物
Linux常见面试题
2013/03/18 面试题
弄虚作假心得体会
2014/09/10 职场文书
2014年卫生保健工作总结
2014/12/08 职场文书
顶岗实习计划书
2015/01/16 职场文书
2015年出纳工作总结与计划
2015/05/18 职场文书
证婚人致辞精选
2015/07/28 职场文书
拙作再改《我的收音机情缘》
2022/04/05 无线电
python开发制作好看的时钟效果
2022/05/02 Python
使用compose函数优化代码提高可读性及扩展性
2022/06/16 Javascript