流程控制if

if 语句

if expression:

statement(s)

else

else语句:

if 语句,else语句

if expression:

statement(s)

else:

statement(s)

elif语句:

if expression1:

statement1(s)

elif expression2(s):

statements2(s)

else:

statement2(s)

注:Python使用缩进作为其语法分组的方法,建议使用4个空格

逻辑值(bool)包含了两个值:

True:表示非空的量(比如:string,tuple,list,set,dictonary)所有非零数。

false:表示0,None,空的量等,包括空的元组、空的列表、空的字典。

[root@localhost ~]# vim 1.py
#!/usr/bin/python
score = int(raw_input("Please input you score"))
if  score >= 90:print 'A'print 'very good'
elif score >= 80:print 'B'print 'good'
elif score >= 70:print 'C'print 'pass'
else : print 'D fail'
print 'End'
[root@localhost ~]# python 1.py
Please input you score90
A
very good
End
[root@localhost ~]# python 1.py
Please input you score80
B
good
End
[root@localhost ~]# python 1.py
Please input you score70
C
pass
End
[root@localhost ~]# python 1.py
Please input you score10
D fail
End
[root@localhost ~]# vim 2.py
#!/usr/bin/python
yn = raw_input("Please input [Yes/No]")
yn = yn.lower()
if yn == 'y' or yn == 'yes':print "program is running"
elif yn == 'n' or yn == 'no':print "program is exit"
else:print "please input [Yes/No]"[root@localhost ~]# python 2.py
Please input [Yes/No]yes
program is running
[root@localhost ~]# python 2.py
Please input [Yes/No]no
program is exit

流程控制for循环

for循环:

在序列里面,使用for循环遍历

语法:

for interating_var in sequence:

statement(s)

#对序列做一个遍历
[root@localhost ~]# vim 3.py
#!/usr/bin/python
for i in range(1,11):print (i)
[root@localhost ~]# python 3.py
1
2
3
4
5
6
7
8
9
10#打印range(1,11),也就是打印1-10
[root@localhost ~]# vim 3.py
#!/usr/bin/python
print [i for i in range(1,11)]
[root@localhost ~]# python 3.py
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]#打印range(1,11)*2,也就是打印(1-10)*2
[root@localhost ~]# vim 3.py
#!/usr/bin/python
print [i*2 for i in range(1,11)]
[root@localhost ~]# python 3.py
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]#判断是不是偶数,是的话放在列表中,并打印出来
[root@localhost ~]# vim 3.py
#!/usr/bin/python
print [i for i in range(1,11) if i%2 == 0 ]
[root@localhost ~]# python 3.py
[2, 4, 6, 8, 10]#打印range(1,11)的三次方,也就是打印(1-10)的三次方
[root@localhost ~]# vim 3.py
#!/usr/bin/python
print [i**3 for i in range(1,11)]
[root@localhost ~]# python 3.py
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]#列表重写,打印range(1,11)的三次方,也就是打印(1-10)的三次方
[root@localhost ~]# vim 3.py
#!/usr/bin/python
for j in [i**3 for i in range(1,11)]:print j,
[root@localhost ~]# python 3.py
1 8 27 64 125 216 343 512 729 1000#列表重写,只是奇数的range(1,11)的三次方,打印1,3,5,7,9的三次方
[root@localhost ~]# vim 3.py
#!/usr/bin/pytho
for j in [i**3 for i in range(1,11) if i % 2 !=0 ]:print j,
[root@localhost ~]# python 3.py
1 27 125 343 729xrange object对象,不占用内存,遍历才能取到值

#用shell实现乘法口诀,", print"  ,表示不换行,print表示换行

[root@localhost ~]# vim 6.py
#!/usr/bin/python
for i in xrange(1,10):for j in xrange(1,i+1):print "%s * %s = %s" %(j, i, j*i),print
[root@localhost ~]# python 6.py
1 * 1 = 1
1 * 2 = 2 2 * 2 = 4
1 * 3 = 3 2 * 3 = 6 3 * 3 = 9
1 * 4 = 4 2 * 4 = 8 3 * 4 = 12 4 * 4 = 16
1 * 5 = 5 2 * 5 = 10 3 * 5 = 15 4 * 5 = 20 5 * 5 = 25
1 * 6 = 6 2 * 6 = 12 3 * 6 = 18 4 * 6 = 24 5 * 6 = 30 6 * 6 = 36
1 * 7 = 7 2 * 7 = 14 3 * 7 = 21 4 * 7 = 28 5 * 7 = 35 6 * 7 = 42 7 * 7 = 49
1 * 8 = 8 2 * 8 = 16 3 * 8 = 24 4 * 8 = 32 5 * 8 = 40 6 * 8 = 48 7 * 8 = 56 8 * 8 = 64
1 * 9 = 9 2 * 9 = 18 3 * 9 = 27 4 * 9 = 36 5 * 9 = 45 6 * 9 = 54 7 * 9 = 63 8 * 9 = 72 9 * 9 = 81

for循环

for

else

for循环如果正常结束,才会执行else语句,

break退出整个循环,break异常退出不执行else内容,

continue退出当前的循环,不执行循环语句内容

pass是占位

import sys    sys.exit()退出整个脚本

import time   [root@localhost ~]# vim 7.py

range(10)直接分配,占用内存资源

b=xrange(10)产生对象,只有对对象做遍历,才能产生值,才会占用内存资源

#!/usr/bin/python
for i in xrange(10):print i
else:print 'main end
[root@localhost ~]# python 7.py
0
1
2
3
4
5
6
7
8
9
main end
#ctrl+C或者break异常退出不执行else内容
[root@localhost ~]# vim 7.py
#!/usr/bin/python
import time
for i in xrange(10):print itime.sleep (1)if i==5:break
else:print 'main end
[root@localhost ~]# python 7.py
0
1
2
3
4
5

#continue跳出本次i=3的循环,继续i=4的循环

[root@localhost ~]# vim 7.py
#!/usr/bin/python
import time
for i in xrange(10):if i==3:continueelif i==5:breakelif i==6:pass            print i
else:print 'main end'
[root@localhost ~]# python 7.py
0
1
2
4[root@133 filevalue]# vim for_else.py #!/usr/bin/pythonimport time
for i in xrange(10):if i == 3:  #i=3的时候跳出本次循环,继续下一次循环,执行i=4continueelif i == 5:break    #i=5的时候break直接跳出循环elif i == 6:passtime.sleep(1)print i
else:print "main end"[root@133 filevalue]# python for_else.py
0
1
2
4

现在让我们用python写一个练习题:

系统随机生成一个20以内的随机整数,

玩家有6次机会进行猜猜看,每次猜测都有反馈(猜大了,猜小了,猜对了,结束)

6次中,猜对了,玩家赢了

否则,系统赢了

[root@133 filevalue]# vim random20.py
#!/usr/bin/python
import random
import sysprint 'The random have been created'
random_num = random.randint(1,20)for i in xrange(1,7) :enter_num = int(raw_input('Please input guess num'))if random_num < enter_num:print 'biger'elif random_num > enter_num:print 'lower'elif random_num == enter_num:print 'you are win'sys.exit()
print 'game over'
[root@133 filevalue]# python random20.py
The random have been created
Please input guess num10
lower
Please input guess num15
lower
Please input guess num18
you are win

while与for相比

for循环用在有次数的循环上

while循环用在有条件的控制上


while循环

while循环,直到表达式为假,才退出while循环,表达式是一个逻辑表达式,必须返回一个True或者False,注意是大写,不是小写

语法:

while expression:

statement(s)

[root@localhost ~]# vim 11.py
#!/usr/bin/python
n=0
while True:if n==10:breakprint n,'hello'n+=1
[root@localhost ~]# python 11.py
0 hello
1 hello
2 hello
3 hello
4 hello
5 hello
6 hello
7 hello
8 hello
9 hell

while循环可以使用break退出,例子是用户输入=q的时候,break退出

[root@localhost ~]# vim 11.py
#!/usr/bin/python
while True:print 'hello'input = raw_input("please input somethong,q for quit:")if input=="q":break
[root@localhost ~]# python 11.py
hello
please input somethong,q for quit:a
hello
please input somethong,q for quit:b
hello
please input somethong,q for quit:q[root@localhost ~]# vim 11.py
#!/usr/bin/python
x=''
while x!='q': #输入值不为q,x!='q'为真True,输出hello,输入为q,x!='q'为假False,执行else,输出hello worldprint 'hello'x = raw_input("please input something,q for quit:")if not x: #当输入为空时,not x为true,if true break,直接跳出break
else:print "hello world"
[root@localhost ~]# python 11.py
hello
please input somethong,q for quit:a
hello
please input somethong,q for quit:q
hello world
[root@localhost ~]# python 11.py
hello
please input somethong,q for quit:
#用continue,条件满足,不执行continue的循环内容,重新开始循环
[root@localhost ~]# vim 11.py
#!/usr/bin/python
x=''
while x!='q':print 'hello'x = raw_input("please input somethong,q for quit:")if not x:breakif x=='quit':continueprint 'continue'
else:print "hello world"
[root@localhost ~]# python 11.py
hello
please input somethong,q for quit:a
continue
hello
please input somethong,q for quit:quit
hello
please input somethong,q for quit:q
continue
hello world #x=q,所以不执行while循环,而是执行esle: 输出hello world

转载于:https://blog.51cto.com/daixuan/1773603

python学习笔记3—流程控制if、for、while相关推荐

  1. Python学习笔记3 流程控制、迭代器、生成器

    第3章 流程控制.迭代器.生成器 3.1 选择语句 1.语法:(1)if -else (2)if-elif-else 2.注意:(1)每个条件后面要使用冒号:(2)使用缩进划分语句块(3)python ...

  2. Python学习笔记02_流程控制

    Python 文件的创建和执行 创建和打开文件 打开cmd,输入以下命令,运行.py 文件 python xxx.py 条件判断 用代码告诉计算机,什么条件下该做什么.很多编程语言都会使用 if .e ...

  3. 狂神说学习笔记 Java流程控制

    目录 Java流程控制 1.用户交互Scanner Scanner对象 next() nextLine(): 2.顺序结构 3.选择结构 4.循环结构 5.Break & Continue 6 ...

  4. linux设置程序循环,linux shell编程学习笔记(7)流程控制之循环结构

    2.1.for循环 1)遍历/列表式循环 --根据变量的不同取值,重复执行命令序列 格式: for  变量名  in 值列表 do 命令序列 done 示例:输出在线的主机IP #!/bin/bash ...

  5. [精易软件开发工程师Leo学习笔记]007流程控制

    如果: 如果:满足条件执行一个分支,不满足则执行另外一个分支 分支线是告诉开发者成立与不成立的执行区域 下面这段代码,如果里面条件成立,所以运行第一条分支,也就是输出成立  判断和如果的区别: 判断规 ...

  6. Python学习笔记:异步IO(1)

    前言 最近在学习深度学习,已经跑出了几个模型,但Pyhton的基础不够扎实,因此,开始补习Python了,大家都推荐廖雪峰的课程,因此,开始了学习,但光学有没有用,还要和大家讨论一下,因此,写下这些帖 ...

  7. Python学习笔记五:控制语句

    Python学习笔记五:控制语句 Pycharm 开发环境的下载安装配置_项目管理 控制语句 Pycharm 开发环境的使用 Pycharm 下载和安装 激活和选择不同UI 风格 创建项目和初始化配置 ...

  8. Python学习笔记(十三):异常处理机制

    Python学习笔记(十三):异常处理机制 关于Python的异常处理机制 Python学习笔记(十三):异常处理机制 一.异常处理机制 常见异常类型 二.异常处理 try...except 异常类的 ...

  9. 【Python学习笔记(一)—— 初识Python】

    Python学习笔记(一) 文章目录 Python学习笔记(一) 前言 一.Python简介 二.初识Python 1.最简单的Python程序 2.数据类型和变量 3.流程控制 4.函数 5.类 6 ...

最新文章

  1. C++——运算符重载operator
  2. linux中的和||(linux中=和==效果是一样的)
  3. 导向滤波python_导向滤波(Guided Filter)简要介绍
  4. 学习笔记:验证对称二叉树
  5. 解决Windows10搜索框空白的问题
  6. raid 物理盘缓存状态_使用MegaCli工具查看Raid磁盘阵列状态
  7. django objects.filter().exists()
  8. socketserver剖析.html
  9. Londiste3 Install
  10. #if defined和#if !defined的含义
  11. 无需第三方,使用Mac预览合并PDF
  12. Mac OS X的入门文档
  13. 大数据之Kafka介绍
  14. 计算机的内存时序参数,装机用户须知:电脑内存时序基础知识
  15. 三剑客python自学笔记--02
  16. MIUI9系统详细刷成开发版启用root权限的教程
  17. 银行笔试计算机基础知识点归纳,银行笔试:六大行笔试考情及重点梳理(内含免费模考)...
  18. 手把手教你处理 JS 逆向之图片伪装
  19. windows批处理学习
  20. 查看计算机远程端口,如何查看服务器远程端口号.doc

热门文章

  1. 北京冬奥一项AI黑科技即将走进大众:实时动捕三维姿态,误差不到5毫米
  2. 李开复张亚勤巅峰对话,还有虚拟人自动驾驶论坛,今年MEET智能未来大会,我蚌埠住了...
  3. 2021未来科学大奖揭晓:SARS病原发现者、上海交大张杰教授等4人获得百万奖金...
  4. 他让张一鸣登门请教,培养出戴文渊李沐陈天奇,创建了传说中的上海交大ACM班...
  5. 「仅凭照片就能判断一个人是否犯罪」?这样的研究能发表,LeCun、MIT谷歌等机构的1700名研究者怒了...
  6. 李开复:AI进入落地期,单凭科学家颠覆行业的机会几乎不存在,这个领域除外...
  7. 谷歌官方TensorFlow开发者认证来了,吴恩达:学我的课,报名费五折
  8. 李开复:为什么我认为“AI+”有四阶段
  9. 推特千赞Demo袭来!简笔画变照片的GauGAN,编故事的GPT-2,浏览器皆可玩
  10. 修改echarts环形图的牵引线及文字位置