10. Loop statement
for loop statements, Python only provides while and for, not for...each/do...while/while...do, etc. like other programming languages.
10.1 while cycle
The basic format of while in Python is as follows:
while condition: doSomething
The example code is as follows:
startNum,endNum=0,5 while startNum< endNum: print(f"cur start number is {startNum}") startNum+=1
The output is as follows:
cur start number is 0 cur start number is 1 cur start number is 2 cur start number is 3 cur start number is 4
10.2 for cycle
1. Basic for cycle
The basic format of the for loop in Python is as follows:
for item in [List, tuple, dictionary, set, character, etc]: doSomething
The example code is as follows:
a={1,2,3,4,5} for item in a: print(f"current item is {item}")
The output is as follows:
current item is 1 current item is 2 current item is 3 current item is 4 current item is 5
2.for..else loop
. The basic syntax for mat is as follows:
for item in [List, tuple, dictionary, set, character, etc]: doSomething else: doSomething
sometimes we need to judge whether the program loop exits normally or midway. We can use the following code:
a = [1, 2, 3, 4] flag = True for i in a: if i == 2: flag = False break if flag: print('yes') else: print('no')
for the above writing method, you can use for...else to simplify the writing method, as follows:
a = [1, 2, 3, 4] for i in a: if i == 2: break else: print('yes') print('no')
since Python provides two forms of loop statements, what is the difference between the two, when to use the while loop and when to use the for loop?
When the number of loop iterations is uncertain, use the while loop. When the number of loop iterations is determined, use the for loop
10.3 interrupt statement
- break: interrupt the whole cycle, that is, when the conditions are met, the cycle will be stopped immediately, and subsequent cycles will not continue
- Continue: interrupt the current cycle, that is, when the conditions are met, the current cycle will be terminated, and the cycle will continue when the conditions are met later
The example code is as follows:
print("break loop") for i in range(6): if i == 3: break print(f"current value is {i}") print("continue loop") for i in range(6): if i == 3: continue print(f"current value is {i}")
The output is as follows:
break loop current value is 0 current value is 1 current value is 2 continue loop current value is 0 current value is 1 current value is 2 current value is 4 current value is 5
10.4 traversing container data
1.range() function
The range() function is commonly used to generate a series of numbers. Its basic usage format is as follows:
range(start,end,step)
- Start: start value
- End: end value
- Step: step, which can be positive, negative or omitted. When omitted, the default step is 1
- Use the range() function to generate data, and also follow the rule of "before and after", such as range(0,3). The generated data is 0,1,2
The example code is as follows:
list(range(0,10,2)) # Output results [0, 2, 4, 6, 8] list(range(10,0,-2)) # Output results [10, 8, 6, 4, 2]
2. Traverse string data
for i in "abcdef": print(f"current char is {i}")
The output is as follows:
current char is a current char is b current char is c current char is d current char is e current char is f
3. Traversal tuple
The example code is as follows:
for i in tuple(range(0,5)): print(f"current value is {i}")
The output is as follows:
current value is 0 current value is 1 current value is 2 current value is 3 current value is 4
4. Traversal list
The example code is as follows:
for i in list(range(0,5)): print(f"current value is {i}")
The output is as follows:
current value is 0 current value is 1 current value is 2 current value is 3 current value is 4
5. Traversal set
The example code is as follows:
for i in set(range(0,5)): print(f"current value is {i}")
The output is as follows:
current value is 0 current value is 1 current value is 2 current value is 3 current value is 4
6. Traversal dictionary
The example code is as follows:
dic={ "a":1, "b":2, "c":3, "d":4, } for k,v in dic.items(): print(f"key is {k} , value is {v}")
The output is as follows:
key is a , value is 1 key is b , value is 2 key is c , value is 3 key is d , value is 4
Address: https://www.cnblogs.com/surpassme/p/12975408.html
This article is synchronously published on wechat subscription number. If you like my article, you can also pay attention to my wechat subscription number: woaitest, or scan the following QR code to add attention: