>>> while True:
... stuff = input("String to capitalize [type q to quit]:")
... if stuff == 'q':
... break
... print(stuff.capitalize())
...
String to capitalize [type q to quit]:test
Test
String to capitalize [type q to quit]:darren chen
Darren chen
String to capitalize [type q to quit]:q
로그인 후 복사
使用continue调到循环开始
while True:
value = input('Integer ,please [q to quit]:')
if value == 'q':
break
number = int(value)
if number % 2 == 0:
continue
print(number,'squared is',number*number)
Integer ,please [q to quit]:>? 1
1 squared is 1
Integer ,please [q to quit]:>? 2
Integer ,please [q to quit]:>? 3
3 squared is 9
Integer ,please [q to quit]:>? 5
5 squared is 25
Integer ,please [q to quit]:>? 6
Integer ,please [q to quit]:>? q
로그인 후 복사
循环外使用else:
当while循环正常结束(没有使用break跳出),程序将进入到可选的else段 。
numbers = [1,3,5]
position = 0
while position < len(numbers):
number = numbers[position]
if number % 2 == 0:
print('Found even number',number)
break
position += 1
else:
print('No even number found')
...No even number found
word = 'Darren Chen'
for i in word:
print(i)
D
a
r
r
e
n
C
h
e
n
로그인 후 복사
对一个字典(或字典的key()函数)迭代将返回字典中的键
home = {"man":'chenda','woman':'lvpeipei'}
for i in home:
print(i)
man
woman
로그인 후 복사
想对值迭代,可以使用字典的values()
>>> for value in accusation. values():
... print( value)
...
ballroom
lead pipe
로그인 후 복사
同while一样,可以使用break跳出循环,使用continue调到循环开始。
循环外使用else:
>>> cheeses = []
>>> for cheese in cheeses:
... print(' This shop has some lovely', cheese)
... break
... else: # 没有 break 表示 没有 找到 奶酪 .
.. print(' This is not much of a cheese shop, is it?')
...
This is not much of a cheese shop, is it?
>>> for x in range( 0, 3):
... print( x)
...
0
1
2
>>> list( range( 0, 11, 2) )
[0, 2, 4, 6, 8, 10]
로그인 후 복사
六、推导式
推导式是从一个或者多个迭代器快速简介地创建数据结构的一种方法。
列表推导式
>>> number_ list = list( range( 1, 6))
>>> number_ list
[1, 2, 3, 4, 5]
>>> number_ list = [number for number in range( 1, 6)]
>>> number_ list
[1, 2, 3, 4, 5]
>>> number_ list = [number- 1 for number in range( 1, 6)]
>>> number_ list
[0, 1, 2, 3, 4]
>>> a_ list = [number for number in range( 1, 6) if number % 2 == 1]
>>> a_ list
[1,3,5]
#嵌套循环
>>> rows = range( 1, 4)
>>> cols = range( 1, 3)
>>> cells = [(row, col) for row in rows for col in cols]
>>> for cell in cells:
... print( cell)
...
(1, 1)
(1, 2)
(2, 1)
(2, 2)
(3, 1)
(3, 2)
로그인 후 복사
字典推导式
{ key_ expression : value_ expression for expression in iterable }
>>> word = 'letters'
>>> letter_ counts = {letter: word. count( letter) for letter in set( word)}
>>> letter_ counts
{'t': 2, 'l': 1, 'e': 2, 'r': 1, 's': 1}
로그인 후 복사
集合推导式
>>> a_ set = {number for number in range( 1, 6) if number % 3 == 1}
>>> a_ set
{1, 4}
로그인 후 복사
生成器推导式——元组是没有推导式的,其实,圆括号之间的是生成器推导式,它返回的是一个生成器对象。
>>> number_ thing = (number for number in range( 1, 6))
>>> type( number_ thing)
< class 'generotor'>
#可以直接对生成器对象进行迭代
>>> for number in number_ thing:
... print( number)
...
1
2
3
4
5
로그인 후 복사
#通过对一个生成器的推导式调用list()函数,使它类似于列表推导式
>>> number_ list = list( number_ thing)
>>> number_ list
[1, 2, 3, 4, 5]
一个生成器只能运行一