The tutorial of using for loop and while loop in python, the way of python that Xiaobai can also learn

table of Contents

 

Preface

One, for loop:

For loop format:

Code example

Use of range() function

Two, while loop:

while loop format

Code example

Three, two kinds of cycle comparison


Preface

In python, to achieve "repetitive and automatic code execution", there are two loop statements for us to choose from:

One is a for...in...loop statement, and the other is a whileloop statement.


One, for loop:

For loop format:

Code example

for i in [1,2,3,4,5]:
   print(i)

Running effect chart:

Of course, the loop here can not only be lists, but also dictionaries and strings, not integers, floating-point numbers,

If it is a dictionary, it will print out all the 【keys】 in a loop; if it is a string, each string will be printed out in order

For example, in the above example, [1,2,3,4,5] is a list, we can also try a dictionary

ages = {'张三':18,'李四':19,'王五':20}

for i in ages:
    print(i)
    
names = '赵六'
for j in names:
    print(j)

running result

Can not be integer and floating point examples

a = 50
b = 0.5

for i in a:
    print(i)

for i in b:
    print(i)

 

 

In addition to the three data types of lists, dictionaries, and strings, we can also traverse other data sets. For example, it is often used with for loops: the range() function.

 

Use of range() function

Using the range(x) function, you can generate a sequence of integers from 0to x-1.

range(a,b), you can generate a sequence of integers from a to b-1. (It is a function that takes left but not right)

range(a,b,n), you can generate an entire sequence from a to b-1, and the interval is n

range(a,b) is actually a special range(a,b,n), if n is not filled in, it defaults to 1

such as

for i in range(5):
    print(i)
print('------------------------')
for j in range(5,8):
    print(j)

for i in range(0,7,2):
    print(i)
print('--------------')
for j in range(0,6,2):
    print(j)

 


Two, while loop:

while loop format

Code example

The while statement can enter the code inside the while and execute it only when the conditions behind whlle are met, otherwise it won’t be entered nor executed

a = 0
while a < 5:
    print(a)
    a=a+1

We can also use the while and input() functions to achieve continuous input, and only when the input result meets certain conditions, it ends

password = ''  # 变量password用来保存输入的密码
while password !='789':
    password = input('请输入您正确的密码:')
print('恭喜你登陆成功!')

 

Three, two kinds of cycle comparison

According to the above code example, we can know that the for loop is used for a relatively small number or some fixed values, we can use the for loop to print

If there is a large number, and there is a certain pattern or a certain condition is met, we can use the while loop to print

But in most cases, for and while achieve the same effect, you can use it appropriately.

We can use for and while codes to print the number from 1 to 7, and not the number 4

Code example

for i in range(1,8):
    if i!=4:
        print(i)
print('----------------')
a = 1;
while a<=7:
   if a!=4:
    	print(a)
   a=a+1

 

Guess you like

Origin blog.csdn.net/qq_27471405/article/details/104013515