---- Python learning process for control of the loop

1. What is the for loop

    循环就是重复做某件事,for循环是python提供第二种循环机制

2, why have a for loop

    理论上for循环能做的事情,while循环都可以做
    之所以要有for循环,是因为for循环在循环取值(遍历取值)比while循环更简洁

3, how to use a for loop

语法:
for 变量名 in 可迭代对象:# 可迭代对象可以是:列表、字典、字符串、元组、集合
    代码1
    代码2
    代码3
    ...

A: for the basic use of the cycle value

Case 1: List cycle value

Simple version

l = ['alex_dsb', 'lxx_dsb', 'egon_nb']
for x in l:  # x='lxx_dsb'
    print(x)

Complicated version:

l = ['alex_dsb', 'lxx_dsb', 'egon_nb']
i=0
while i < 3:
    print(l[i])
    i+=1

Case 2: Dictionaries cycle value

Simple version

dic={'k1':111,'k2':2222,'k3':333}
for k in dic:
    print(k,dic[k])

Complex version: while loop can traverse the dictionary, too much trouble

Case 3: A string value cycle

Simple version

msg="you can you up,no can no bb"
for x in msg:
    print(x)

Complex version: while loop can traverse the dictionary, too much trouble

II: summary for loop and while loop similarities and differences

1, in common: both loop, a for loop can do something, the while loop 2 may be dry, except: while circulating loop conditions referred to, when the number of cycles depending on the condition becomes false for loop termed is the number of "cycle value", contains the number of cycles depends on the value in

for x in [1,2,3]:
    print('===>')
    print('8888')

Three: for the control loop cycles: range ()

After the data is placed directly in a type number of control cycles have limitations: when too many cycles, data type contains the value required format accompanied by an increase

for x in 'a c':
    inp_name=input('please input your name:   ')
    inp_pwd=input('please input your password:   ')

Features range

'''
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> 
>>> range(1,9) # 1...8
[1, 2, 3, 4, 5, 6, 7, 8]
>>> 
>>> range(1,9,1) # 1 2 3 4 5 6 7 8 
[1, 2, 3, 4, 5, 6, 7, 8]
>>> range(1,9,2) # 1 3 5 7 
[1, 3, 5, 7]
'''

-----------------------------------------------------------------------------------------
for i in range(30):
    print('===>')


for+break: 同while循环一样
for+else:同while循环一样
username='egon'
password='123'
for i in range(3):
    inp_name = input('请输入您的账号:')
    inp_pwd = input('请输入您的密码:')

    if inp_name == username and inp_pwd == password:
        print('登录成功')
        break
else:
    print('输错账号密码次数过多')
    

Four: range additional knowledge (understanding)

1, for with range, you can parameter according to the index, but the trouble is not recommended

l=['aaa','bbb','ccc'] # len(l)
for i in range(len(l)):
    print(i,l[i])

for x in l:
    print(l)

2, range () obtained in python3 there is a "mother hen will lay eggs."

五:for+continue

for i in range(6):  # 0 1 2 3 4 5
    if i == 4:
        continue
    print(i)

Six: for nested loop: one cycle of the outer loop, the inner loop requires a full cycle is completed

for i in range(3):
    print('外层循环-->', i)
    for j in range(5):
        print('内层-->', j)

Added: termination for loop only break a scheme

print('hello %s' % 'egon')

1, comma use of print

print('hello','world','egon')

2, line breaks

print('hello\n')
print('world')

3, print parameter value for use in end

print('hello\n',end='')
print('word')
print('hello',end='*')
print('world',end='*')

Guess you like

Origin www.cnblogs.com/x945669/p/12458520.html