python---loop statement

loop statement

 

1.5 cycles

1.5.1 while loop

Set the condition of the loop, the suggestion can be terminated. Space indentation is very important.

1 Introduction

  • What is the condition and until when.
  • What is the operation of the condition

2. Basic Grammar

3. Cycle 100 times

# 循环
i=0
while i<100:
    print("数字",i)
    i+=1

cycle 100 times

4. Find the sum of 1-100

# 1-100的和
i=1
sum=0
while i<=100:
    sum+=i
    i+=1
print(f"1-100的和{sum}")

1.5.2 Case of while loop

Still a case of guessing numbers

# 猜数字
# 导包
import random
# 输出1-10之间的随机数
num=random.randint(1,10)
print(f"随机数{num}")
i=0 #猜测的次数
flag=True #循环的标记
temp=0 #记录当前用于猜测的值
while flag:
    temp=int(input("输入你要猜测的值"))
    if temp==num:
        flag=False
        print("恭喜你猜中了")
    elif temp>num:
        print("你猜大了")
    else:
        print("你猜小了")
    i+=1

print(f"猜测的次数{i}")

1.5.3 Nesting of while loops

Space indentation is key! ! !

Eat three meals a day, eat three steamed buns for each meal, eat one by one

#while嵌套
i=1
j=1
while i<=3:
    print(f"第{i}顿饭")
    while j<=3:
        print(f"吃第{j}","个馒头")
        j+=1
    j=1
    i += 1

multi-level nesting

1.5.4 Nested case of while loop

Printing of ninety-nine multiplication table

Replenish:

  • How to print without wrapping
  • how to tab

# 不换行输出
print("hello")
print("word")
# end是一种传参
print("hello",end='')
print("word",end='')

# 制表符 ,本质就是按下去tab键
print("\t")
print("hello \t word")
print("hi \t zhangsan")

2. Generation of ninety-nine multiplication table

# 99乘法表
# 行和列内容,行数是固定的,每一行多少列数和行数是有关的.行的内容就是i*j.输出完一列需要换行,不换行的需要制表符

# i行数.j列数
i=1
j=1
while i<=9:
    while j<=i:
        print(f"{i}*{j}={i*j}\t",end='')
        j+=1;
    print()
    j=1
    i+=1


1.5.5 for loop

1 Introduction

  • The condition is not like the while loop, and the condition can be specified arbitrarily
  • Generally the value is the specified number of iterations

2. Case 1: Strings are printed sequentially. Take characters one by one

  • It is almost the same as fetching objects

# 循环取字符
name ="hello world"
# name的内容挨个取出给x
for x in name:
    print(x)

3. Case 2 - count the number of occurrences of a character


# 统计a出现的次数
i=0
name ='a b c c d d d  a a kk'
for x in name:
    if x=='a':
     i+=1

print("a出现的次数,%d" %i)

1.5.6 range type

sequence type. Strings, tuples, lists, and tuples are all types of sequences.

  • The essence of the for loop is to traverse the sequence.
  • range(num), from 0 to num-1. does not contain num

# range
for x in range(5):
    print(x,' ',end='')

print()

# 一个一个的迭代
for x in range(5,10):
    print(x,' ',end='')

print()

# 有步长
for x in range(5,10,2):
    print(x,' ',end='')

print()

summary

1.5.7 Temporary scope of for loop

1. Originally written

# 有步长
for x in range(5,10,2):
    print(x,' ',end='')
print()
print(x)

The outside is accessible, and it is accessible when printing. But it is not recommended. .

It is a soft requirement, not a hard requirement, so it is recommended not to use it. It is recommended to define in advance when using it to conform to the grammatical rules.

2. Summary

1.5.8 for loop nesting

The case of eating

#for嵌套
for i in range(1,4):
    print(f"第{i}顿饭")
    for j in range(1,4):
        print(f"吃第{j}","个馒头")
        j+=1
    j=1
    i += 1

summary

1.5.9 for loop 99 multiplication table

for+for

for i in range(1,10):
    for j in range(1,i+1):
        print(f"{i}*{j}={i*j}\t",end='')
        j+=1;
    print()

for+while

# 99乘法表
# 行和列内容,行数是固定的,每一行多少列数和行数是有关的.行的内容就是i*j.输出完一列需要换行,不换行的需要制表符

# i行数.j列数
for i in range(1,10):
    j=1
    while j<=i:
        print(f"{i}*{j}={i*j}\t",end='')
        j+=1;
    print()
    i+=1

The effect of the above two methods

1.5.10 Loop break keyword

1 Introduction

# 循环中断
for i in range(0,10):
    if i%5==0:
        print("中断")# 循环中断
for i in range(1,10):
    print(i)
    if i%5==0:

        print("中断")
        break
    else:
        continue
        break
    else:
        continue

summary

Guess you like

Origin blog.csdn.net/weixin_41957626/article/details/129781761