Basic grammar - variable

A variable

#### 1.1 What is a variable

Variables: describes the status change things in the world

  • Capacity: recording status in the real world, so that the computer can like people to identify things in the world.

Today I probably 180, next year I was not probably a 185 (5cm but points), and that this state is not change.

  • Change: the state of the real world will change happen.
# 身高:160,体重:140,
height = 160
print(height)  # 160
print(160)

1.2 define the variable

Variable composition (rules):

Variable name (variable value received) = (assignment symbol) variable value (value) is defined variable #

身高 = 180  # 不建议这样做,但是可以这么做(英文差的同学,前期可以这样做)
print(身高)

weight = 160

name = 'randysun'

1.3 naming convention

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

  1. Variable names are readable (sense) deposit is not an end, is the purpose of taking
  2. Do not use (the character has some special meaning) Keyword
  3. Both definitions way a variable name
# 1. 变量名必须由数字/字母/下划线_组成
height_of_randysun = 180  # 下划线只在连接的时候用

# 2. 变量名都具有可读性(意义) 存不是目的,取才是目的
_ = 180
print(_)

# 3.不要使用关键字(具有某种特殊意义的字符)

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

# 变量名的两种定义方式

height_of_randysun = 180  # 下划线式(使用,约定俗成)
print(height_of_randysun)

HeightOfRandysun = 180  # 驼峰体,low,c/java
print(HeightOfRandysun)

Second, the constant

Constant: The 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.

age = 18  # 变量

age = age + 1
print(age)

AGE = 18  # 所有字母全大写(常量)
print(AGE)

Third, the variable memory management

Variables: describe the dynamic of things in the world

Whenever the program memory to run the definition of variables to open a memory to store a variable, the reference count will pass (for the value of the variable), height = 180 [ , X = height , whenever a reference count variable is increased by a reference height, using del X , deletes the reference count == x == (for minus one) == x == thereby releasing memory space, del height, to completely remove the variables in memory occupied by the memory, the application time count is zero, it will starting garbage collection mechanism to release the memory occupied , [-5,256] small integer pool , when the python interpreter starts automatically opened up, so that he will not be affected garbage collection

height = 180  # 定义变量
print(180) # print会自动帮你创建一个变量180,打印完之后,马上释放180的内存空间
# 定义一个变量,内存就开辟一个内存空间存储这个变量
# 引用计数(针对变量值)
# 180的引用计数加1

x = height  # 180的引用计数加1

del x   # 释放的是引用计数,180的引用计数为1

del height # 引用计数为0,

# 引用计数为0时,触发垃圾回收机制,释放内存占用

# [-5,256](小整数池)在python解释器启动的时候就自动开辟了,所以说他不会受垃圾回收机制的影响,适用于jupyter
# [-5,256]用的比较多

# pycharm中做了优化,间隔时间短的,无论多大,id都一样
a = 257
b = 257
print(id(a))  # 打印内存地址
print(id(b))  # 打印内存地址

a = 23423423423423423423
b = 23423423423423423423
print(id(a))
print(id(b))

Four, three feature variable

  1. Get the memory address of the variable value for the variable values
  2. Data types for variable values
# 获取变量值的内存地址,针对变量值
print(id(height))  # 1908245568

# 数据类型,针对变量值
print(type(height))  # int类型(integer 整型)

name = 'randy'
print(type(name))  # str类型(string 字符串)


# 打印变量值
print(name)

Fifth, the fancy assignment

  1. Cross assignment (decompression)
  2. Modify x and y in the case to ensure that the same memory address
  3. Cross assignment
# 交叉赋值(解压缩)

height_of_randy,height_of_handsome,heigh_of_laowang=180,160,170
print(height_of_randy)
print(height_of_handsome)
print(heigh_of_laowang)


# 保证内存地址不变的情况下修改x和y
x = 2000
print('x:',id(x))
y = 1000
print('y:',id(y))


# 中间变量的思想
# z = x
# x = y
# y = z


# 交叉赋值
x,y = y,x

print('y:',id(y))  # 内存地址相同,值一定相同
print('x:',id(x))


# x = 1000
# y = 2000
# print('y:',id(y))  # 值相同,内存地址不一定相同
# print('x:',id(x))


# 链式赋值(值相同才能使用)

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

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

This Commentary

  • Notes divided into single-line and multi-line comments
# 单行注释

# 解释  ;  让代码在python解释器运行的时候不被解释,即让他无意义

# a = 10
# print(a)


picasso_height = 160  # 毕加索的身高,解释代码的意思


# 代码块的解释放到代码上面


# 打印0-9
for i in range(10):
    print(i)

# ctrl+/


# 多行注释
'''
a = 10
print(a)
'''

# 不问目的的抄,编程在你敲代码的时候突然顿悟,永远都不会在你思考的时候顿悟

Seven execute Python program in two ways

Interactive (jupyter)

Pros: Run an implementation of a

Disadvantages: Close disappear

Command line (pycharm)

Pros: has been saved

Disadvantages: all finished before they can debug bug

Although txt file can run, but try to use py file

Guess you like

Origin www.cnblogs.com/randysun/p/11264074.html