python学习笔记二:流程控制

python学习笔记二:流程控制,第1张

概述一、if else:#!/usr/bin/pythonx = int(raw_input('please input:'))if x >= 90:if x >= 95:print 'a+'else:print 'a'elif x >= 80:if x >= 85:print 'b+'else:print 'b'elif x >= 70:if x >= 75:print 'c+'else:print 'c'else:if x >= 60:print 'd+'else:print 'bad'二、逻辑运算符 and or not#!/usr/bin/pythonx = int(raw_input('please input x:'))y = int(raw_input('please input y:'))if x >= 90 and y >= 90:print 'a'elif x >= 80 or y >= 80:print 'b'elif not x < 60 and (not y < 60):print 'c'else:print 'bad'三、for序列:#!/usr/bin/pythons = "hello python"for x in s:print xfor index in range(len(s)):print s[index]字典:#!/usr/bin/pythondic = {'a':1,'b':2,'c':3}for x in dic:print x,dic[x]for k,v in dic.items():print k,v控制else:for正常执行完会执行else中的内容,否则不会(如下面的代码执行过程按Ctrl+c)#!/usr/bin/pythonimport timefor x in range(10):print xtime.sleep(1)else:print 'end'break:跳出当前这层循环#!/usr/bin/pythonfor x in range(10):print xif x == 6:breakelse:print 'end'这里不会执行else中的内容pass:占位exit:退出#!/usr/bin/pythonfor x in range(10):print xif x == 2:print 'hello',xcontinueif x == 4:passif x == 5:exit()if x == 6:breakprint '*'*10else:print 'end'四、while 当条件失败,正常结束会执行,执行break后else中不执行#!/usr/bin/pythonx = 'hello'while x != "q":print xx = raw_input('please input something,q for quit:')if not x:breakelse:print 'ending'  一、if else:x = int(raw_@R_404_5983@(<span >if x >= 90<span >:
<span >if
x >= 95<span >:
<span >print
<span >'<span >a+<span >'
<span >else<span >:
<span >print <span >'<span >a<span >'
<span >elif x >= 80<span >:
<span >if x >= 85<span >:
<span >print <span >'<span >b+<span >'
<span >else<span >:
<span >print <span >'<span >b<span >'
<span >elif x >= 70<span >:
<span >if x >= 75<span >:
<span >print <span >'<span >c+<span >'
<span >else<span >:
<span >print <span >'<span >c<span >'
<span >else<span >:
<span >if x >= 60<span >:
<span >print <span >'<span >d+<span >'
<span >else<span >:
<span >print <span >'<span >bad<span >'

x = int(raw_@R_404_5983@(= int(raw_@R_404_5983@(<span >if x >= 90 <span >and y >= 90<span >:
<span >print
<span >'
<span >a
<span >'

<span >elif
x >= 80 <span >or
y >= 80<span >:
<span >print <span >'<span >b<span >'
<span >elif <span >not x < 60 <span >and (<span >not y < 60<span >):
<span >print <span >'<span >c<span >'
<span >else<span >:
<span >print <span >'<span >bad<span >'

=

<span >for x <span >in<span > s:
<span >print<span > x

<span >for index <span >in<span > range(len(s)):
<span >print s[index]

字典:

= {:1,:2,:3<span >for x <span >in<span > dic:
<span >print
<span > x,dic[x]

<span >for k,v <span >in<span > dic.items():
<span >print k,v

控制

else:for正常执行完会执行else中的内容,否则不会(如下面的代码执行过程按Ctrl+c)

<span >for x <span >in range(10<span >):
<span >print
<span > x
time.sleep(
1<span >)
<span >else
<span >:
<span >print
<span >'
<span >end
<span >'

break:跳出当前这层循环

x range(10 x == 6

这里不会执行else中的内容

pass:占位

exit:退出

x range(10 x == 2 x == 4 x == 5 x == 6 *10 四、while

 当条件失败,正常结束会执行,执行break后else中不执行

=

<span >while x != <span >"<span >q<span >"<span >:
<span >print<span > x
x = raw@R404_5983@(<span >'<span >please @R_404_5983@ something,q for quit:<span >'<span >)
<span >if <span >not<span > x:
<span >break
<span >else<span >:
<span >print <span >'<span >ending<span >'

总结

以上是内存溢出为你收集整理的python学习笔记二:流程控制全部内容,希望文章能够帮你解决python学习笔记二:流程控制所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1208071.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存