程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Statements, expression statements, expressions in Python

編輯:Python

This article has participated in 「 New people's creation ceremony 」 Activities , Start the road of nuggets creation together .

Python The statement in 、 Expression statement 、 Expression differences

The content discussed in this paper is based on 《python Learning manual 》 And official python Reference manual

1、 What is a sentence ?

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 .

2、 Expression statement

An expression statement is a kind of statement .== Generally, expression statements are required when calling functions and methods ==.

3、 expression

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 :

Python Medium break、continue、pass sentence

One 、 Basic introduction and general loop format

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 .

Two 、pass

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.

3、 ... and 、continue

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 .

Four 、break

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 .

5、 ... and 、 loop else

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 )》

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved