01 Python based learning

Note: The environment uses python-3.5.2

 

1 python basis Introduction

1.1 python Profile

Comparison 1.1.1 python's

coding:

2.x = = ASSIC = default encoding is not supported by Chinese

3.x = default encoding = UNICODE = default support Chinese

 

python3 and python2 difference:

python3:

1, default support for Chinese

2, is not compatible with 2.x

3, core syntax adjustment, easier to learn

4, the new features by default only on 3

 

After the installation after the python interpreter, after the win + R cmd_DOS enter the environment, input python, this time into the python interactive mode ( where the code can be run directly write ), as shown below:

 

The first test program: Print ( "the Hello world!")

 

Note: programs written in memory of all here, if you turn off this program interface will disappear

 

The above program writes a file and then in the DOS execution (permanent)

The implementation of a way:

 

Perform way:

 

Summary: Perform py way for the program

1 , the interaction, a disadvantage is that the program can not be saved permanently, mainly used in connection with simple grammar test

2 , file execution

 

1.2 Detailed variables

Example: script test variables code01.py

x = 2

y = 3

z = x * y

print("x*y=", z)

operation result:

 

1.2.1 Role of variables

Some variables to store intermediate results of a program calculation process, in order to facilitate the call back procedure, named variables need to see to know the name of Italy

 

1.2.2 variable naming

Specifications variable naming: the number of students named variables

Method 1: Use "_" underscore ( the official recommended ) , for example: student_number

Second way: The second word after the first letter of the word in all caps ( hump body ) , for example: studentNumber

Variable naming rules summary:

1, be descriptive

2, variable names can only _, numbers, characters, and must not be spaces or special characters (#? <., $ *! ~)

3, Chinese can not be a variable name

4, can not start with a number

5, reserved characters can not be used (program keywords: If the print ...)

 

Constant : Value does not change the amount of the operation program (eg: Mathematics pi ...)

NOTE: In no python constants, all variables are variable, but in order to distinguish between human constant and variable name in all caps will be constant (this constant but is variable, while the value can be changed)

 

1.2.3 reassigned variables

Test 01 :

name = "debain"

name2 = name

print(name,name2)

 

Graphical analysis:

 

 

Analysis: name to address their data in memory assigned to the variable name2, name2 thereafter points to name data area in memory

 

Then test 01 tests continue the re-assignment

name = "frdora"

print(name,name2)

 

图示分析:

 

分析总结:此时name被重新赋予变量的值,指向内存的另外一个区域,因此name的值发生改变

 

Python内存如何回收?

python中内存是自动回收的不需要写回收机制,当变量和变量的值的指向关系断裂后,在内存中变量之前指向的数据会被自动清空掉

 

清除变量和值的关系的2种方式:

(1)、使用 del 强硬的删除变量(直接删除变量名)

>>> age = 10

>>> print(age)

10

>>>

>>> del age

>>> print(age)

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

NameError: name 'age' is not defined

 

(2)、将当前的变量指向其它的区域

>>> num = 11

>>> id(num)

497419024

>>> print(num)

11

>>>

>>> num = 33

>>> id(num)

497419728

>>> print(num)

33

>>>

Guess you like

Origin www.cnblogs.com/fanrui/p/11111869.html