程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

11.python loop statement

編輯:Python

List of articles

  • Loop statement
      • for loop
      • while loop
      • break sentence
      • continue sentence

Loop statement

‘’’
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 loop

‘’’
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 loop

‘’’
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 sentence

‘’’
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

‘’’
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

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved