Just say a sentence to the regular saying of Python and learn a sentence from python

Source: Internet
Author: User

Just say a sentence to the regular saying of Python and learn a sentence from python

When a child is learning to speak, he or she often starts learning with one word. For example, he or she learns "dumplings". It seems difficult for him or her, and adults are also smart, so it is simplified. Instead of "dumplings", it is to let the children learn a word to express it. Of course, some people disagree with this method from the perspective of pedagogy. This is not discussed here. If you compare and learn programming, it is like the various types of data that you have learned before (corresponding to a single word or word in this natural language), to express a complete meaning, or let the computer complete a task (action) and have to use one sentence, which is a statement, which is organized according to certain rules. A sentence in a natural language is organized according to the syntax of the subject and the object. Statements in computer programming are also organized according to certain syntax requirements.

Although the first part has sporadic issues related to statements, it has also been applied in different scenarios. After all, this is not the case. This section introduces the statements in python systematically.

To make a general impression, Let's first look at the statements in python:

Assignment Statement

If statement. When the condition is true, the statement block is run. It is often used with else and elif (equivalent to else if.
For statements, including list of columns, strings, dictionaries, and collections, process each element in the iterator in sequence.
When the while statement is true, the statement block is run cyclically.
Try statement. It works with mongot, finally, and else to handle exceptions that occur during the program running.
Class statement. Defines the type.
Def statement. Defines functions and types.
Pass statement. This action is empty and does not run any operation.
Assert statement. Used to test whether the running conditions are met during the program adjustment phase.
With statement. The syntax defined after Python2.6 runs statement blocks in a scenario. For example, lock the statement block before it is run, and then release the lock after it exits.
Yield statement. Used in the iterator function to return an element.
Raise statement. Throw an exception.
Import Statement. Import a module or package. Common Syntax: from module import name, import module as name, from module import name as anothername
In particular, the above division is not very strict, some content, some friends do not think it is a statement. It doesn't matter. It's the same thing, used in programming. It is not entangled in nouns. In short, these are all to be mastered in order to program smoothly.

Assignment Statement again

Do you still remember to assign values? Isn't it simple? Is the assignment statement mentioned in that lecture simple? Since we talk about the statements, we should start from this point. On the one hand, we should review the statements. On the other hand, we hope that we can get deeper and deeper feelings. (I am talking about understanding python and thinking without evil. There is a previous article about list: more in-depth, more familiar with list, there will be a joke watching Guan Sixie. Haha .)

Copy codeThe Code is as follows:
>>> Qiwsir = 1
>>> Python = 2
>>> X, y = qiwsir, python # equivalent to x = qiwsir, y = python
>>> X
1
>>> Y
2
>>> X, y # outputs tuple
(1, 2)
>>> [X, y] # This is a list
[1, 2]

>>> [A, B] = [qiwsir, python]
>>>
1
>>> B
2
>>> A, B
(1, 2)
>>> [A, B]
[1, 2]

In another way, the above two assignment methods are combined:

Copy codeThe Code is 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 works. In fact, from this we can see that the value assignment corresponds to associating the variables on the left with the objects on the right.

There is an interesting question: if a = 3, B = 4, you want to change the values of these two variables, that is, a = 4, B = 3. In some advanced languages, we need to first introduce another variable c as an intermediate technical secondary school, which is like this:

Copy codeThe Code is as follows:
A = 3
B = 4
C = a # c = 3
A = B # a = 4
B = c # B = 3

Beginners may be confused. That is, I held a suitcase with both of you. Now we need to change the boxes, but both of them are occupied and cannot be changed (of course, the boxes must not be implemented, do not place it on a table or the like ). So I found another person named Zhang San, who had two empty hands. Then I first gave the box to Zhang San, and then I took your box, your box is in my hand. My suitcase is in Michael's hand. You took it, so we changed the boxes.

That's why we have no more hands. However, this is not python, and python has more hands. She can do this:

Copy codeThe Code is as follows:
> Qiwsir = 100
>>> Python = 200
>>> Qiwsir, python = python, qiwsir
>>> Qiwsir
200
>>> Python
100

It's amazing that python is three-headed and six-armed.

Sequence assignment

In fact, the assignment in the above experiment is essentially a sequence assignment. I just want to strengthen it here. If the variables on the left are sequences and the objects on the right are also sequences, the values are assigned one by one.

Copy codeThe Code is as follows:
>>> [A, B, c] = (1, 2, 3) # One-to-one correspondence between left and right sequences, variables on the left, and objects on the right
>>>
1
>>> B
2
>>> C
3
>>> (A, B, c) = [1, 2, 3]
>>>
1
>>> B
2
>>> C
3
>>> [A, B, c] = "qiw" # Do not forget that str is also a series data.
>>>
'Q'
>>> B
'I'
>>> C
'W'
>>> (A, B, c) = "qiw"
>>> A, c
('Q', 'w ')
>>> A, B, c = 'qiw' # is equivalent to the preceding
>>> A, B
('Q', 'I ')
>>> A, B = 'qiw' # An error is reported because the left and right sides do not correspond one by one.
Traceback (most recent call last ):
File "<stdin>", line 1, in <module>
ValueError: too has values to unpack

>>> (A, B), c = "qi", "wei" # observe how this corresponds
>>> A, B, c
('Q', 'I', 'wei ')
>>> String = "qiwsir"
>>> A, B, c = string [0], string [1], string [2] # Take slice.
>>> A, B, c
('Q', 'I', 'w ')
>>> (A, B), c = string [: 2], string [2:]
>>> A, B, c
('Q', 'I', 'wsir ')

From the experiment, we can see that to figure out this dazzling assignment, we just need to hold the lifeline of "one-to-one correspondence.

If you use python3, there are more interesting things in the assignment. However, python2 is used in this lecture.




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.