Table of contents
loop statement
In Python, common loop statements are while loop and for loop. A while loop is a loop that repeatedly executes a block of code while a condition is met, while a for loop is a loop that iterates over a sequence.
while loop statement
If the condition is met, it can continue to loop.
Syntax:
while condition:
When the condition is met, do event 1
When the condition is met, do event n
Note: The condition needs to get Boolean type, True or False
Example:
Calculate and output the sum of 1 to 100.
i = 1
sum = 0
while i <= 100:
sum += i
i += 1
print(f"1到100的和:{
sum}")
Running result: sum of 1 to 100: 5050
Nested use of while loop
while condition 1:
when condition 1 is met, do thing 1
when condition 1 is met, do thing n
while condition 2: when
condition 2 is met, do thing 1 meet
condition 2, do thing
n
, the challenge was successful on the 100th day.
i = 1
while i <= 100:
print(f"今天是开始跳绳的第{
i}天")
# 内层循环的控制变量
j = 1
while j <= 10:
print(f"今天的跳的第{
j}个")
j += 1
print("加油努力坚持")
i += 1
print(f"坚持到{
i - 1}天,挑战100天每天10个跳绳成功。")
operation result:
The output does not wrap
Add end='' after the output content
Example:
print("Hello", end='')
print("World", end='')
Running result: HelloWorld
output alignment
\t The effect is equivalent to the tab key, which can align multi-line strings
Example:
print("Hello\tWold")
print("Hi\t\tWorld")
operation result:
Comprehensive case:
output and print a nine-nine multiplication table.
Use while loop to print ninety-nine multiplication table
for loop
Polling, processing one by one, until the end.
Syntax:
for temporary variable in data set (sequence) to be processed:
the code executed when the loop meets the condition
Note:
①The for loop cannot build an infinite loop.
②The data set to be processed (that is, the sequence type) can be a string, a list, a tuple, etc.
Example:
# 遍历name中有多少个a
name = "I am a hardworking girl"
count = 0
for x in name:
if x == "a":
count += 1
print(f"{
name}中共有:{
count}个a。")
Running results: There are 3 a's in I am a hardworking girl.
range statement
①range
Get the sequence of numbers starting from 0 and ending with num, excluding num itself
Example: range(3) gets 0, 1, 2
②range(num1,num2)
Get the sequence of numbers starting from num1 and ending with num2, excluding num2 itself
Example: range(1,3) gets 1, 2
③range(num1,num2,step)
Example: get the sequence of numbers starting from num1 and ending with num2, excluding num2 itself,
and the step size between numbers is based on step, and step defaults to 1
example: range(5,10,2) gets 5,7,9
comprehensive case:
# range(num)
for x in range(10):
print(f"{
x}\t", end='')
print() # print(),输出一个换行
# range(num1,num2)
for x in range(5, 10):
print(f"{
x}\t", end='')
print() # print(),输出一个换行
# range(num1,num2,step)
for x in range(5, 10, 2):
print(f"{
x}\t", end='')
print() # print(),输出一个换行
# 打印10个加油
for x in range(10):
print(f"加油\t", end='')
operation result:
Nested use of for loops
for temporary variable in data set to be processed (sequence):
code 1 executed when the loop meets the condition
n
for temporary variable in data set to be processed (sequence):
code 1 executed when the loop satisfies the condition Example
of code n executed when the loop meets the condition
:
the output prints a nine-nine multiplication table.
Use for loop to print ninety-nine multiplication table
continue and break interrupt the loop
continue: interrupt this loop and go directly to the next one (temporary interruption)
break: interrupt the entire loop (permanent interruption)
Points to note: When nested, it can only take effect on the loop of the current layer, and does not affect the loop of the upper layer.
Example:
for i in range(1, 5):
print(f'今天是第{
i}天')
continue
print(f'今天是第{
i}天') # 总共循环4次,此句都不会执行
for i in range(1, 5):
print(f'明天是第{
i}天')
break
print(f'明天是第{
i}天') # 总共循环1次,此句不会执行
Running results:
comprehensive case:
use for loop combined with continue, break to complete scholarship distribution