>>> if 1<‘a‘:
... print "chen"
...
chen
>>> a = "0"
>>> if a:
... print "not null"
...
not null
>>> a = 0
>>> if a:
... print "chen"
... else:
... print "ok!"
...
ok!
#!/usr/bin/python
while 1:
x = int (raw_input("please input :"))
if x >= 90:
print "x >= 90"
elif x >= 80:
print "90 > x >= 80"
elif x >= 60:
print "80 > x >= 60"
else :
print "< 60"
break
>>> True and False
False
>>> 1 and 1 and 0
0
>>> True or 0
True
>>> True or 0 or False
True
>>> True and 0 or False
False
>>> a = (1,3,4,78,12)
>>> for i in a:
... print i
...
1
3
4
78
12
>>> for i in "ads87":
... print i
...
a
d
s
8
7
注意:
i 是起始值,包括在内
j 是终止值,不包括在内
(类似于切片)
>>> for i in range(1,4):
... print i
...
1
2
3
>>> for i in range(1,10,2):
... print i
...
1
3
5
7
9
range 可以产生一个序列:
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> a = {1:11,2:22,3:33,4:44}
>>> for i in a:
... print i
...
1
2
3
4
>>> a.items()
[(1, 11), (2, 22), (3, 33), (4, 44)]
>>> for i in a.items():
... print i
...
(1, 11)
(2, 22)
(3, 33)
(4, 44)
>>> for key,value in a.items():
... print key
... print value
...
1
11
2
22
3
33
4
44
>>> for i in range(10):
... pass
... else:
... print "end~"
...
end~
>>> import time
>>> for i in range(10):
... print i
... time.sleep(2)
... else:
... print "ok"
...
0
1
2
^CTraceback (most recent call last):
File "<stdin>", line 3, in <module>
KeyboardInterrupt
>>> for i in range(10):
... if i > 4:
... break
... else:
... print "end"
...
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/chenguibao/article/details/47976841