python基础4--控制流
1、if语句
结构:
if condition:
do something
elif other_condition:
do something
number = 60
guess = int(input('Enter an integer : ')) if (guess == number):
# New block starts here
print('Bingo! you guessed it right.')
# New block ends here
elif (guess < number):
# Another block
print('No, the number is higher than that')
# You can do whatever you want in a block ...
else:
print('No, the number is a lower than that')
# you must have guessed > number to reach here print('Done')
2、循环语句允许我们执行一个语句或语句组多次,下面是在大多数编程语言中的循环语句的一般形式:

3、for语句
for i in range(1, 10):
print(i)
else:
print('The for loop is over') #遍历List
a_list = [1, 3, 5, 7, 9]
for i in a_list:
print(i) #遍历Tuple
a_tuple = (1, 3, 5, 7, 9)
for i in a_tuple:
print(i) #遍历Dict
a_dict = {'Tom':'', 'Jerry':'', 'Cathy':''}
for key in a_dict:
print(key, a_dict[key]) for (key, elem) in a_dict.items():
print(key, elem)
运行结果:

4、while语句
number = 59
guess_flag = False while (guess_flag == False):
guess = int(input('Enter an integer : '))
if guess == number:
guess_flag = True
elif guess < number:
print('No, the number is higher than that, keep guessing')
else:
print('No, the number is a lower than that, keep guessing')
print('Bingo! you guessed it right.')
5、break, continue, pass
(1) break 语句:跳出循环
(2) continue 语句:进行下一次循环
(3) pass 语句:什么都不做
number = 59
while True:
guess = int(input('Enter an integer : '))
if guess == number:
break
if guess < number:
print('No, the number is higher than that, keep guessing')
continue
else:
print('No, the number is a lower than that, keep guessing')
continue print('Bingo! you guessed it right.')
print('Done')
python基础4--控制流的更多相关文章
- Python基础之控制流
介绍一些Python的基本的东西,你会发现,Python真的很简单.我也尽可能说得简单一些,因为我理解的也很简单. 在到目前为止我们所见到的程序中,总是有一系列的语句,Python忠实地按照它们的顺序 ...
- Python基础+Pythonweb+Python扩展+Python选修四大专题 超强麦子学院Python35G视频教程
[保持在百度网盘中的, 可以在观看,嘿嘿 内容有点多,要想下载, 回复后就可以查看下载地址,资源收集不易,请好好珍惜] 下载地址:http://www.fu83.cc/ 感觉文章好,可以小手一抖 -- ...
- python基础——继承实现的原理
python基础--继承实现的原理 1 继承顺序 class A(object): def test(self): print('from A') class B(A): def test(self) ...
- python基础语法及知识点总结
本文转载于星过无痕的博客http://www.cnblogs.com/linxiangpeng/p/6403991.html 在此表达对原创作者的感激之情,多谢星过无痕的分享!谢谢! Python学习 ...
- python基础语法(一)
Python的特点 1. 简单 Python是一种代表简单思想的语言. 2. 易学 Python有极其简单的语法. 3. 免费.开源 Python是FLOSS(自由/开放源码软件)之一. 4. 高层语 ...
- Python基础语法(转)
作者:Peter 出处:http://www.cnblogs.com/Peter-Zhang/ Python 基础语法(一) Python的特点 1. 简单 Python是一种代表简单思想的语言. ...
- Python 基础语法_Python脚本文件结构
目录 目录 前言 软件环境 Python Script文件结构 导入模块的流程 Python的包package 最后 前言 Python基础语法这一章,主要记录了Python的文件结构.逻辑运算符.算 ...
- (转)Python成长之路【第九篇】:Python基础之面向对象
一.三大编程范式 正本清源一:有人说,函数式编程就是用函数编程-->错误1 编程范式即编程的方法论,标识一种编程风格 大家学习了基本的Python语法后,大家就可以写Python代码了,然后每个 ...
- python基础全部知识点整理,超级全(20万字+)
目录 Python编程语言简介 https://www.cnblogs.com/hany-postq473111315/p/12256134.html Python环境搭建及中文编码 https:// ...
- python之最强王者(2)——python基础语法
背景介绍:由于本人一直做java开发,也是从txt开始写hello,world,使用javac命令编译,一直到使用myeclipse,其中的道理和辛酸都懂(请容许我擦干眼角的泪水),所以对于pytho ...
随机推荐
- RDSS和RNSS
RNSS英文全称Radio Navigation Satellite System,由用户接收卫星无线电导航信号,是一种卫星无线电导航业务,自主完成至少到4颗卫星的距离测量,进行用户位置,速度及航行参 ...
- 【腾讯海纳】系统未发布时如何获取获取property_id在本地进行测试?
有现成https协议域名使用者,可忽略此文. 直接先上图,明白的人看一眼图片就知道怎么拿了,如下所示: 解释说明: 在完成添加套件,以及测试应用的前提下,按如下操作流程: 1.访问路径:登录“海纳开发 ...
- SpringBoot报错:Failed to load ApplicationContext(javax.websocket.server.ServerContainer not available)
引起条件: WebSocket+单元测试,单元测试报错! 解决方法: SpringBootTest增加webEnvironment参数. https://docs.spring.io/spring-b ...
- linux mailbox模型
一.device tree中的写法 二. mailbox框架 (driver/mailbox/mailbox.c) struct mbox_controller { struct device *de ...
- laravel之知识点
- springmvc的日期类型转换
springmvc的日期类型转换 # spring mvc绑定参数之类型转换有三种方式: ## 1.实体类中加日期格式化注解 @DateTimeFormat(pattern="yyyy- ...
- LeetCode编程训练 - 拓扑排序(Topological Sort)
拓扑排序基础 拓扑排序用于解决有向无环图(DAG,Directed Acyclic Graph)按依赖关系排线性序列问题,直白地说解决这样的问题:有一组数据,其中一些数据依赖其他,问能否按依赖关系排序 ...
- 【面试篇】资深招聘HR有哪些面试技巧?
15年资深招聘HR总结的面试技巧 1.做一下自我介绍 了解应聘者的基本信息和工作经历 2.以往工作中您的职责是什么? 了解应聘者的相关工作经验和其系统性全面性 3.请讲一下您以往的工作经历. ...
- 【从零开始搭建自己的.NET Core Api框架】(三)集成轻量级ORM——SqlSugar:3.1 搭建环境
系列目录 一. 创建项目并集成swagger 1.1 创建 1.2 完善 二. 搭建项目整体架构 三. 集成轻量级ORM框架——SqlSugar 3.1 搭建环境 3.2 实战篇:利用SqlSuga ...
- [Swift]LeetCode173. 二叉搜索树迭代器 | Binary Search Tree Iterator
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...