【Python爬虫作业】- 第二周 循环

import json

# 循环 格式化传参
base_url='http://www.python.com/?page={}'
request_url=[]

list_a=range(1,10)
print(list_a)
for loop in list_a:
   request_url.append( base_url.format(loop))

# 字符串分割 列表索引
a='I like python'
b=a.split()
for loop in b:
    print(loop)
for index,item in enumerate(b):
   print(index,' ',item,' ')

# 索引切片
c=b[1]
d=b[2][2:4]
e=b[2]
if e.find(d)>=0:
    print("存在")
else:
    print("不存在")

# 循环判断
if e.startswith(d):
    print('Yes')
else:
    print('No')

for loop in list_a:
    if loop<6:
        print(loop,' < 6')
    elif loop==6:
        print('跳出循环')
        break;

# 字典,json
dict_a={}

for index,value in enumerate(list_a):
    dict_a.update({index:value})

print(dict_a)
for key in dict_a:# 字典类型在for循环中只能得到key值
    print(key,'',dict_a.get(key))

json_a=json.dumps(dict_a)
print(json_a)

# 循环嵌套
list_row=range(1,10)
for i in list_row:
    for j in range(1,i+1):
        if i==j:
            end_symbol=''
        else:
            end_symbol = '  '
        print(j,'*',i,'=',j*i,end=end_symbol)
    print('\r')

运行结果

range(1, 10)
I
like
python
0 I
1 like
2 python
存在
No
1 < 6
2 < 6
3 < 6
4 < 6
5 < 6
跳出循环
{0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 7, 7: 8, 8: 9}
0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
{“0”: 1, “1”: 2, “2”: 3, “3”: 4, “4”: 5, “5”: 6, “6”: 7, “7”: 8, “8”: 9}
1 * 1 = 1
1 * 2 = 2 2 * 2 = 4
1 * 3 = 3 2 * 3 = 6 3 * 3 = 9
1 * 4 = 4 2 * 4 = 8 3 * 4 = 12 4 * 4 = 16
1 * 5 = 5 2 * 5 = 10 3 * 5 = 15 4 * 5 = 20 5 * 5 = 25
1 * 6 = 6 2 * 6 = 12 3 * 6 = 18 4 * 6 = 24 5 * 6 = 30 6 * 6 = 36
1 * 7 = 7 2 * 7 = 14 3 * 7 = 21 4 * 7 = 28 5 * 7 = 35 6 * 7 = 42 7 * 7 = 49
1 * 8 = 8 2 * 8 = 16 3 * 8 = 24 4 * 8 = 32 5 * 8 = 40 6 * 8 = 48 7 * 8 = 56 8 * 8 = 64
1 * 9 = 9 2 * 9 = 18 3 * 9 = 27 4 * 9 = 36 5 * 9 = 45 6 * 9 = 54 7 * 9 = 63 8 * 9 = 72 9 * 9 = 81

    原文作者:丽雁解
    原文地址: https://www.jianshu.com/p/a45537063f0e
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞