Python's variable declaration

Source: Internet
Author: User

Python, like most other languages, has local variables and global variables, but it does not have a distinct variable declaration. The variable is generated by first assignment and automatically dies out when it goes out of scope.

Example 1. Defining Myparams Variables

if __name__ = = "__main__": Myparams = {"Server": "Mpilgrim", "Database": "Master", "UID": "sa", "pwd": "Secret"}

  

First notice the indentation. The If statement is a block of code that needs to be indented like a function.
Second, the assignment of a variable is a command that is divided into multiple lines, with a backslash ("") as the continuation character.
Shanghai Python Training and editing, reproduced the source of the explanation!

1. Write multiple lines of command
When a command is split into multiple rows with a continuation character (""), subsequent lines can be indented in any way, and the usual strict indentation rules for Python do not need to be followed. If your Python IDE is free to indent subsequent lines, you should treat it as a default unless you have specific reasons not to do so.

Strictly speaking, expressions in parentheses, square brackets, or curly braces (such as defining a dictionary) can be split into multiple lines with or without a continuation character (""). I like to use the continuation character even when it's not necessary, because I think it makes the code easier to read, but it's just a matter of style.

You have never declared a variable myparams, you just assigned a value to it. This is like VBScript does not have the Option Explicit options set. Fortunately, unlike VBScript, Python does not allow you to reference a variable that is not assigned, and attempting to do so throws an exception.

2. Variable reference
Example2. Referencing a variable that is not assigned a value

if __name__ = = "__main__": Myparams = {"Server": "Mpilgrim", "Database": "Master", "UID": "sa", "pwd": "Secret"}

  

Sooner or later, you'll thank Python for that.

3, one-time assignment multi-value
A programming shorthand for comparing "cool" in Python is to use sequences to assign values to multiple variables at once.

Example 3. Assign multiple values at once

>>> v = (' A ', ' B ', ' E ') >>> (x, y, z) = V (1) >>> x ' A ' >>> y ' b ' >>> z ' e '

  

(1) v is a tuple of ternary, and (x, Y, z) is a tuple of three variables. Assigning a tuple to another tuple assigns each value of V to each variable in order.

There are many uses for this kind of usage. I often want to assign a certain range of values to multiple variables. In the C language, you can use the enum type to manually list each constant and its corresponding value, which is especially tedious when the values are continuous. In Python, you can use the built-in range function and multivariate assignment methods to quickly assign values.

Example 4. Continuous value Assignment

>>> v = (' A ', ' B ', ' E ') >>> (x, y, z) = V (1) >>> x ' A ' >>> y ' b ' >>> z ' e '

  

(1) The built-in range function returns a list of elements as integers. The simplified invocation of this function is to receive an upper value, and then return a list of the initial value starting at 0, which is incremented until the upper value is not included. (If you prefer, you can pass in other parameters to specify a non-0 initial value and a non-1 step.) You can also use print range.__doc__ to learn more about the finer
Section. )

(2) MONDAY, Tuesday, Wednesday, Thursday, FRIDAY, SATURDAY and SUNDAY are variables we define. (This example comes from the Calendar module.) It is a very interesting printed calendar of small modules, like the UNIX cal command. This calendar module defines the integer constant representation of the day of the week. )

(3) Each variable now has its own value: The value of Monday is 0, the value of Tuesday is 1, and so on.

You can also use a multivariate assignment to create a function that returns multiple values, as long as a tuple containing all the values is returned. The caller can treat it as a tuple, or assign a value to a separate variable. Many of the standard Python libraries do this, including OS modules.

Thanks for reading, welcome comments, more content or timely support please visit the Shanghai Python Training

Python's variable declaration

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.