Home  >  Article  >  Backend Development  >  Python basic introduction to process control

Python basic introduction to process control

WBOY
WBOYforward
2022-11-10 17:11:582534browse

This article brings you relevant knowledge about Python, which mainly introduces the relevant content about process control, including selection structure and loop structure. Let’s take a look at it together. I hope everyone has to help.

Python basic introduction to process control

【Related recommendations: Python3 video tutorial

1. Select structure

1.1, if statement

Grammar format

if 表达式:
    代码块

Description: If the expression is true, the following code block will be executed; if the expression is not true, , nothing is executed.

Usage example

age = int(input('请输入您的年龄:'))if age >= 18:    print('已成年,可独自观看')

Running result:

请输入您的年龄:22
已成年,可独自观看

If the entered age is less than 18, the statement block after the if will not be executed; if the entered If the age is greater than or equal to 18, the code block after the if is executed.

1.2, if else statement

Grammar format

if 表达式:
    代码块 1else:
    代码块 2

Description: If the expression is true, execute the if followed by The code block 1 that follows; if the expression does not hold, the code block 2 that follows else is executed.

Usage example

age = int(input('请输入您的年龄:'))if age >= 18:    print('已成年,可独自观看')else:    print('未成年,请在家长的陪同下观看')

Running result:

请输入您的年龄:22
已成年,可独自观看
请输入您的年龄:3
未成年,请在家长的陪同下观看

If the entered age is greater than or equal to 18, execute the statement block after the if; if the entered If the age is less than 18, the code block after else is executed.

1.3, if elif else statement

Grammar format

if 表达式 1:
    代码块 1elif 表达式 2:
    代码块 2elif 表达式 3:
    代码块 3...//其它elif语句else:
    代码块 n

Description: Python will judge the expression one by one from top to bottom Whether the expression is true or not, once a true expression is encountered, the following code block will be executed; the remaining code will not be executed after that, regardless of whether the following expression is true or not. If all expressions are false, the code block after the last else is executed.

Usage examples

scope = int(input('请输入分数:'))if scope >=90:    print('优秀')elif scope >=80:    print('良好')elif scope >=70:    print('一般')elif scope >=60:    print('及格')else:    print('李在赣神魔?')

Running results:

请输入分数:88
良好
请输入分数:30
李在赣神魔?

Notes:

  • if, elif, There is a colon at the end of the else statement:

  • The code blocks following if, elif and else must be indented (the default indentation is 4 spaces), and the indentation of the same code block The amount must be the same, and those with different indentation amounts do not belong to the same code block.

  • elif and else cannot be used alone and must be used together with if.

2. Loop structure

2.1. for statement

Grammar format

for 临时变量 in 可迭代对象:    代码块

Iterable objects include: strings, lists, tuples, dictionaries, and collections

Perform a for loop on the values ​​

Implement traversal and execution from 1 to 100 Accumulation:

result = 0for i in range(101):
    result += iprint(result)

Execution result:

5050

range function

range() function is used to generate a series of consecutive integers, often combined with for loops use.

Usage example: Return an integer in the interval [0,5) (left-closed, right-open interval):

for i in range(5):    print(i)

Execution result:

0
1
2
3
4

Return [1,5) interval Integer:

for i in range(1, 5):    print(i)

Execution result:

1
2
3
4

When using the range() function, you can also specify the step size: return an odd number within 1-15

for i in range(1,15,2):    print(i)

Execution result : Start printing from 1, and then continue to add 2 until it reaches or exceeds the final value

1
3
5
7
9
11
13

Perform a for loop on lists and tuples

my_list = [1,3,5,7,9,11,13]for i in my_list:    print(i)print("==============================")
my_tuple = (2,4,6,8,10,12)for i in my_tuple:    print((i))print("==============================") 
#打印列表元素的下标,len():返回列表的长度for i in range(len(my_list)):    print(i)

Execution results:

1
3
5
7
9
1113
==============================2
4
6
8
1012
==============================0
1
2
3
4
5
6复制代码

Perform a for loop on the dictionary

Use a for loop to traverse the dictionary directly, returning the key in each key-value pair and the return value of the keys() method are the same:

my_dict = {'name':'李逍遥','age':'18','addr':'逍遥谷'}for i in my_dict:    print(i)

Execution result:

name
age
addr

2.2, while loop

Syntax format: When the condition is true, it will always Execute the following code block (or loop body)

while 条件表达式:
    代码块

Use example

Print all numbers from 1 to 100:

i = 0while i < 100:
    i+=1
    print(i)

Use while to traverse one String variable:

my_char="http://weipc.com"i = 0while i<len(my_char):    print(my_char[i],end="")
    i+=1

end is the parameter in the print function, which means it ends with the given string or tab character without default newline.

Execution result:

http://weipc.com

Notes:

  • The code in the while loop body must be indented by the same amount (default indentation is 4 spaces)
  • When using a while loop, you must ensure that there are conditions for exiting the loop, otherwise it will be an infinite loop.

While loop is used in combination with else

When the judgment condition in the while loop is not met and the loop is jumped out, the code block after else will be executed first:

my_char="http://weipc.com"i = 0while i<len(my_char):    print(my_char[i],end="")
    i+=1else:    print('循环退出')

Of course, you can also add an else code block inside the for loop:

my_char="http://weipc.com"for i in  add:    print(i,end="")else:    print('循环退出')

The selection structure and the loop structure can also be nested in each other.

Termination of the loop

Python provides 2 ways to terminate the loop:

  • continue, terminate this loop, go to And execute the next cycle.

  • break can completely terminate the current loop.

【Related recommendations: Python3 video tutorial

The above is the detailed content of Python basic introduction to process control. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.im. If there is any infringement, please contact admin@php.cn delete