python Development -- 01 variable

1, Notes

1. What is a note

Notes are notes

2. How to use notes

  • Comment on the entire file
"""
Note 1
 Note 2
 Note 3
 Make an overall description of the whole document
"""
  • Comment on key information
# [annotated information]
print("Hello word!")  # [annotated information]
#  It can be written after the beginning of the code
#  If the code is too long, try not to write it later
  • Batch annotation
#   "Ctrl +?" (comment out all rows selected with the mouse)
# print("123")
# print("123")
# print("123")

2, Variable

1. What are variables

  • Change: the state of things is changing
  • Quantity: record the state of things
  • Variables are a mechanism for accessing memory

2. Why use variables

  • The meaning of variables is to let the computer record the state of things like people

3. How to use variables

principle:

  • Define first
  • Post reference
#Error example
print(x)
x = 1

4. Three components of variables

  • Variable name: used to access the value of the variable (equivalent to a house number)
  • Assignment symbol: bind the memory address of the value to the variable name
  • Variable value: record the state of things, that is, save data

5. Variable name command rules

  • Major premise: see the name and meaning: for example, age = 18, level = 20
  • It consists of letters, numbers and underscores
  • Cannot start with a number
  • Keywords in * * * * language cannot be used

6. Naming style

  • Plain lowercase underlined
age_of_mm = 21
hight_of_mm = 173
  • Hump body
AgeOfmm = 21
HightOfmm = 174

7. Summary

# 1. Three components of variables
 Variable name: used to find value
 Assignment compliance: the memory address of the variable value is bound to the variable name
 Variable value: it is the data we store, or record the state of things

# 2. The whole variable definition process is as follows:
First, apply for memory space in memory to store variable values, and bind the memory address of the memory space to the variable name.

# 3. The premise of variable name naming: the naming of variable name should refer to the meaning of the name

# 4. Naming specification:
4.1 It consists of letters, numbers and underscores.
4.2.Cannot start with a number
4.3.Keywords cannot be used

# 5. Naming style
 Hump body: the initial letter is capitalized without underline LeveLfAge = 18
 Plain lowercase and underline (in, this style is recommended for naming variable names) level_of_age =18

# 6. The value of a variable has three characteristics
6.1 id,It reflects the unique number of variables in memory, and the memory addresses are different id It must be different
 If two variables id Same, indicating that they are located at the same memory address.
is: judge id(Are memory addresses equal
==: Determine whether the memory addresses are equal
 Note: if id Equal, then the values must be equal.

6.2,type,Type of variable value
6.3,value,Variable value

3, Use of variables

1. Define variables

age = 18
level = 10
name = "mm"

2. Reference variables

print(age)
print(name)

3. Meaning of variable name location

#  The variable name on the left represents the assignment
age = 18

#  On the left is assignment, and on the right is value
age = age + 1  # age = 18+1 = 19

4, Two characteristics of variables

  • id: the id card of the variable value, which reflects the memory address

  • Type: type of variable value

#  "id" and "type" of a variable
x = "aaa"
print(id(x))  #2282514548400
print(type(x))  #<class 'str'>

#  Like "id", it represents the same memory address, points to the same memory space, and the value must be the same
x = 10
y = 10
print(id(x))  #140724873359904
print(id(y))  #140724873359904
print(type(x))  #<class 'int'>
print(type(y))  #<class 'int'>

5, Judgment of variable value

  • Is: indicates that "id" is the same as the value, but it is actually the same value

  • "= =": indicates that the values are the same, "id" can be different, that is, different memory spaces can put the same value

x = 10
y = 10
print(x is y)  #True
print(x == y)  #True (judge whether the values are equal)

6, Small integer pool

1. In interactive mode

  • There is a small integer pool when implementing int
  • In order to avoid the efficiency problem caused by repeatedly applying for memory space by creating the same value
  • The interpreter will create a small integer pool at startup. The range is * * [- 5256]**
  • Small integer objects in this range are reused in the global interpreter range and will never be recycled by GC
#  "- 5 ~ 256" are all taken from the same pool, so the memory address is the same
>>> y=4
>>> id(y)
4297641184
>>> 
>>> x=3
>>> x+=1
>>> id(x)
4297641184

2. In pycharm

  • However, when running programs in pycharm, pycharm will expand the size of the integer pool for performance reasons
  • Other immutable types such as strings are also included, so they are handled in the same way
  • We just need to remember that this is an optimization mechanism. There is no need to study the scope

6, Constant

  • Constant quantity (such as PI: π)

  • It is usually expressed in capital letters (but it can still be changed in)

AGE_OF_HAHA = 78
level_OF_HAHA = 100

7, Garbage collection mechanism GC

  • Reference count

x = 18  #18 was quoted once and the count was 1
y = x   #18 was quoted twice and the count was 2
  • Circular reference

#  Circular reference ----- > there is a memory leak vulnerability, which needs to be cleared
  • Generation recycling

y = 20  #The reference of 18 is subtracted by 1 and the count is 1
del x   #The reference of 18 is minus 1 and the count is 0

8, Shortcut key supplement

 
ctrl + d         # copy

ctrl + x         # shear

ctrl + v         # paste

ctrl + /         # notes

shift + tab      # Back 4 spaces

tab              # Indent 4 spaces

Generation recycling

y = 20  #The reference of 18 is subtracted by 1 and the count is 1
del x   #The reference of 18 is minus 1 and the count is 0

8, Shortcut key supplement

 
ctrl + d         # copy

ctrl + x         # shear

ctrl + v         # paste

ctrl + /         # notes

shift + tab      # Back 4 spaces

tab              # Indent 4 spaces

Tags: Python Back-end

Posted on Sun, 31 Oct 2021 11:18:42 -0400 by fanfavorite