Python_list(三)列表for循环

/ Python / 没有评论 / 392浏览

for循环遍历整个列表

[root@pa1 lijinghua]#cat for.py 
#!/usr/bin/python
# -*- coding: utf-8 -*-
cars = ['bigben','audi','bmw']
for car in cars:
	print(car)
[root@pa1 lijinghua]#python for.py
bigben
audi
bmw

for循环中执行更多

[root@pa1 lijinghua]#python for_2.py 
Audi,that was a good car.
Bmw,that was a good car.
Bigben,that was a good car.
I think too.Bigben
[root@pa1 lijinghua]#cat for_2.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
cars = ['audi','bmw','bigben']
for car in cars:
	print (car.title()+",that was a good car.")
print ( "I think too."+ car.title() )

要点

在for循环后面,没有缩进的代码都只执行一次,而不会重复执行