Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze the Control of python process

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

What this article shares with you is about how to analyze the control of python process. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Foreword:

After a simple understanding of the basic syntax and use of python, it is found that they are all sequentially executed statements, that is, they are executed sequentially, without any branches, loops, and will not return to the previous execution statements-process control. In practical use, of course, it is inevitable to use process control statements, but they are controlled by logical expressions, so let's first introduce Boolean logic.

1. Boolean logic

What is Boolean logic?

To put it simply, Boolean logic is to manipulate true values. Like most programming languages, Python also uses Boolean logic to make decisions, expressed in True and False. Four Boolean logic operators are commonly used, not, and, or, and = =. In the use of the Python language, almost all judgment decisions can be represented by these four logical operators. First of all, we calculate the possible values produced by Boolean logic. suppose that there are two variables, TRUE b, then there are four combinations of either true or false for them, as shown in the following table: F for FALSE,T represents TRUE

A

B

A few years ago

A few years ago

An and b

An or b

Not b

F

F

T

F

F

F

T

F

T

F

T

F

T

T

T

F

F

T

F

T

F

T

T

T

F

T

T

F

Feel the result of Boolean logic calculation intuitively through the code:

= = (logically equal), when an and b contain the same true values, that is, both are True or both are False, the result of the expression axionomb is True.

! = (logic varies)

And (logic and), the result of a Boolean expression is True only if both an and b are True, and False in other cases

Or (logical OR), the Boolean expression an or b is True only if at least one of an and b is True

Not (logic is not), not true is false, not false is true

Where there is calculation, the priority of calculation will be involved. for the priority of Boolean operations, the rules are as follows.

Priority from high to low:

An and b, not, an or b, a! = b

For example: not True and False or True, the result of the execution is shown in figure True. You can see that the order of execution is not True, not Ture (and False), and finally or, so let's simplify whether the result of this evaluation is to calculate False and False or True. This is also a small step in optimization! Of course, let's consider based on the above table: and takes precedence over or, so calculating False and False is equivalent to False. Then the final expression is to evaluate False or True.

We randomly add parentheses to the previous expression to see how parentheses affect the evaluation of Boolean expressions. At the same time, expressions containing parentheses are often encountered when Boolean logical expressions are used in loop statements or if conditional statements. As shown below:

Therefore, when you need to use more complex Boolean logical expressions, you need to pay special attention to the use of parentheses!

After understanding the rules of Boolean logic, Python, like other languages, uses certain methods to improve the speed of Boolean expression calculation, which is called short-circuit evaluation.

If you are interested, you can have an in-depth understanding of the implementation principle.

Operation rules

Calculation result

An or b

If an is False, the result is b, otherwise the result is a

An and b

If an is False, the result is a, otherwise the result is b

This logical expression is often used by Python users. Intuitively, take True or Any as an example, no matter what the value of the Any is, the result of the expression is True. Similarly, the result of False and Any is False. But keep in mind that while the logic is easy to use, it will also have a poor sense of readability to the code. You can use the if conditional statements mentioned below to realize the operation rules.

1.1 conditional control statement-if

If- conditional control statement, its function is to make the code program make decisions according to certain conditions when it is executed. If statements are almost used in the research and implementation of text processing, script development, website development, data science and so on.

1.2 if/else statement

Suppose there is a simple small requirement: boot requires the user to enter a user name and password, if correct, let them log in, if incorrect tell the password is incorrect.

If _ _ name__ = = "_ _ main__": while True: scan_user = input ('please input your name:') scan_pass = input ('please input your passwd:') if scan_pass = 'if/else': print (' Logging on current') Else: print ('your passwd is inactive clients') Print ('I'm not in the if/else statement!')

Execution result:

As you can see, if the variable scan_pass points to a string of "if/else", it shows that the login was successful, and if not, any value of the variable indicates a password error.

From this, let's draw a basic sentence structure of if/else:

In the use of Python,: is usually used as the closing tag of the if statement header, loop header, and function header. Also, the code block can contain any number of Python statements and other if statements. In Python programming, you also need to indent a code block, but compared with other languages for beauty, Python must use indentation to indicate the ownership of a code block. The last line of code in the example above is not in the else statement.

1.3 if/elif statement

If/elif statement is an extension of if/else statement, a lot of logic implementation will not only contain two conditions, it may contain multiple conditions. For example, some preferential conditions set by a shopping mall: 20% discount for 200, 40% discount for 400, 50% discount for 800 over 400, and so on.

Such a code is implemented as follows:

If _ _ name__ = = "_ _ main__": while True: consumer = int (input ('Please enter your consumption amount:') money = 0 if 200

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report