python Statements in ：
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 .
>>> hiekay = 1
>>> python = 2
>>> x, y = hiekay, python # amount to x=hiekay,y=python
>>> x, y # The output is tuple
>>> [x, y] # This is one list
>>> [a, b] = [hiekay, python]
>>> a, b
>>> [a, b]
Another way , The above two assignment methods are cross combined ：
>>> [c, d] = hiekay, python
>>> c, d
>>> f, g = [hiekay, python]
>>> f, g
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
It's amazing .
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,b,c) = [1,2,3]
>>> [a,b,c] = "kay" # Don't forget ,str It is also data of sequence type
>>> (a,b,c) = "kay"
>>> a,b,c = "kay" # Equivalent to the previous
>>> 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
("h", "i", "kay")
>>> string = "hiekay"
>>> a,b,c = string,string,string # So is slicing
("h", "i", "e")
>>> (a,b),c = string[:2],string[2:]
("h", "i", "ekay")
summary : Variables and values One to one correspondence .