Python blog finishing day03

Python day03

First, variables and constants

  • What are the variables
    describing the state of things in the world

  • Compositional variables

    1. variable name
    2. Assignment symbol
    3. variable
  • Naming variables

    1. Variable names should reflect the values ​​of the state variables as described, can not be Chinese
    2. You must be alphanumeric or underscores, and can not start with a number
    3. You can not use keywords as variable names
  • Two styles of variable names

    1. Hump ​​body
    2. Underline
  • constant

    Variable is the amount of change, constants are the same amount.

    python syntax is not used arbitrarily defined constants, that is to say, python variable is defined in the constant nature. If you have to define constants, variable names must be all uppercase.

    If it is constant, then we need to change, so it is only python developed a specification, but did not specify the syntax constants, and therefore also the constant can be modified, but not recommended.

Two, python memory management

  • Variable holds
    variable concept python interpreter is provided, and when we run the python, define variables and running, it will open up a new memory space to store the variables in memory

  • python garbage collection mechanism

    1. Reference count

      Reference count (for variable value): value of the variable reference number

      x = 10  # 10引用计数加1为1
      y = x  # 10引用计数加1为2
      x = 11  # 10引用计数减1为1;11引用计数加1为1
      del y  # 10引用计数减1为0,触发python垃圾回收机制,python清理10的内存占用
    2. Garbage collection
      time when the value of a variable reference count is 0, it will trigger a garbage collection mechanism, the magnitude of change will be recycled

    3. Small integer pool

      In order to avoid creating the same value repeated application memory space efficiency brought about, Python interpreter creates a small integer pool at startup, the range [-5,256] small integer objects are within the scope of the global scope of interpreter be reused, garbage collection will never be recovered

Third, variable

  • Numeric

    1. Plastic Plastic is mainly used to describe age, phone number, etc.

      age = 18
      
      #整形和浮点型的用法都一样
      x = 1
      y = 2
      print(x + y)
      print(x - y)
      print(x * y)
      print(x / y)
      print(x % y)  # 取余
      print(x // y) # 取整
      print(x ** y) # 幂
    2. Float

      Floating point form used to describe the main payroll, etc.

      salary = 15.3
      #整形和浮点型的用法都一样
      x = 1
      y = 2
      print(x + y)
      print(x - y)
      print(x * y)
      print(x / y)
      print(x % y)  # 取余
      print(x // y) # 取整
      print(x ** y) # 幂
  • String

    String type is mainly used to describe the name, hobbies, gender, etc.

    name = 'simple'
    hobby = 'run'
    
    
    print(name +' ' + hobby ) # 字符串不能和数字相加
    print(hobby * 10)
  • Three printing mode variable

    age = 10
    
    # 打印值
    print(age)
    # 打印内存地址
    print(id(age))
    # 打印数据类型()
    print(type(age))
  • Note

    1. Single-line comments

      #用'#'将单行代码注释,程序不执行本行代码
    2. Multi-line comments

      '''
         用三引号进行多行注释,多行注释本质上是字符串的定义方法,定义了一个变量但不使用
      '''

Guess you like

Origin www.cnblogs.com/samoo/p/11492927.html