It's easy and easy to assign values to Python.
Variable name
In the article "always powerful functions at the beginning", there is a section dedicated to "learning from names", which is about variable names. This article is a warm-up and new principle. Here we will review it:
Name format: (underline or letter) + (any number of letters, digits or underscores)
Note:
1. Case Sensitive
2. Do not use reserved words
3. Observe common habits
• Variable names (_ X) starting with a single underline are not imported by the from module import * Statement.
• Variable names (X) with underscores (_) are system-defined variable names, which have special meanings for interpreters.
• A variable name (_ X) that starts with two underscores but does not end with two underscores is a local (Compressed) variable of the class.
• When running in interactive mode, only a single underscore variable (_) will save the final expression result.
You need to explain the reserved words, that is, some words are reserved in python. These words cannot be used as variable names. What are there? (Python2 and python3 have few differences, but they are similar in general)
Copy codeThe Code is as follows:
And assert break class continue def del elif else alias t exec finally for from global if import in is lambda not or pass print raise return try while yield
Do you need to remember? Of course not required. On the one hand, you can easily find it on the Internet, and you can also:
Copy codeThe Code is as follows: >>> not = 3
File "<stdin>", line 1
Not = 3
^
SyntaxError: invalid syntax
>>> Pass = "hello, world"
File "<stdin>", line 1
Pass = "hello, world"
^
SyntaxError: invalid syntax
In the interactive mode lab, an error is reported when reserved words are used as variables. Of course, you need to change the name at this time.
The above principles are the basic principles. In actual programming, we usually do this to make the program more readable:
• The name has a certain meaning. For example, writing: n = "qiwsir" is better than writing: name = "qiwsir.
• Do not mislead others. For example, if account_list is used to indicate a group of accounts, it will be misunderstood as list-type data. In fact, it may or may not be. So it is better to change the name at this time, for example, directly using accounts.
• The name should be distinguished in a meaningful way. Sometimes you may use names such as a1 and a2. It is best not to do so. In other ways, you can clearly distinguish them.
• It is better to read the name. never create an English word by yourself or use anything in disorder. Especially in your country, you also like to use the Chinese pinyin abbreviation as the name, which is even more troublesome, it is better to work together. It is best to use a complete word or a recognized abbreviation that does not cause ambiguity.
• A single letter and number will be used less, not only because you are too lazy, but also because there may be many single letters and numbers in a piece of code, causing trouble in searching, others do not even know whether your I is a meaning or not.
In short, it takes a lot of attention to get the name. In any case, remember a standard: Clear
Assignment Statement
For the value assignment statement, it is no stranger to the official. If you want to use any variable in python, you must assign a value first.
Statement format: variable name = Object
The essence of the assignment is also analyzed in the previous section.
There is also an implicit value assignment method, which uses import, from, del, class, for, and function parameters. Module import, function and class definition, for loop variables and function parameters are all implicitly assigned values. This will be coming soon.
Copy codeThe Code is as follows:
>>> Name = "qiwsir"
>>> Name, website = "qiwsir", "qiwsir. github. io" # assign values to multiple variables in sequence
>>> Name
'Qiwsir'
>>> Website
'Qiwsir. github. io'
>>> Name, website = "qiwsir" # When there are several variables, they correspond to several objects, either few or more.
Traceback (most recent call last ):
File "<stdin>", line 1, in <module>
ValueError: too has values to unpack
If the value is assigned in this way, the numbers on both sides must be consistent:
Copy codeThe Code is as follows:
>>> One, two, three, four = "good"
>>> One
'G'
>>> Two
'O'
>>> Three
'O'
>>> Four
'D'
This is equivalent to splitting good into one letter and assigning the variable to the left.
Copy codeThe Code is as follows:
>>> [Name, site] = ["qiwsir", "qiwsir. github. io"]
>>> Name
'Qiwsir'
>>> Site
'Qiwsir. github. io'
>>> Name, site = ("qiwsir", "qiwsir. github. io ")
>>> Name
'Qiwsir'
>>> Site
'Qiwsir. github. io'
That's fine.
In fact, there are many assignment styles. The core is to map variables with an object. Object, you can use the above method, maybe it is like this
Copy codeThe Code is as follows:
>>> Site = "qiwsir. github. io"
>>> Name, main = site. split (". ") [0], site. split (". ") [1] # Remember str. split (<sep>? Forget it. google it.
>>> Name
'Qiwsir'
>>> Main
'Github'
Enhanced assignment
The name of this object is better than the value assignment.
In python, the following method is called enhancement assignment:
| Enhanced Assignment Statement |
Equivalent to statements |
| X + = y |
X = x + y |
| X-= y |
X = x-y |
| X * = y |
X = x * y |
| X/= y |
X = x/y |
Other similar structures: x & = y x | = y x ^ = y x % = y x> = y x <= y x ** = y x // = y
Let's take a look at the example below. There is a list and we want to get another list. Each number is 2 larger than the original list. You can use the following method:
Copy codeThe Code is as follows:
>>> Number
[1, 2, 3, 4, 5]
>>> Number2 = []
>>> For I in number:
... I = I + 2
... Number2.append (I)
...
>>> Number2
[3, 4, 5, 6, 7]
If the above enhancement assignment is used, I = I + 2 can be written as I + = 2. Try it:
Copy codeThe Code is as follows:
>>> Number
[1, 2, 3, 4, 5]
>>> Number2 = []
>>> For I in number:
... I + = 2
... Number2.append (I)
...
>>> Number2
[3, 4, 5, 6, 7]
This is the enhancement assignment. Why do I use an enhanced value assignment? Because I + = 2, it is faster than I = I + 2, and the latter has to copy an I on the right.
The above example can be modified. Do not forget the powerful functions of list parsing.
Copy codeThe Code is as follows:
>>> [I + 2 for I in number]
[3, 4, 5, 6, 7]
Python assignment
Hello, because you want to use for loop through each_list, and you cannot use a value assignment statement such as number = each_list [0]. According to your requirements, there are four global variables: number, year, model, and speed:
Def number (I ):
Global number
Number = I
Def year (I ):
Global year
Year = I
Def model (I ):
Global model
Model = I
Def speed (I ):
Global speed
Speed = I
Each_list = ["100", "2012", "34A", "390"]
Dic = {0: number, 1: year, 2: model, 3: speed}
For I in each_list:
Dic [each_list.index (I)] (I)
Print number, year, model, speed
Python assignment
The Return Value of the function chooseCave () is assigned to the variable caveNumber;
The function has no return or only return, and the return value is None; otherwise, the value following return is returned;