Four, Python basis (1)

table of Contents

Four, Python basis (1)

1. What is a variable?

A variation amount, the amount of state on the world record, refers to the change of these states will change.

2. Why have variable?

Because running the computer program is a series of changes in the state.

3. Define Variables

Define a variable in Python:

name = 'magua'
age = 24
gender = 'man'
height = 168
weight = 250

4. Composition of variables

three parts:

1. Variable Name: variable name used to reference variable values, variable values ​​whenever required, variable names are required.

2. assignment symbol: Assignment

3. Variable Value: store data, recording state.

name #报错,无任何意义
age = 24
height = 168
print(age)
print(height)

24

168

The variable naming

Remember, the definition of variables in fact, recording status, keep forever is not an end, to take the ultimate goal.

1. Variable naming should reflect the state variable values ​​as described in Chinese not remember.

2. Variable names must be a combination of alphanumeric characters and underscores, and the first character of the variable name can not be a number.

3. The keyword can not be declared variable name

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

6. The variable name two styles

1. hump body

AgeOfMagua = 24
print(AgeOfMagua)

2. underscore (recommended)

age_of-magua = 24
print(age_of-magua)

7. Constant

Constant is the same amount, Python does not enforce defined constants. General constants in uppercase.

AGE = 25
print(AGE)

8. Which of the variables exist?

x = 10  

Program runs on Python interpreter, the character into the memory, which is memory and variables into existence there.

9.Python garbage collection

If we add a piece of code x = 11, Python will replace the previous swap x = 10replaced, x corresponds to the house number, re-assignment will replace the previous value, the interpreter 10 will release the memory occupied.

10. The reference count (house 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垃圾回收机制,清理10的内存占用。

11. Small integer pool

Python implementation int time there is a small integer pool. In order to avoid creating the same value is repeated application efficiency caused by the memory space, Python interpreter created when the starting pool small integer range [-5,256], small integer within the range of the object is global interpreter is reused within the range, garbage collection will never be recovered.

When you run the program in pycharm in python, pycharm for reasons of performance, will expand the scope of the pool of small integers, strings, etc. other immutable types are also included a deal will be the same way, we just need to remember this is a live optimization mechanism, as in the end how much range without careful study.

12. The three characteristic variables

(1) Print

x = 10
print(x)  #获取变量的变量值

10

(2) whether the variable values ​​are equal

name1 = 'guapi'
name2 = 'magua'
print(name1 == name2)   #False
False

(3) whether the variable is equal id

x=10
y=x
z=10
print(x==y)
ture
print(x is y)
ture        
print(x is z)
ture
x = 257
x = 257
print(x is z)
False             #整数池原因[-5,256],如果超出整数池,而且打印的时间较短,id可能会相等。

It concluded: equal id variable, the value must equal, pointing to the same memory address; value equal to the variable id is not necessarily equal.

13. Fancy assignment

(1) chain assignment (the same value as when to use)

a = 10
b = 10
c = 10
print(a,b,c)
10 10 10
a=b=c=10
print(a,b,c)
10 10 10

(2) cross-assignment

Modify x and y in the case to ensure that the same memory address

x=200
y=100
z=x
x=y
y=z
print('x:'x,'y:'y)
x:100
y:200
x=200
y=100
x,y = y,x
print('x:'x,'y:'y)
x:100
y:200

14. Notes

To facilitate future can understand when the calling code.

Note the code division multi-line and single-line comments, single-line comment with #, three multi-line comments can be single / double quotes, tris quotes

Notes can wrap

#单行注释

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

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

Notes principles:

1. do not all add a comment, or bad for an important part of understanding part can add comments.

2. Comments can either Chinese or English, but not with the alphabet.

Guess you like

Origin www.cnblogs.com/guapitomjoy/p/11264624.html