Python Basics - Variables and Constants

1. What is a variable

A variable is the amount of change

Change: the state is always changing

Quantity: to express a state

2. How to define variables

variable name=value

1) The variable name is used to find the variable value

2) Assignment symbol (=)

3) value data, used to represent a certain state

 

3. Garbage collection mechanism:

The Python interpreter periodically recycles values ​​with a reference count of zero

Clear the reference count:

  age=19 

  del age #The reference count is 0 at this time

increment reference count

   age=19

   age1=age #At this time, the reference count of 19 is 2

4. Each variable defined has three characteristics: id type value  

id : memory address

type:int  float等

value 

age=19
print(id(age),type(age),age)
497538384 <class 'int'> 19

5. == and is 

== Determines whether the values ​​are equal

is to determine whether the ids are equal

6. Variable naming convention

1) The variable name must reflect the state represented by the variable value

2) The variable name can only be any combination of letters, numbers or underscores

3) The first character of the variable name cannot be a number

4) Keywords cannot be declared as variable names

['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']

7. Definition:

hump

AgeOfLichunke=19 #One high and one low, it is not recommended to define variables in this way

underscore

age_of_lichunke=19 #This method is recommended

Bad way:

1) The variable name is Chinese

2) The variable name is too long

3) Variable nouns are unsatisfactory

8. Constant: constant quantity

Constants are not defined syntactically in Python

The convention is to define constants in all uppercase: AGE_OF_LI=19

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325739023&siteId=291194637