Python Statements and syntax
Python Program Structure:
1. The program is composed of modules.
2. The module contains statements.
3. The statement contains an expression.
4. The expression establishes and processes the object.
Python's syntax is essentially composed of statements and expressions. The expression handles the object and is nested within the statement. Statement is also where the object is generated (for example, an expression in an assignment statement), some statements completely generate new object types (functions, classes, and so on). Statements are always present in modules, and the modules themselves are managed by statements.
Python statement:
| Statement |
Role |
Example |
| Assign value |
Creating reference values |
A,b,c = ' good ', ' bad ', ' ugly ' |
| Call |
Execute function |
Log.write (' Hello,world ') |
| Print Call |
Print objects |
Print ("Hello World") |
| If/elif/else |
Select action |
If ' print ' in Text:print (text) |
| For/else |
Sequence iterations |
For x in Mylist:print (x) |
| While/else |
General circulation |
While X>y:print ("Hello") |
| Pass |
Empty placeholder |
While True:pass |
| Break |
Loop exit |
While True: If Exittest (): Break |
| Continue |
Continue the next cycle |
While True: If Skiptest (): Continue |
| Def |
Functions and methods |
def f (a,b,c=1,*d): Print (A+b+c+d[0]) |
| Return |
function returns the result |
def f (a,b,c=1,*d): return a+b+c+d[0] |
| Yield |
Builder expression |
Def gen (n): For i in N:yield i * 2 |
| Global |
namespaces, global variables |
x = ' old ' def function (x): Global x,y,;x = ' new ' |
| Nonlocal |
Local Variables (python3.0+) |
def outer (): x = ' old ' def function (): nonlocalx;n= ' new ' |
| Import |
Module access, importing |
Import Sys |
| From |
Module Property Access |
From sys import stdin |
| Class |
Creating objects |
Class Subclass (Superclass): Staticdata = [] Def method (self): Pass |
| Try/except/finally |
Catching exceptions |
Try Action () Except Print (' Action error ') |
| Raise |
Triggering an exception |
Raise Ebdsearch (location) |
| Assert |
Debug check |
Assert x>y, ' x too small ' |
| With/as |
Environment Manager |
With open (' data ') as MyFile: Process (myfile) |
| Del |
Delete Reference |
Del Data[k] Del Data[i:j] Del obj.attr Del variable |
Terminating a line is the terminating statement
x = 1;
In Python, the general principle is that the end of a line automatically terminates the statement that appears on that line. That is, you can omit the semicolon.
x = 1
- The end of a line is to terminate the line statement (no semicolon).
- A nested statement is a block of code and is related to the actual indentation (without curly braces).
Although the statement is generally one line, it is possible in Python that a line is squeezed into multiple statements, separated by semicolons:
>>> a = 1;b = 2; Print (A + b)3
This is the only place in Python where a semicolon is required--as a statement delimiter.
Another special rule of a statement is that the range of a statement can span multiple lines. In order to do this, you only need to enclose the statement in parentheses (), square brackets [], or the curly braces {} of the dictionary. Any program code enclosed in these symbols can span several lines. Statement will run until it encounters a row that contains a closed parenthesis.
>>> mylist = [111222333444]>>> mylist[111, 222, 333, 444]
Parentheses can contain everything-because any expression can be included.
x = (a + b + c + d )ifand and = = 3) : Print ('helloWorld')
There is an old rule that also allows us to span several lines-when the previous line ends with a backslash, you can continue on the following line:
X = A + B + + D
Indent in
The Python compound statement is styled in indented code. typically 4 spaces.
while (x > 0): ---1---- ---2----... .
Assignment statements \ Expressions and printing
Python Statements and syntax