When children are just beginning to learn to talk, often a word began to learn a word, such as the theory of "dumplings", for him/her, seems to be a little difficult, adults are also smart, so the simplification, with "dumpling dumplings" to replace, in fact, is to let children learn a word can be expressed. Of course, from the perspective of pedagogy, some people disagree with this method. This is not discussed here. If you learn to program by contrast, it's like the various types of data that you've already learned (corresponding to a single word in the natural language, word, to express a complete meaning, or let the computer complete a Thing (action), have to pass a sentence, this sentence is a statement, it is organized according to a certain rule. The words in natural language are organized according to the grammatical way of subject and predicate, and the sentences in computer programming are also organized according to certain grammatical requirements.
Although in the first part, the problem of the sentence has been dealt with sporadically, and some applications have been made on different occasions. After all, not so system. This section provides a more systematic introduction to the statements in Python.
To get a blanket impression, first look at what statements are included in Python:
Assignment statement
If statement, the statement block is run when the condition is set. Often used in conjunction with else, elif (equivalent to else if).
For statement, a list of columns, a string, a dictionary, a collection, and so on, which in turn handles each element of the iterator.
While statement that loops the statement block when the condition is true.
Try statement. With except, finally, else is working with the exception that occurs when the program is running.
Class statement. Used to define a type.
def statement. A method for defining functions and types.
Pass statement. Indicates that this line is empty and does not run any operations.
Assert statement. The test run condition is satisfied when the program is tuned to the stage.
With statement. Python2.6 the syntax to be defined later, running a block of statements in a scene. For example, a lock is added before the statement block is run, and the lock is released after the statement block runs out.
Yield statement. Used within an iterator function to return an element.
Raise statement. Throws an exception.
Import statement. Import a module or package. Common notation: From module import name, import module as name, from module import name as Anothername
Specifically, the above division is not very strict, some content, some friends do not think that belong to the statement. That's okay, anyway, that's the thing that's used in programming. Do not dwell on the classification of nouns. In short these are to master, in order to smoothly programming it.
Talk about assignment statement again
Remember to assign a value, simple or not simple that mention in the assignment statement? Since talking about the sentence, it should start from this, on the one hand review, on the other hand, hope to be able to deep, deep feeling is always good (I am talking about understanding python, thinking of Innocence.) There is a list in front of the content: a deeper point, more understanding list, there are like to see jokes reader thought evil. Ha ha. )
Copy Code code as follows:
>>> Qiwsir = 1
>>> python = 2
>>> x, y = qiwsir, python #相当于x =qiwsir,y=python
>>> x
1
>>> y
2
>>> x, y #输出的是tuple
(1, 2)
>>> [x, Y] #这就是一个list
[1, 2]
>>> [A, b] = [Qiwsir, Python]
>>> A
1
>>> b
2
>>> A, b
(1, 2)
>>> [A, b]
[1, 2]
In another way, the above two methods of assignment are combined:
Copy Code code as follows:
>>> [C, d] = Qiwsir, python
>>> C
1
>>> D
2
>>> C, D
(1, 2)
>>> F, g = [Qiwsir, Python]
>>> F
1
>>> g
2
>>> F, G
(1, 2)
It's okay. In fact, from here we can see that the assignment is to associate the left variable with the object on the right.
There is such an interesting problem, if a=3,b=4, want to change the value of these two variables, that is, a=4,b=3. In some high-level languages, it is to introduce another variable C as intermediate secondary school, is this:
Copy Code code as follows:
A = 3
b = 4
c = a #即c =3
A = b #a =4
b = C #b =3
Beginners may be a little confused. I and you two hands are holding a box, now we have two to change the box, but two hands are occupied, can not change (of course, the requirements of the box can not be landed, do not put on the table and so on). Then find a name Yue John person, he empty two hands, then I first to John, I will empty out, and then take your box, your box to my hand. My box is now in the hands of John, you pick up, so we two changed the box.
So long-winded, just because we have no more hands. But that's not the Python,python have more hands. She can do this:
Copy Code code as follows:
>>> Qiwsir = 100
>>> python = 200
>>> qiwsir, Python = python, Qiwsir
>>> Qiwsir
200
>>> python
100
It's kind of magical, Python is three heads.
Assign values to a sequence
In fact, the above experimental assignment is essentially a sequence assignment. It's just a little more fortified here. If the left variable is a sequence, the object on the right is also a sequence, and the two will be assigned to each.
Copy Code code as follows:
>>> [A, B, c] = (1, 2, 3) #左右序列一一对应, the left is the variable, 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] = "QIW" #不要忘记了, str is also a sequence type of data
>>> A
' Q '
>>> b
I
>>> C
' W '
>>> (a,b,c) = "Qiw"
>>> A,c
(' Q ', ' W ')
>>> a,b,c = ' Qiw ' #与前面等价
>>> a,b
(' Q ', ' I ')
>>> a,b = ' Qiw ' #报错了, since left and right are not one by one correspond
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Valueerror:too Many values to unpack
>>> (a,b), c = "Qi", "Wei" #注意观察, so the image is how the corresponding
>>> A,b,c
(' Q ', ' I ', ' Wei ')
>>> string = "Qiwsir"
>>> a,b,c = string[0],string[1],string[2] #取切片也一样
>>> A,b,c
(' Q ', ' I ', ' W ')
>>> (a,b), C = string[:2],string[2:]
>>> A,b,c
(' Q ', ' I ', ' Wsir ')
From the experiment, you can see that to understand this dazzling assignment, just buckle "one by one corresponding" this lifeline can be.
If reader use Python3, there are more interesting things in the assignment. However, this lecture is still python2.