第四章、 控制流
控制语句后面要加冒号:
1)    if语句

if guess == number:
    print 'Congratulations, you guessed it.' # New block starts here
elif guess < number:
    print 'No, it is a little higher than that' # Another block
else:
print 'No, it is a little lower than that' 
if not False and True: #组合条件
    print "OK"

注:Python暂时没有switch语句

2)    while语句
注:while语句有一个可选的else从句

while running:
    guess = int(raw_input('Enter an integer : '))
    if guess == number:
        print 'Congratulations, you guessed it.'
        running = False # this causes the while loop to stop
    elif guess < number:
        print 'No, it is a little higher than that'
    else:
        print 'No, it is a little lower than that'
else:
print 'The while loop is over.' 

3)    range语句

print range(10) #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print range(5,10) #[5, 6, 7, 8, 9]
print range(1,10,3) #[1, 4, 7]
print range(-10, -100, -30) #[-10, -40, -70]

用法参考help(range)

4)    for循环

a = ['apple', 'banana', 'carrot']
for i in range(len(a)): #range()和len()一起用于字符串索引
print a[i]
#apple
#banana
#carrot

带逗号的print语句输出的元素之间会自动添加空格

for i in range(len(a)):
print a[i],  #带,的print语句
# apple banana carrot

C/C++中的for (int i = 0; i < 5; i++),等价于Python:for i in range(0,5)。

5)    break语句

while True:
    s = raw_input('Enter something : ')
    if s == 'quit':
        break
    print 'Length of the string is', len(s)
print 'Done' 

6)    continue语句

while True:
    s = raw_input('Enter something : ')
    if s == 'quit':
        break
    if len(s) < 3:
        continue
print 'Input is of sufficient length' 

7)    条件表达式

x, y = 3, 4
small = x if x < y else y
print small #3 

python 教程 第四章、 控制流的更多相关文章

  1. 2018-06-20 中文代码示例视频演示Python入门教程第四章 控制流

    知乎原链 续前作: 中文代码示例视频演示Python入门教程第三章 简介Python 对应在线文档: 4. More Control Flow Tools 录制中出了不少岔子. 另外, 输入法确实是一 ...

  2. [Learn Android Studio 汉化教程]第四章 : Refactoring Code

    [Learn Android Studio 汉化教程]第四章 : Refactoring Code 第四章 Refactoring Code    重构代码 在Android Studio中开发,解决 ...

  3. [Python笔记][第四章Python正则表达式]

    2016/1/28学习内容 第四章 Python字符串与正则表达式之正则表达式 正则表达式是字符串处理的有力工具和技术,正则表达式使用预定义的特定模式去匹配一类具有共同特征的字符串,主要用于字符串处理 ...

  4. Cobalt Strike系列教程第四章:文件/进程管理与键盘记录

    Cobalt Strike系列教程分享如约而至,新关注的小伙伴可以先回顾一下前面的内容: Cobalt Strike系列教程第一章:简介与安装 Cobalt Strike系列教程第二章:Beacon详 ...

  5. [ABP教程]第四章 集成测试

    Web应用程序开发教程 - 第三章: 集成测试 //[doc-params] { "UI": ["MVC","NG"], "DB& ...

  6. Flask 教程 第四章:数据库

    本文翻译自 The Flask Mega-Tutorial Part IV: Database 在Flask Mega-Tutorial系列的第四部分,我将告诉你如何使用数据库. 本章的主题是重中之重 ...

  7. 进击的Python【第四章】:Python的高级应用(一)

    Python的高级应用(一) 本章内容: 内置函数 生成器 迭代器 装饰器 JSON和PICKLE的简单用法 软件目录结构规范 一.内置函数 1.数学运算类 abs(x) 求绝对值1.参数可以是整型, ...

  8. Python【第四章】:socket

    ocket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字"向网络发出请求或者应答网络请求. socket起源于Uni ...

  9. 脚本语言丨Batch入门教程第四章:调用与传参

    今天是Batch入门教程的最后一章内容:调用与传参.相信通过前面的学习,大家已经掌握了Windows Batch有关的基础知识和编程方法,以及利用Windows Batch建立初级的编程思维方式.今后 ...

随机推荐

  1. php实现矩形覆盖

    php实现矩形覆盖 一.总结 很简单的斐波那契数列 二.php实现矩形覆盖 题目描述: 我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形.请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总 ...

  2. python 升级pip

    废话少说,直接上图,希望谅解我的懒惰!:)

  3. java痛苦学习之路[十二]JSON+ajax+Servlet JSON数据转换和传递

    1.首先client须要引入 jquery-1.11.1.js 2.其次javawebproject里面须要引入jar包  [commons-beanutils-1.8.0.jar.commons-c ...

  4. Bash玩转脚本1之自己的脚本安装程序

    Bash之打造自己的脚本安装器 前言 还是理所当然的前言,我一直想找一套管理脚本的"框架",能让自己杂乱的脚本有点规整.无奈眼界尚浅,未能找到. 因此萌生自己写一点优化脚本的工具来 ...

  5. NYOJ 364 田忌赛马

    田忌赛马 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描写叙述 Here is a famous story in Chinese history. "That ...

  6. Qt元类型(MetaType)注册门(使用qRegisterMetaType注册错误等级)

    昨天调试项目时,突然发现如下消息: QObject::connect: Cannot queue arguments of type 'ERROR_LEVEL' (Make sure 'ERROR_L ...

  7. JAVA SkipList 跳表 的原理和使用例子

    跳跃表是一种随机化数据结构,基于并联的链表,其效率可比拟于二叉查找树(对于大多数操作需要O(log n)平均时间),并且对并发算法友好. 关于跳跃表的具体介绍可以参考MIT的公开课:跳跃表 跳跃表的应 ...

  8. Java基本数据类型的取值范围

    版权声明:本文为博主原创文章,未经博主允许不得转载. 先看一段代码public class Hello{    public static void main(String[] args){      ...

  9. Dynamips GNS3

    https://baike.baidu.com/item/dynamips Dynamips的原始名称为Cisco 7200 Simulator,源于Christophe Fillot在2005年8月 ...

  10. Android SqlDelight具体解释和Demo样例

    一.简单介绍 SQLDelight 和 SqlBrite 是 Square 公司推出的一个 Android 平台数据库解决方式. 在了解这个两个东西前,必须先得有Andorid的Sqlite的知识(S ...