python循环遍历-for循环

python循环遍历

for循环
append() 方法向列表的尾部添加一个新的元素。
1)循环字符串

str_1='abcde你好呀!'
result=[]
for i in str_1:
    result.append(i)
print(result)

python循环遍历-for循环_第1张图片
2)循环列表

list_1=['a','b','c','你','好','呀','!']
result=[]
for i in list_1:
    result.append(i)
print(result)

python循环遍历-for循环_第2张图片
3)循环range

range_1=range(10)
result=[]
for i in range_1:
    result.append(i)
print(result)

python循环遍历-for循环_第3张图片
4)循环字典

dict_1={'a':1,'b':2,'c':'好'}
result=[]
for i in dict_1:
    result.append(i)
print(result)

python循环遍历-for循环_第4张图片
5)循环遍历文件

for content in open ('test.txt',encoding='utf-8'):
    print(content)

python循环遍历-for循环_第5张图片
6)循环1到5相乘

sum = 1
for i in list(range(1,6)):
    sum *= i
print('1*2*3*4*5=',sum)

python循环遍历-for循环_第6张图片
7)使用len()和range()函数的for循环

name=['li','w','chen']
for i in range(len(name)) :
    print(name[i])
print(len(name))

python循环遍历-for循环_第7张图片

你可能感兴趣的:(python)