The Python learning process will use a lot of data, that for the convenience of operation, it is necessary to use a simple name to represent the data, easy to reference in the next program.
A variable is a name that represents a data (value). Simply put, a variable is a name for the data.
Name of variable names:
Consists of alphanumeric underscores and cannot start with a number, and cannot be used with a keyword, which is case-sensitive.
Naming conventions:
Variable names (_x) that begin with a single underscore are not imported by the From module import * statement
The variable name (X), preceded and underlined, is a system-defined variable name that has special meaning for the interpreter.
Variable names (__x) that begin with a double underscore but have no double underscore at the end are local variables of the class
- When run through interactive mode, only a single underlined variable name (_) Saves the result of the last expression
Summary of Python naming conventions:
Module Name: Lowercase letters, words separated by _, such as ad_stats.py
Package Name: Same as module name
Class Name: Capitalize the first letter of the word, such as Adstats Configutil
Global variable Name: uppercase letters, words separated by _, such as Umber Color_write
Common variables: Lowercase letters, words separated by _, such as This_is_a_var
Instance variables: Start with _, other than normal variables, such as _price _instance_var
Private instance variable (external access error): Starts with __ (2 underscore), and the other is the same as the normal variable
__private_var
Proprietary variables: start, end, usually Python's own variable, not named in this way
Doc _class
Variable assignment:
Is the process of declaring and defining variables
Single Variable assignment:
#!/usr/bin/python
-
-Coding:utf-8--
counter = 100 # Assignment Integer variable
Miles = 1000.0 # floating-point
Name = "John" # string
Print counter
Print miles
Print Name
Assign values to multiple variables:
Python allows you to assign values to multiple variables at the same time.
For example:
A = b = c = 1
The above example creates an integer object with a value of 1 and three variables allocated to the same memory space.
Assign different values to multiple variables at the same time.
For example:
A, b, C = 1, 2, "John"
For the above example, two integer objects 1 and 2 are assigned to variables A and B, and the string object "John" is assigned to variable C.
The way variables store data:
General programming language variables store data in a way that:
A variable is an area of computer memory in which variables can store values within a specified range, and the values are variable.
Creating a variable creates a space in memory. Based on the data type of the variable, the interpreter allocates the specified memory and determines what data can be stored in memory. Therefore, variables can specify different data types, which can store integers, decimals, or characters.
For example, C language after declaring a variable a, it will open up in memory of the corresponding space, in this space can store different values, that is, to give the variable a different value
Python variables are stored in memory in a way that is different from other programming languages:
In Python, the variable name has no type, but the object has
Variable name is just a reference to an object (internally implemented as a pointer)
Python is dominated by data, variable A is just equivalent to a memory space of the label, A=1 Open a space to store 1, then re-copy a=2 is to re-create a new space storage 2, variable name a changed position to point to the new space 2
The same address space can have two or more tags, such as a=1,b=1 is actually a and B point to the same address space
To view the address of a variable pointing to an address space: Using the ID (variable name) function
>> a=1
>> ID (a)
19882304
>> b=1
>> ID (b)
19882304
The above example found that the same value was assigned to different variables, the actual address space did not change, but the label has changed
Cloud computing Python automation: A python variable explanation