‘’’
In our programming process , You need to traverse various data , For example, traversing a string , list , aggregate , Dictionaries , Tuples and so on ,
At this time, it is often necessary to use circular statements for processing .
python Loop statements mainly include for,while Two expressions
‘’’
‘’’
for variable in iter_object
test_module
variable: by Customize a temporary variable
iter_object: Is an iterable object
test_module: It is the processing module of the program
‘’’
print('console'.center(50,'-'))
# Traverse the list
x = [1,2,3,4]
for x_index in x:
print(x_index)
print('*'*20)
# Traversal string
x = 'hello,world'
for y_index in x:
print(y_index)
print('*'*20)
# Ergodic dictionary
x = {
'name':'xu','age':18}
for x,y in x.items():
print(x,y)
print('*'*20)
#1+2+3+...+100 Methods
# Use range Generate a 1 To 100 Sequence
sum = 0
for rax in range(1,101):
sum+=rax
print(sum)
---------------------console----------------------
1
2
3
4
********************
h
e
l
l
o
,
w
o
r
l
d
********************
name xu
age 18
********************
5050
‘’’
while Cycle is python Another way of cycling
The format is :
while condition:
test_module
condition: Is conditional control
test_module: The running module of the cycle
‘’’
# Infinite loop
print('console'.center(50,'-'))
while True: # When the gear condition is true , The statement indented by the space below will be executed consistently
print(' Infinite loop ')
---------------------console----------------------
Infinite loop
Infinite loop
Infinite loop
...
#1+2+3+...+100 Methods
print('console'.center(50,'-'))
x = 1
sum = 0
while x<=100: # When the conditions are met, the while Code inside , If the condition is not satisfied, the loop will jump out , To perform the
sum+=x
x+=1
print(sum)
---------------------console----------------------
5050
‘’’
break Statements are often used in the process of loop statements in combination with conditional control statements
perform break Statement will jump out of the entire loop statement
‘’’
print('console'.center(50,'-'))
for ins in range(1,10):
if ins==5: # When the conditions are met, the break sentence , The break The following code will not execute , Jump out of the entire loop code
print(' I'm ready to jump out of the loop ')
break
print(ins)
print(' I jumped out of the loop ')
---------------------console----------------------
1
2
3
4
I'm ready to jump out of the loop
I jumped out of the loop
-------------
‘’’
continue sentence Statements are often used in the process of loop statements in combination with conditional control statements
continue sentence Statement will jump out of the current loop , But it won't jump out of the whole cycle
‘’’
print('console'.center(50,'-'))
for ins in range(1,10):
if ins==5: # When the conditions are met, the continue sentence , The continue The following code will not execute , Continue the next new cycle
print(' I'm ready to jump out of the current cycle ')
continue
print(ins)
print(' I jumped out of the loop ') # Execute after the loop is completed
---------------------console----------------------
1
2
3
4
I'm ready to jump out of the current cycle
6
7
8
9
I jumped out of the loop