0729 Learn finishing

0729 Learn finishing

Python basis Summary

First, the implementation of the Python program in two ways

1.1 Interactive

Python3 input in the terminal, and then enter the code python

1.2 command line

Python3 path input text file in the terminal

Implementation modalities Interactive Command line
advantage Run an implementation of a high-efficiency Permanent preservation
Shortcoming Can not save, close disappears All finished in order to debug BUG, ​​low efficiency

Second, the implementation of the program two IDE Python

2.1 Pycharm

2.2 Jupyter

Third, variable

3.1 What is a variable

Describe the state of the real world, and this status will change, that is, the amount will change. Such as: x = 10

3.2 composition variables

  • Variable name : variable name used to refer to variable values, whenever required by the variable value goes through the variable name
  • Assignment symbol : Assignment
  • Variable value : storing data used to record a certain state in the real world

Note: The variable name is used to receive variable values

3.3 specification defined variable names

1, the variable name has some significance

2, numbers, letters, underscores, and can not start with the numbers, nor from the start with an underscore

3, can not use Python keywords

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

3.4 in two ways defined variable names

  • Hump ​​body: NameOfNick
  • Underline: name_of_nick (recommended)

3.5 Constant

Constant python essentially variables, constants convention is a specification that defines constants, then the variable name must be all uppercase. In fact, it can be modified

3.6 small integer pool range [-5,256]

In order to avoid creating the same variable values ​​and repeat application memory space efficiency brought about, python interpreter creates a small integer pool at startup, the range [-5,256], small integer objects pool is small integer Global Interpreter reused within the range, garbage collection will never be recovered. pycharm for small integer pool has optimization mechanism, will expand the scope of

Fourth, comments

4.1 Role Notes

  • Comment characters are not executed statements and syntax, that is, ordinary characters, usually used to explain a piece of code

  • Add comments principle
    1. Not all add comments, or just need to find it difficult to understand the important part to add a comment to their own
    2. Comments can either Chinese or English, but do not use Pinyin

4.2 Classification

Multiple single-line and sub-line comments, single-line comment with #, three multi-line comments can be single / double quotes, tris comments can wrap quotes

# 单行注释

'''
三单引号注释
三单引号注释
'''

"""
三双引号多行注释
三双引号多行注释
"""

4.3 reference count

As long as the value of a variable binding the house number, it is not garbage, whereas the value of the variable is not bound to the house number, the value of this variable is garbage, python will automatically clean up the garbage. Here we refer to the count for a given house number professional interpretation in python this house number is called.

The number of variable reference value

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的内存占用

The code count process is a subtraction of a reference

x = 257  # 257的引用计数为1
y = x   # 257的引用计数为2
del x  # 257的引用计数为1

4.4 garbage collection

x = 10
x = 11

As seen above, the first x = 10, has opened up a large memory space for storing a small value of the variable x 10, 10 bound a house number, but the second x = 11, the value of the variable is bound to the house number x 11 , large memory will be connected to the lifting of x 10, 11 x and make the connection. Therefore, because there is no house number 10 x, has become a garbage python's eyes, it will relieve the memory footprint of 10, this is the python garbage collection mechanism.

Fifth, the fancy assignment

5.1 Assignment chain

x=y=z=10

5.2 cross-type assignment

# 交叉赋值
x = 10
y = 20

x, y = y, x

print(x, y) # 输出结果为x=20,y=10

# 使用临时变量
x = 10 
y = 20 
temp = x
x = y
y = temp

print(x, y)

Guess you like

Origin www.cnblogs.com/zhuangyl23/p/11266437.html