您的位置:首页 > 编程语言 > Python开发

python学习9-语句

2016-04-11 20:55 525 查看
选择表达式

1.逻辑表达式

and ,or , no

相当于c++的&&,||,!

即与,或,非

2.语句

if....
elif....
elif....

else...


3.bool类型

只有两个结果:True和False

0,空格,空列表,空字符是False,其他都是True。

4.for循环

values=[1,2,3,4,5,6,7,87]
for i in range(len(values))


5.while循环

whie condition:
block


6.控制循环

current_line=1
earth_line=0
file=open('data.txt','r')
for line in file:
line=line.strip()
if line=='earth':
earth_line=current_line
current_line+=1
print 'earth is at line %d' %earth_line


contnue 和break控制循环

entry_number=1
file=open('data.txt','r')
for line in file:
line=line.strip()
if line.startswith('#'):    #避开注释行产生的错误,也就是注释行的词语不再搜索范围之内
continue       #continue是退出后面的语句,重新进入for语句
if line=='earth'
break              #跳出for循环
entry_number=entry_number+1
print 'earth is the %dth-lightest planet.' %(entry_number)


这里需要注意的是:continue是调到循环体顶部进行循环,而break是跳出了for循环语句。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: