如何在python中实现循环指定次数?

如题所述

python中实现循环指定次数:

count=0

for item in list:
print item
count +=1    if count % 10 == 0:
print 'did ten'

或:

for count in range(0,len(list)):
print list[count]    if count % 10 == 0:
print 'did ten'

在Python的for循环里,循环遍历可以写成:

for item in list:

   print item

扩展资料:

Python 注意事项:

1、tuple:元组

(1)元组一旦初始化就不可修改。不可修改意味着tuple更安全。如果可能,能用tuple代替list就尽量用tuple。

(2)定义只有一个元素的tuple的正确姿势:t = (1,),括号内添加一个逗号,否则会存在歧义。

2、dict:字典

a.获取value值:dict['key'],若key不存在,编译器就会报错KeyError。避免方法:

一是通过 in 判断 key 值是否在dict中:

'key' in dict # 返回True 或 False。

二是通过 dict 的函数get():

dict.get('key') # 返回 value 值 或 None。

温馨提示:答案为网友推荐,仅供参考
相似回答