One, print statement, import statement, assignment statement.
1.1. Print Statement: output
>>> print (2,3,4)//python2.x
(2, 3, 4)
>>>
>>> Print (//python3.x)
1 2 3
>>>
Note: The print function inserts a space character between each parameter when it is output
In python2.x, the parameters of print form a tuple, but in python3.x, the print parameter forms a tuple.
If you do not want to leave spaces between the arguments, use the connection character "+" (plus sign). Such as:
>>> print (1+2+3)//Add by integer
6
>>> print (' 1 ' + ' 2 ' + ' 3 ')//character connection operation.
123
>>>
1.2. Import statement: Importing something as another thing .
Import function method from module one: Import somemodule
Import function method from module two: from Somemodule import somefunction
Import function method from module three: fromsomemodule import somefunction, anotherfunction,yetanotherfunction
Import function from module method four: from somemodule import *
Import function method from module five: Import math as Foobar //Add module alias , keyword as after module alias
Import function method from module VI: From math import sqrt as Foobar //Add function alias , keyword as after function alias
such as: From Module1 import open as Open1
From module2 import Open as Open2
Method Four should be used only if you are sure you want to import all features from a given module. If a function is available in two modules, such as the open function, you can import it using method one and then use the function as follows:
Module1.open (...)
Module2.open (...)
1.3. Assignment statements
A), sequence unpacking: multiple assignments at the same time (the variables on both sides need to be the same number)
Such as:
|
In python3.x
>>> x,y,z=1,2,3
>>> x
1
>>> y
2
>>> Z
3
>>> print (x, y, z)
1 2 3
>>>
|
in python2.x
>>> x,y,z=1,2,3
>>> x
1
>>> y
2
>>> Z
3
>>>
>>> print (x, y, z)
(1, 2, 3) |
>>> x,y,z=1,2,3
>>> print (x, y, z)
(1, 2, 3)
>>> x,y,z=z,x,y//Exchange variable
>>> print (x, y, z)
(3, 1, 2)
>>>
Note: Sequence unpacking (or iterative unpacking) unlocks the sequence of multiple values and then places them in a sequence of variables. A more vivid representation of the method, such as:
>>> values=1,2,3
>>> values
(1, 2, 3)
>>> x,y,z=values
>>> Print (x,z,y)
(1, 3, 2)
>>>
Take a key-value pair from the dictionary:
>>> d={' name ': ' Zhangsan ', ' age ': ' 30 '}
>>> D
{' Age ': ' + ', ' name ': ' Zhangsan '}
>>> Key,value=d.popitem () //assigns a value to a variable (key, value) using the Popitem method (a key-to-values delete in a random dictionary).
>>> Key
' Age '
>>> value
' 30 '
>>> Print (Key,value)
(' Age ', ' 30 ')
>>> D
{' name ': ' Zhangsan '}
>>>
b), Chain-type assignment:
Chained assignment (chained assigment) is a shortcut to assigning the same value to multiple variables.
Such as:
>>> X=y=1
>>> x
1
>>> y
1
>>> |
Equivalent to:
>>> Y=1
>>> x=y
>>> x
1
>>>
|
c), increment assignment (+ =,-=, *=, \=,%=)
X+=1: That is x=x+1, other increments are similar.
Note: The same applies to other data types (as long as the two-dollar operator itself applies to these data types):
Such as:
>>> a= ' Love '
>>> a+= ' u '
>>> A
' Loveu '
>>> a*=3
>>> A
' Loveuloveuloveu '
>>>
Second, the statement block
A statement block is a set of statements that executes or executes multiple times (a loop statement) when the condition is true (a conditional statement). You can create a statement block by placing a space in front of the code to indent the statement. Of course the tab character can also indent the statement block, a tab is 8 spaces, it is recommended to use a space indent
Many programming languages use special words or characters (such as begin or {) to denote the beginning of a block of statements, and another word (end or}) to represent the end of a block of statements. in Python, a colon (:) is used to identify the beginning of a statement block, and each statement in the block is indented (the same amount of indentation). Represents the end of the current statement block when it is rolled back to the same indent as the closed block .
III. conditions and Conditional statements:
Python statement--conditional statement