Say a word formally with Lao qi who is learning Python

  • 2020-04-02 14:13:25
  • OfStack

When a child is just beginning to learn how to speak, he or she often starts learning word by word. For example, learning how to say "jiaozi" seems a little difficult for him or her. Of course, from a pedagogical perspective, there are those who disapprove of this approach. This is not discussed here. If compared to learn programming, as it has already been studied the various types of data (corresponding to a single word, word) in the natural language, to express the meaning of a complete, or let the computer to complete a thing (action), has to pass a word, this sentence is a statement, it is organized according to certain rules. A sentence in natural language is organized in a subject-verb-object way, and a sentence in computer programming is also organized in accordance with certain grammatical requirements.

In the first part, though, statement problems have been dealt with sporadically, and some applications have been made in different situations. It's not that systematic. In this section, I'll give you a more systematic introduction to statements in python.

To give you an overview, take a look at which statements are included in python:

Assignment statement

If statement, which runs the statement block when the condition is true. Often used in conjunction with else, elif.
A for statement that iterates through lists, strings, dictionaries, collections, and so on, processing each element in the iterator in turn.
While statement. When the condition is true, the statement block is looped.
The try statement. Work with except, finally, or else to handle exceptions that occur while the program is running.
The class statement. Used to define a type.
Def statement. Methods used to define functions and types.
Pass the statement. Indicates that this behavior is empty and that no operation is running.
The assert statement. Used to test whether the running conditions are satisfied during the program adjustment phase.
With statement. Syntax defined after Python2.6 to run a block of statements in a scenario. For example, a statement block is locked before it is run, and then the lock is released after the statement block exits.
Yield statements. Used within an iterator function to return an element.
Raise statement. Throws an exception.
The import statement. Import a module or package. 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 do not think belong to the statement. It doesn't matter, it's just that thing, it's used in programming. Don't worry about categorizing nouns. In short, these are to master, in order to successfully program it.

Let's talk about assignment statements

Remember the assignment statement that we talked about in the lecture, which was simple and not simple? Since the statement, should start from this, on the one hand review, on the other hand, I hope to be able to be deep, deep feeling is always good (I mean understanding python, thinking innocent. In front of a list of content: further, more understanding of the list, there is like to see the joke of the viewer thought evil. Ha ha.


>>> qiwsir = 1
>>> python = 2
>>> x, y = qiwsir, python   # The equivalent of x=qiwsir,y=python
>>> x
1
>>> y
2
>>> x, y                    # The output is tuple
(1, 2)
>>> [x, y]                  # So that's one 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 valuation methods are intercombined:


>>> [c, d] = qiwsir, python
>>> c
1
>>> d
2
>>> c, d
(1, 2)
>>> f, g = [qiwsir, python]
>>> f
1
>>> g
2
>>> f, g
(1, 2)

Even that. In fact, we can see from here that assignment corresponds to associating the variable on the left with the object on the right.

So here's an interesting question, if a is equal to 3 and b is equal to 4, and I want to switch the values of these two variables, so a is equal to 4 and b is equal to 3. In some high-level languages, it is necessary to introduce another variable c as the intermediate technical specialist, that is:


a = 3
b = 4
c = a   # namely c=3
a = b   #a=4
b = c   #b=3

Beginners may be a little confused. You and I are holding a box in both hands. Now we are going to change the box, but both hands are occupied and cannot be changed. Then look for a person named zhang SAN to come, he has two empty hands, so I first give the box to zhang SAN, I am free, and then take your box, your box to my hand. My box is now in zhang SAN's hand, you take it, so we two changed the box.

Just so wordy, just because we two have no more hands. But, this is not python, and python has more hands. She could do this:


>>> qiwsir = 100
>>> python = 200
>>> qiwsir, python = python, qiwsir
>>> qiwsir
200
>>> python
100

It's kind of amazing. Python has three heads and six arms.

Sequence assignment

In fact, the assignment of the above experiment is essentially a sequential assignment. It's just a little bit stronger here. If the variable on the left is a sequence and the object on the right is also a sequence, they are assigned one-to-one.


>>> [a, b, c] = (1, 2, 3)   # Left and right sequences correspond one to one, with variables on the left and objects on the right
>>> a
1
>>> b
2
>>> c
3
>>> (a,b,c) = [1,2,3]
>>> a
1
>>> b
2
>>> c
3
>>> [a,b,c] = "qiw"     # Don't forget, str Also sequence type data
>>> a
'q'
>>> b
'i'
>>> c
'w'
>>> (a,b,c) = "qiw"
>>> a,c
('q', 'w')
>>> a,b,c = 'qiw'       # The same thing as before
>>> a,b
('q', 'i')
>>> a,b = 'qiw'         # Wrong, because the left and right don't correspond one to one
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: too many values to unpack >>> (a,b),c = "qi","wei"    # Notice, how does this image correspond
>>> a,b,c
('q', 'i', 'wei')
>>> string = "qiwsir"
>>> a,b,c = string[0],string[1],string[2]   # Same thing with slices
>>> a,b,c
('q', 'i', 'w')
>>> (a,b),c = string[:2],string[2:]
>>> a,b,c
('q', 'i', 'wsir')

Experiments have shown that the only way to make sense of such dazzling assignments is to hold on to the lifeblood of one-to-one correspondence.

If you use python3, there's a lot more interesting stuff in the assignment. However, python2 is still used in this lecture.


Related articles: