This article has participated in 「 New people's creation ceremony 」 Activities , Start the road of nuggets creation together .
The content discussed in this paper is based on 《python Learning manual 》 And official python Reference manual
The previous articles , It introduces python Core object types in : There are numbers 、 character string 、 list 、 Dictionaries 、 Tuples etc. .python In addition to these core types , There is also an important foundation —— sentence . So what is the sentence ? A sentence is a sentence written to tell python What should I do .
The program consists of modules -> The module contains statements -> Statement contains expression -> Expressions handle objects
The whole logic is : Use statements to implement programs —— Guide expressions to handle core object types python The statements in are : Basically, statements and core object types make up python All , There is a familiar circular judgment statement in the statement 、 Functions and classes . The whole program is basically composed of these , So we can easily understand this concept .
An expression statement is a kind of statement .== Generally, expression statements are required when calling functions and methods ==.
Expressions are different from statements , So an expression is strictly different from an expression statement . for instance :yield Statement and yield expression , You can look at it yield Explanation of the statement :
This article is in while Circular discussion , These statements can also be used in for In the sentence The general loop format is : Let's introduce how they are used together .
pass A statement is a placeholder statement without an operation , No operation means no operation , Placeholder means that this statement only occupies one line of code . It is mainly used in : Write an empty topic . such as
while true:
pass
def func_1():
pass # When a function has no body , But when you need this function later , Use pass placeholder
When you are writing a project theme or some architecture , When you need a function, but you don't need to write down the function topic , You can use pass.
continue The statement immediately jumps to the top of the loop . It means not to carry out continue Statement , Start directly at the top of the loop . For example, output even numbers :
x = 10
while x:
x = x - 1
if x % 2 != 0:
continue
print(x)
Of course , It doesn't have to be so troublesome to output even numbers , The above example is just continue A simple example of a statement . At the same time, it should be noted that ,continue Use less as far as possible , This statement has an impact on readability and maintainability .
break Statement ends the current loop .continue Will execute from the beginning of the loop , and break Statement is to break out of the loop . It can be used break To avoid some nesting , For example while Use in if Statement for conditional judgment , To carry out break Statement to jump out of a nested loop , wait .
loop else Statements are generally similar to break Statement combination . In circulation , There is no trigger break sentence , Execute at the end of the loop else sentence . for example : The above example mainly makes you understand else and break Combined use means , Practical usage, for example : Judge positive integers y Is it a prime number How not to execute break, Then the number is prime .
Part of this article is excerpted from 《python Learning manual ( The Fourth Edition )》