7.29 day03

variable

What is variable

All things described changes state

Compositional variables

Variable name (variable value received) = (assignment symbol) variable value (value)

Naming the variable name

weight = 160

name = 'hyc'

Variable names must consist of number / letter / underscore _ Composition

stu_name = 'hyc'

Variable names are readable (sense) deposit is not an end, is the purpose of taking

Do not arbitrarily take a variable name, such as meaningless _ like

Do not use keywords

Do not use as variable names such as print, if, as and the like

Two kinds of ways defined variable names

Underline style

(General use, the convention)

stu_name = 'hyc'

Hump ​​body

StuName = 'hyc'

Variable Memory Management

Define the variable

print (180) # print will automatically help you create a variable 180, after printing, immediately release the memory space 180

Define a variable, it opens up the memory space to store a memory variable

Reference count

height = 180 # 180 reference count plus 1

x = height # reference count plus 1

reference count by del x # 1

del height # reference count is 0

When the reference count is 0, triggering garbage collection mechanism to release the memory occupied

[-5,256] when the python interpreter starts automatically opened up, so the impact of garbage collection will not be affected

pycharm If the time interval is short, no matter what value are the same id

Three variables defined feature

Get the memory address of the variable value for the variable values

print(id(stu_name))

Get variable data types for variable values

print(type(stu_name))

Print variable values

print(stu_name)

constant

The same amount, the constant nature is also a variable

All the letters are capitalized constants

After working in all capital letters is a constant quantity, do not go changing

AGE = 1

Note

So that when the code is not to be construed in python interpreter to run, that let him senseless

Note wherein the code block in the above code, the remaining single annotation (such as variable names) on the back code

Single-line comments

Comment on the statement by the former #

Multi-line comments

With '' '' '' on the need to comment out multiple lines of statements

Fancy assignment

Cross assignment

height_of_teacher, height_of_stu = 170,170

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

Create a new variable z, x and y to be interchangeable by the assignment of z

z = x

x = y

y = z

Cross assignment

x, y = y, x

The same memory address, the same value necessarily the same value, not necessarily the same memory address

Chain assignment

a = b = c = 10

uncommonly used

Guess you like

Origin www.cnblogs.com/hyc123/p/11265015.html