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 ...
随机推荐
- using Sysyem.Net.Http命名空间引用不了的解决方案
1.查看.Net Framework的框架是否是在4.5之上,如果不是要下载4.5之上的目标框架. 2.在引用器里面添加using System.Net.Http命名空间 选择项目列表中的“引用”-- ...
- Eureka-Client(Golang实现)
Eureka-Client Golang实现eureka-client 原理 根据Java版本的源码,可以看出client主要是通过REST请求来与server进行通信. Java版本的核心实现:co ...
- COOKIE和Session的原理及异同
COOKIE和Session的原理及异同 1. cookie的创建和读取 cookie是客户端技术,服务器把每个用户的数据以cookie的形式写给用户各自的浏览器.当用户使用浏览器再去访问服务器中的w ...
- Ajax刷新DIV内容
Ajax刷新DIV内容 实现了网页的异步数据处理,不用刷新整个页面 <标签 onmouseover="method"/ >method:这个参数是处理onmouseov ...
- Linux入门总结——虚拟机安装配置以及vim简单操作
安装配置ubuntu 安装准备 vittualbox-5.2.22版本(win10) ubuntu-12.04 安装VirtualBox 1.双击VirtualBox-5.2.2-119230-Win ...
- leetcode-数组中只出现一次的数字
一.版本1—有序数组中只出现一次的数字 1.题目描述 给定一个只包含整数的有序数组,每个元素都会出现两次,唯有一个数只会出现一次,找出这个数. 示例 1: 输入: [1,1,2,3,3,4,4,8,8 ...
- [Swift]LeetCode101. 对称二叉树 | Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- [Swift]LeetCode377. 组合总和 Ⅳ | Combination Sum IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [Swift]LeetCode881. 救生艇 | Boats to Save People
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat c ...
- [Swift]LeetCode1029. 两地调度 | Two City Scheduling
There are 2N people a company is planning to interview. The cost of flying the i-th person to city A ...