Basic Python loop statements

    • 1 a for loop
      • (1) Loop through the list
      • (2) Loop through dict
      • (3) Example: Calculate the sum of arithmetic sequences
    • 2 the while loop
      • (1) Numerical cycle
      • (2) Example: Calculate the sum of arithmetic sequences
    • 3 Loop nesting
      • Example: Simulate the web site login authentication process

Loops can be used to execute a statement repeatedly until a condition is met or to iterate over all elements.

1 a for loop

The for loop iterates through elements of the collection data types list, tuple, dict, and set.

(1) Loop through the list

city_list = ['guangzhou'.'shenzhen'.'dongguan'.'the foshan']

for city in city_list:
    print("Current city: {0}".format(city))
Copy the code
City: Guangzhou City: Shenzhen City: Dongguan City: Foshan CityCopy the code

(2) Loop through dict

city_dict = {'A':'guangzhou'.'B':'shenzhen'.'S':'dongguan'.'E':'the foshan'}
for code in city_dict.keys():
    city = city_dict[code]
    print(The license plate number of {0} is yue {1}..format(city,code))
Copy the code
Guangzhou's license plate code is yue A, Shenzhen's license plate code is Yue B, Dongguan's license plate code is Yue S, Foshan's license plate code is Yue ECopy the code

(3) Example: Calculate the sum of arithmetic sequences

Using the for loop, calculate the sum of the numbers 1-20

sum = 0
for i in range(1.21) :# range(1,21)
    sum += i
print('Sum of values %d'%sum)
Copy the code
The sum of the values is 210Copy the code

2 the while loop

The loop continues as long as the condition is satisfied, and exits when the condition is not.

(1) Numerical cycle

n = 0
while(n < 5):
    n+=1
    print("Current value {0}".format(n))
Copy the code
Current value 1 Current value 2 Current value 3 Current value 4 Current value 5Copy the code

(2) Example: Calculate the sum of arithmetic sequences

Using the for loop, calculate the sum of the numbers 1-20

sum = 0
n = 0
while(n < 20):
    n += 1
    sum += n
print('Sum of values %d'%sum) 
Copy the code
The sum of the values is 210Copy the code

3 Loop nesting

To embed a loop inside the body of a loop, you can embed a for loop inside a while loop, or a while loop inside a for loop.

Example: Simulate the web site login authentication process

n = 5
pwd = "123789"
while (n > 0):
    in_str = input("Please enter your password:")
    n -= 1
    if len(in_str) < 6:
        print("Output password less than 6 characters, remaining chance {0} times, please re-enter!".format(n))
    if in_str == pwd:
        print("Login successful!")
        break
    else:
        print("Output password error, remaining chance {0} times, please input again!".format(n))

if n == 0:
    print("Login failed, please try again later!") 
Copy the code
Please input password: 123 If the output password is less than 6 characters, there are 4 remaining chances. Please input again! The output password is incorrect, the remaining 4 times, please re-enter! Please input password: 123567 output password error, the remaining opportunity 3 times, please re-enter! Please enter the password: 123789 login success!Copy the code

Original link: www.sdk.cn/details/EmG…

SDK is a neutral communities, there are a variety of front knowledge, has the rich API, developers have a love of learning artificial intelligence, have humor developers take you learn python, there will be the next hot HongMeng, when various elements together, let us together to build professional, fun and imaginative valuable developer community, Help developers realize their self-worth!