Ⅲ: Variable


First, what is a variable?
  • Variable is the amount may vary, the amount refers to the state of things, such as the person's age, gender, level of game characters, money, etc.

 

Second, why have variable?

  • In order for a computer to like people to remember things in a certain state, and the state changes can occur
  • Detailed says: is change the nature of a series of program execution state, change is directly reflected in the implementation of the program, so we need to have a mechanism that reflects or is preserved Cheng

 

Third, how to use variables ?

1, the basic use of variables

  Principles: to define, after references

  name = 'egon' # define - "deposit

  print (name) # quote - "take

 

2, memory management: garbage collection

  Garbage: When a variable value is bound variable name number is 0, the value of the variable can not be accessed, call it trash

 

  Reference count increases:

  • x = reference count of 10 10 # 1
  • y = x reference count of 10 # 2
  • z = x # 10 reference count of 3

 

  Reference count reduction:

  • unbind del x # 10 x and the value of the variable name, the reference count becomes 210
  • # Print (y)
  • del y # 1 becomes the reference count 10
  • # Print (z)
  • z = 12345 # # 10, the reference count becomes 0
  • # Print (z)

 

3, there are three major components of variable

  • Variable name = "refers to the memory address of the value of the right side of the equal sign is used to access the value of the right side of the equal sign
  • Assignment symbol: the value of the memory address of the variable binding to the variable name
  • State Representative record of things: variable values

 

4, variable names naming rules

  Principle: Named variable name should see the name EENOW

  • Variable names can be any combination of letters, numbers or underscore
  • The first character variable names can not be digital
  • Keywords can not be declared as a variable name, commonly used keywords are:

    ['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']

  • ps: Do not use pinyin, do not use Chinese, known under the name meaning see the premise as short as possible

 

5, variable names naming style

  1, pure lowercase underlined the way (in python, named on the variable name is recommended in this way)

    age_of_alex = 73

    print(age_of_alex)

  2, hump body

    AgeOfAlex = 73

    print(AgeOfAlex)

 

6, the value of the variable three important features

  • id: reflects the value of the variable memory address, the memory address is different in different id
  • type: different types of values ​​used to represent different recording state
  • value: the value itself

 

7, is with ==

  • is: compare two values ​​about identity id are equal
  • ==: compare two values ​​about their values ​​are equal
  • Different id in a case, values are may be the same , i.e., two different memory space can be kept the same value
  • id相同的情况下,值一定相同,x is y成立,x == y也必然成立

e.g.

  >>> x='info:Egon:18'

  >>> y='info:Egon:18'

  >>> print(x,y)

  info:Egon:18 info:Egon:18

  >>> print(id(x),id(y))

  4565819264 4566192176

  >>>

  >>> x == y

  True

  >>> x is y

  False

 

8、小整数池[-5,256]

  从python解释器启动那一刻开始,就会在内存中事先申请好一系列内存空间存放好常用的整数

e.g.

    >>> m=10

    >>> n=10

    >>> id(m)

    4562619328

    >>> id(n)

    4562619328

    >>> 

    >>> res=4+6

    >>> res

    10

    >>> id(res)

    4562619328

e.g.

    >>> x=-5

    >>> y=-5

    >>> x is y

    True

    >>> x=-6

    >>> y=-6

    >>> x is y

    False

e.g.

    >>> x='aaa'

    >>> y='aaa'

    >>> id(x)

    4566200880

    >>> id(y)

    4566200880

 

四、常量:不变的量

  注意:python语法中没有常量的概念,但是在程序的开发过程中会涉及到常量的概念

      AGE_OF_ALEX = 73 # 小写字母全为大写代表常量,这只是一种约定、规范

      AGE_OF_ALEX = 74

      print(AGE_OF_ALEX)

 

Guess you like

Origin www.cnblogs.com/qujiu/p/12412028.html