Chapter Content
- Defined
- Variable rules
- Assigning values to variables
Defined
Variables are used to store information for subsequent program calculations. Variable information is present in memory. Variable declaration
name = "Cathy Wu"
The code above declares a variable named: Name, and the value of the variable name is: "Cathy Wu"
Variable rules
- Variable names can only be any combination of letters, numbers, or underscores.
- The first character of a variable name cannot be a number.
- The following keywords cannot be declared as variable names
[' and ', ' as ', ' assert ', ' Break ', ' class ', ' Continue ', ' Def ', ' del ', ' elif ', ' Else ', ' except ', ' exec ', ' finally ', ' for ', ' F ' Rom ', ' Global ', ' if ', ' import ', ' in ', ' was ', ' lambda ', ' not ', ' or ', ' pass ', ' print ', ' raise ', ' return ', ' try ', ' while ', ' WI Th ', ' yield ']
Attention:
2name, name of Cathywu, $name error.
_name ______name name is correct.
Cannot take an underscore-this represents a minus sign.
It is better not to use Chinese to make variable names.
There are two types of naming plans:
- Gfofoldboy Hump Naming method
Hump nomenclature: When a variable name or function name is a unique identifier consisting of one or more words, the first word starts with a lowercase letter, and the first letter of the second word is capitalized or the first letter of each word is uppercase.
- Small Hump method is usually identified by small hump method. The Hump method means that in addition to the first word, the first letter of the word is capitalized. such as int mystudentcount; Variable Mystudentcount The first word is all lowercase and the first letter of the following word is capitalized.
- The big hump method compared to the small hump method, the big hump method (that is, Pascal named) the first word of the initial letter is capitalized. Commonly used for class names, function names, properties, namespaces. For example, public class Databaseuser;
- Underline Gf_of_oldboy
Python suggests underlining.
Assigning values to variables
In Python, the equal sign = is an assignment statement, you can assign any data type to a variable, the same variable can be repeatedly assigned, and can be different types of variables, the variable itself is the type of non-fixed language called Dynamic language. A variable A is assigned to another variable B. Example of an assignment principle: a = ' abc ' b = a A = ' xyz ' Print b the last line to print out the contents of the variable B is ' abc ' or ' xyz '? If we understand mathematically, we will mistakenly conclude that B and a are the same, and should be ' XYZ ', but actually the value of B is ' abc ', let us execute the code in one line, we can see exactly what happened: execute a = ' abc ', the interpreter created the string ' ABC ' and variable A, and pointed a to ' ABC ':
Execute B = A, the interpreter creates the variable B and points B to the string ' ABC ' that points to a:
Execute a = ' xyz ', the interpreter creates the string ' xyz ' and changes the point of a to ' XYZ ', but B does not change:
So, the result of printing variable B at last is ' ABC ' naturally.
Reference pages
Http://www.cnblogs.com/alex3714/articles/5465198.html
Python Base 2 variables