从左往右,只有当第一个操作数的值无法确定逻辑运算的结果时,才对第二个操作数进行求值。
continue break pass
(for语句同样可使用)
for letter in 'Python':if letter == 'h':passprint '这是 pass 块'print '当前字母 :', letter
结果:
当前字母 : P
当前字母 : y
当前字母 : t
这是 pass 块
当前字母 : h
当前字母 : o
当前字母 : n
while…else…
count = 0
while count < 5:print count, " is less than 5"count = count + 1
else:print count, " is not less than 5"
结果:
0 is less than 5
1 is less than 5
2 is less than 5
3 is less than 5
4 is less than 5
5 is not less than 5
代码:
for letter in "python":print(letter)
结果:
p
y
t
h
o
n
代码:
fruits = ['apple', 'banana', 'watermelon', 'peer', 'grapes']
for fruit in fruits:print(fruit)
或使用序列索引:
for index in range(len(fruits)):print(fruits[index])
结果:
apple
banana
watermelon
peer
grapes
for…else…
for num in range(10,20):for i in range(2,num):if num%i == 0:print('%d = %d * %d' % (num, i, num/i))breakelse: print ('%d is a special number!' % (num))
结果:
10 = 2 * 5
11 is a special number!
12 = 2 * 6
13 is a special number!
14 = 2 * 7
15 = 3 * 5
16 = 2 * 8
17 is a special number!
18 = 2 * 9
19 is a special number!
打印九九乘法表:
for num in range(1,10):for i in range(1,num+1):print('%d * %d = %d' % (i, num, i*num), end = ' ')print(' ')
本文发布于:2024-01-31 10:27:25,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170666804527858.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |