python if,for,while

# -*- coding:utf-8 -*-
# 第四章 if for while #布尔逻辑 print True == False
print True and False
print True or False
print not True
print True != False # if elif else
name = 'lee'
if name == 'chen' :
print 'yes, you are chen'
elif name == 'le':
print 'yes, you are god'
elif name == 'shou':
print 'no, get out' # 条件表达式, 类似于?表达式
print 'Yes' if 1 + 1 == 2 else 'No' # for
# range
print help(range)
print type(range(10))
print range(0, 10, 2)
print range(10, 0, -1)
# range(start, stop=None, step=None) step 是每次递增或者递减(-)的step
for i in range(10, 0, -1) :
print i for i in ['','','lala'] :
print i # while
name = 'chen'
while True :
print 'heheda'
if name == 'le' :
break
name = 'le'
continue
print "no continue"

结果:

False
False
True
False
True
Yes
Help on built-in function range in module __builtin__: range(...)
range(stop) -> list of integers
range(start, stop[, step]) -> list of integers Return a list containing an arithmetic progression of integers.
range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.
When step is given, it specifies the increment (or decrement).
For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!
These are exactly the valid indices for a list of 4 elements. None
<type 'list'>
[0, 2, 4, 6, 8]
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
10
9
8
7
6
5
4
3
2
1
12
23
lala
heheda
heheda

for or while else

# while or for else
# 循环正常结束(没有break, return),有else的时候会进入else代码段.
for i in range(10) :
print i
if i == 3 :
break
else :
print "for else"

结果:

0
1
2
3
上一篇:Android编译系统中的Android.bp【转】


下一篇:javascript实现打印功能