Python - while loop
This time, we will introduce the use of while loop statements in Python 3.
In Python 3 programming, the while statement is used to execute the program circularly, that is, under certain conditions, execute a certain program circularly to deal with the same tasks that need to be processed repeatedly.
In Python 3, the while execution statement can be a single statement or statement block. The judgment condition can be any expression, and any non-zero or non empty (null) value is true.
When the judgment condition is false, the loop ends.
The general format of the while loop is as follows:
while judgment condition:
Execute statement
Example 1 dead cycle
i =1 while i <=3: print('please be careful ')
Example 2: execute three times if conditions permit
i =1 while i <=3: print('please be careful ') i+=1 # (example 2: i = 1 syntax dead loop) while True: print('haha') i += 1 if i == 10: break
Example 3 else can not be written. After the loop conditions are not met, execute the code in else
i = 1 while i <= 3: print('today is nice') i = i + 1 else: print('End of cycle')
Example 4 use while to find the sum of multiples of 3 within 200
i = 1 sumV = 0 while i <=200: if i %3 != 0: i +=1 continue sumV += i i +=1 print(sumV) Python 3 Result: 6633
Example 5
j = 1 while j <= 100: if j == 50: Out of the loop, in the loop body, break The following code is no longer executed break print(j) j = j + 1
Example 6 factorial
Seek 5! = 1 * 2 * 3 * 4 * 5 i = 1 mul = 1 while i <= 5: mul *= i i = i + 1 print(mul)
Python 3 Result: 120
Example 7 summation
# 1 + 2 + ... + 100 i = 1 sumV = 0 while i <= 100: sumV += i i = i + 1 print(sumV)
Python 3 Result: 5050
Example 8
count = 1 while count <= 3: inputAge = int(input('please input age:')) if inputAge > 20: print('Am I so old?') elif inputAge < 20: print('Am I so young?') else: print('You are right!') break count += 1 else: print('Good bye!')
Example 9
i = 1 while i <= 10: j = 1 while j <= 10: print('i=%d j=%d'%(i, j)) j = j + 1 if j == 5: break i = i + 1 while Medium continue var = 7 while var > 0: var = var -1 if var == 5: continue print ('Current variable value :', var) print ("Good bye!") Python 3 result: Current variable value : 6 Current variable value : 4 Current variable value : 3 Current variable value : 2 Current variable value : 1 Current variable value : 0 Good bye! while Medium break var = 7 while var > 0: print('The value is :', var) var = var - 1 if var == 5: break print("Bye bye!") Python 3 result: The value is : 7 The value is : 6 Bye bye!
Similar to the syntax of if statement, if you have only one statement in the body of while loop, you can write the statement on the same line as while
flag = 'python' while (flag): print ('Welcome to study Python!') print ("Good bye!")
The above infinite loop can be interrupted by CTRL+C.
Python 3 Result:
Welcome to Python!
Welcome to Python!
Welcome to Python!
........
........
Welcome to xiaoting'er's blog: https://blog.csdn.net/u010986753
Xiaotinger's python is growing, and there are still many deficiencies. With the deepening of study and work, it will gradually improve and perfect the previous blog content.
Xiaotinger's python is growing, and there are still many deficiencies. With the deepening of study and work, it will gradually improve and perfect the previous blog content.
Xiaotinger's python is growing, and there are still many deficiencies. With the deepening of study and work, it will gradually improve and perfect the previous blog content.
Say something important three times......
● Author: Xiao tinger
● author blog address: https://blog.csdn.net/u010986753
● all rights reserved. Welcome to share this article. Please keep the source for reprint