python学习9-语句

选择表达式

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循环语句。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值