What's program? The program consists of commands divided into four categories: expressions, assignment statements, Process Control statements, I/O statements .
An expression : The operator joins the operand and finally calculates a value. function calls can also be seen as part of an expression. Because their return values can be connected to other data through the operator. When an expression is connected to an if/while/for, it is called a conditional expression.
Assignment Statement : Assigns a value to a variable.
Flow control Statements : Three control flow statements , if/while/for. Terminates the loop with break and jumps out of the current loop module with continue. (any program can use only three structures: order, select, loop to write)
I/O: Used to complete the interaction, get input from the user/file, and then print some results, or output to a file.
operators : Different operators have different precedence, and you can use () to group operators and operands to make the program more understandable. The general operators are combined in order from left to right and some assignment operators are in the right-to-left order, for example a = b = c is processed to a = (b = c).
Control Flow :
if: judge the condition as true, execute the if-statement block, or execute the else/elif-block. Else/elif clauses are optional.
If a==1:
print ' A '
Elif a<1:
print ' B '
Else
print ' C '
A colon: represents followed by a statement block, and the same indented statement represents them as the same statement block.
while : The condition is true, the loop body is repeated, and the ELSE clause is optional. (assuming a initial value of 1, refer to the following program segment)
a = Int (raw_input ())
While a! = 1:
If a > 1:
A = A-1
Else
A = a + 1
Else
Print (' A = 1 finally! ')
for...in : Another looping statement that iterates over an object in a sequence (list) , using each item in the queue one at a--the ELSE clause is optional.
For I in range (1, 5):
Print I
Else
print ' The For loop was over '
Break : Terminates from the for or while loop, and the other loop body will no longer execute.
continue: Skips the remaining statements in the current loop block and proceeds to the next round of loops.
function : is a reusable program segment, defined with the DEF keyword, followed by a function name and (), which can contain variables in parentheses to: end.
Def Printmax (A, b=5):
If a > B:
Print a, ' is maximum '
Else
Print B, ' is maximum '
Printmax (3, 4)
Parameters at the end of the parameter can have default parameter values, which cannot be reversed, for example, Def func (A, b=5) is valid, but def func (a=5, b) is invalid.
The return statement can return a value from a function, while jumping out of a function, with no return function, equivalent to none on return.
Data Type :
Note
Constant names are generally capitalized; the variable does not need to define the data type in advance; the "three quotation marks can indicate a string containing a newline, and the middle can contain single quotes ' and double quotes"; The escape character is a backslash \; The identifier is preceded by a letter, an underscore, and a number, and is case sensitive; , can be used to represent the end of a logical flight statement, if you want to write two logical line statements in a physical line, you can use a semicolon, but not recommended; Docstrings document string. __doc__ can print a document string;
Reference
1. "Invent Your Own computer games with Python"--chanpter4--summary
2. "A byte of Python"
Python Learning log 01_ basic syntax