python——语句

条件语句

if condition:
    #code list
[
elif condition_1:
    #code list
]
...
[
elif condition_n:
    #code list
]
[
else:
    #code list
]
注1:[]表示optional
注2:elif不能拆分为else if
注3:不支持switch
score = 90

if score >= 90:
    print 'excellent'

score = 80

if score >= 60:
    print 'pass'
else:
    print 'fail'

score = 70

if score >= 90:
    print 'A'
elif score >= 80:
    print 'B'
elif score >= 70:
    print 'C'
elif score >= 60:
    print 'D'
else:
    print 'E'

循环语句

for...in

sum = 0

for i in range(11):
    sum = sum + i

print sum

while

i = 0
sum = 0

while i < 11:
    sum = sum + i
    i = i + 1

print sum
注:不支持do while

空语句

pass

条件语句

salary = 15000
if salary >= 30000:
    print 'very high income'
elif salary >= 20000:
    print 'high income'
elif salary >= 10000:
    print 'middle income'
else:
    print 'low income'
注意:
  • elif意义是else if,但不能写成else if
  • elif分支optional
  • else分支optional
def judgeCondition(x):
    if x:
        print True
    else:
        print False	

x = 0
judgeCondition(x)
x = 1
judgeCondition(x)
x = ''
judgeCondition(x)
x = "abc"
judgeCondition(x)
x = ()
judgeCondition(x)
x = (1, 2)
judgeCondition(x)
x = None
judgeCondition(x)
output:
False
True
False
True
False
True
False
单独变量作为判断条件时,有以下情况:
  • 整型&浮点型:0为False,非0为True
  • 字符串:空串为False,非空串为True
  • list&tuple&dict&set:空为False,非空为True

循环语句

for...in

namelist = ['tom', 'jack', 'martin']
for name in namelist:
    print name

while

namelist = ['tom', 'jack', 'martin']
index = 0
while index < len(namelist):
    print namelist[index]
    index = index + 1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值