python Statements in :

Assignment statement

if sentence , Run the statement block when the condition is true . Often with else, elif( amount to else if) Use together .

for sentence , Pass through list , character string , Dictionaries , Set iterator , Process each element in the iterator in turn .

while sentence , When the condition is true , Loop statement block .

try sentence . And except, finally, else Use together to handle exceptions during program operation .

class sentence . Used to define the type .

def sentence . Methods for defining functions and types .

pass sentence . Indicates that this line is empty , Do not run any operations .

assert sentence . Used to test whether the running conditions are met during the program adaptation phase .

with sentence .Python2.6 Syntax defined later , Running a statement block in a scenario . such as , Lock before running statement block , Then release the lock after the statement block exits .

yield sentence . Use within iterator functions , Used to return an element .

raise sentence . Throw an exception .

import sentence . Import a module or package . Common writing :from module import name, import module as name, from
module import name as anothername

Special note , The above division is not very strict , Some content , Some friends don't think it belongs to sentences . It doesn't matter , It's that thing anyway , Use in programming . Not tangled in noun classification . In short, these are to be mastered , To program smoothly .

Assignment statement

example :

>>> hiekay = 1

>>> python = 2

>>> x, y = hiekay, python # amount to x=hiekay,y=python

>>> x

1

>>> y

2

>>> x, y # The output is tuple

(1, 2)

>>> [x, y] # This is one list

[1, 2]

>>> [a, b] = [hiekay, python]

>>> a

1

>>> b

2

>>> a, b

(1, 2)

>>> [a, b]

[1, 2]

Another way , The above two assignment methods are cross combined :

>>> [c, d] = hiekay, python

>>> c

1

>>> d

2

>>> c, d

(1, 2)

>>> f, g = [hiekay, python]

>>> f

1

>>> g

2

>>> f, g

(1, 2)

I can't believe it . actually , assignment , This corresponds to associating the variables on the left with the objects on the right .

There is such an interesting question , If a=3,b=4, I want to exchange the values of these two variables , that is a=4,b=3. In some high-level languages , Is to introduce another variable first c As an intermediate variable :

a = 3

b = 4

c = a # Namely c=3

a = b #a=4

b = c #b=3

python More awesome , No intermediate variables are required :

>>> hiekay = 100

>>> python = 200

>>> hiekay, python = python, hiekay

>>> hiekay

200

>>> python

100

It's amazing .

Sequence assignment

In fact, the assignment of the above experiment , It is essentially sequence assignment . If the variable on the left is a sequence , The object on the right is also a sequence , The two will be assigned one by one .

>>> [a, b, c] = (1, 2, 3) # One to one correspondence between left and right sequences , On the left is the variable , On the right is the object

>>> a

1

>>> b

2

>>> c

3

>>> (a,b,c) = [1,2,3]

>>> a

1

>>> b

2

>>> c

3

>>> [a,b,c] = "kay" # Don't forget ,str It is also data of sequence type

>>> a

"k"

>>> b

"a"

>>> c

"y"

>>> (a,b,c) = "kay"

>>> a,c

("k", "y")

>>> a,b,c = "kay" # Equivalent to the previous

>>> a,b

("k", "a")

>>> a,b = "kay" # Wrong report , Because the left and right don't correspond one by one

Traceback (most recent call last):

File "", line 1, in

ValueError: too many values to unpack

>>> (a,b),c = "hi","kay" # Pay attention to observation , How do such images correspond

>>> a,b,c

("h", "i", "kay")

>>> string = "hiekay"

>>> a,b,c = string[0],string[1],string[2] # So is slicing

>>> a,b,c

("h", "i", "e")

>>> (a,b),c = string[:2],string[2:]

>>> a,b,c

("h", "i", "ekay")

summary : Variables and values One to one correspondence .

Technology