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 ...
随机推荐
- zookeeper使用和原理探究
转:http://www.blogjava.net/BucketLi/archive/2010/12/21/341268.html zookeeper介绍 zookeeper是一个为分布式应用提供一致 ...
- 用kattle将数据从SQLserver中导入到vertica中
今天简单的学习了一下ETL工具kattle了,只是简单的上手,不过这也已经够我去做POC了. 首先大体介绍一下kattle,Kettle是一款国外开源的ETL工具,纯java编写,可以在Window. ...
- 818C.soft thief
Yet another round on DecoForces is coming! Grandpa Maks wanted to participate in it but someone has ...
- [Educational Round 59][Codeforces 1107G. Vasya and Maximum Profit]
咸鱼了好久...出来冒个泡_(:з」∠)_ 题目连接:1107G - Vasya and Maximum Profit 题目大意:给出\(n,a\)以及长度为\(n\)的数组\(c_i\)和长度为\( ...
- java 的基本数据类型及转换
数据类型精度: byte 8 位short 16 位int 32 位long 64 位float 32 位double 64 位char 16 位 boolean 占几位要看 jvm 的具体实现, 虽 ...
- 【转载】C++ ,C#数据类型对照
C++ C#=====================================WORD ushortDWORD uintUCH ...
- [Swift]LeetCode712. 两个字符串的最小ASCII删除和 | Minimum ASCII Delete Sum for Two Strings
Given two strings s1, s2, find the lowest ASCII sum of deleted characters to make two strings equal. ...
- PHP算法之冒泡排序
//冒泡排序 //①思路,先比较出第一次,找一个最大的值,排到最后; //②重复count遍之后,就能得到排序; //③优化,每一次循环之后不需要再次全部重复; $array = [11,5,4,58 ...
- Java Jvm运行机制原理
一:简介 在学习Java虚拟机之前,也就是Jvm之前,我想大家能够带着问题去学习,这样的话,大家学习起来也会比较有所获! 1.Java虚拟机(Jvm)是什么? 2.Java虚拟机是用来干什么的? 3. ...
- Hive篇---Hive使用优化
一.前述 本节主要描述Hive的优化使用,Hive的优化着重强调一个 把Hive SQL 当做Mapreduce程序去优化 二.主要优化点 1.Hive运行方式:本地模式集群模式 本地模式开启本地模式 ...