python变量

作者:【吴业亮】云计算开发工程师
博客:http://blog.csdn.net/wylfengyujiancheng
一、变量:是计算机的内存中的一块区域,变量可以存储规定范围ude值,而且值可以改变
二、命名规则:
1、变量可以有字母、数字、下划线组成
2、数字不能开头,不能带”-“
3、不可以使用关键字,如python
正确举例:

a 
a1 
a_ 
a_1
_a 

错误举例

>>> 1a=1233
  File "<stdin>", line 1
    1a=1233
     ^
SyntaxError: invalid syntax
>>> a-a=2
  File "<stdin>", line 1
SyntaxError: can't assign to operator

三、变量的赋值
是变量的声明和定义过程

>>> c+a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'c' is not defined

1、重新赋值,以最后一次为准

>>> a=100
>>> a=1222
>>> a
1222

2、变量值发生变化,内存地址也发生变化

>>> id(a)
31739976
>>> a=100
>>> id(a)
30847664

3、同一个地址空间可对应多个变量,如:

>>> a=123
>>> b=123
>>> id(a)
30849104
>>> id(b)
30849104

4、变量可以运算

>>> a=1111
>>> b=2222
>>> a+b
3333

5、变量尽量定义易懂的关键字

你可能感兴趣的:(python)