六、变量字符编码
Variables are used to store information to be referenced and manipulated in a computer program. They also provide a way of labeling data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves. It is helpful to think of variables as containers that hold information. Their sole purpose is to label and store data in memory. This data can then be used throughout your program.
声明变量
1
2
3
|
#_*_coding:utf-8_*_ name = "Alex Li" |
上述代码声明了一个变量,变量名为: name,变量name的值为:"Alex Li"
变量定义的规则:
- 变量名只能是 字母、数字或下划线的任意组合
- 变量名的第一个字符不能是数字
- 以下关键字不能声明为变量名
['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']
变量的赋值
-
- 变量是用来存数据的,在程序中创建一个变量是要存数据的,以方便后面程序调用。
- 变量值是存在变量中的,变量是存在内存中的,一旦重启后变量即会消失
-
12345678
name
=
"Alex Li"
name2
=
name
print
(name,name2)
name
=
"Jack"
print
(
"What is the value of name2 now?"
)
- name1的变量值被写到内存中,内存地址2525467954288.
- name2的变量值是找到name1当前所指向到的内存地址,即是2525467954288.一旦问到该内存地址后,name2将直接指向name1的内存地址。
- 当name1的变量值发生变化为Wangcx,此时name1的内存地址将发生变化,而name2的内存地址不会变化,还是指向George的内存地址。
- 总结:name2=name1,注意此处无“”,只是问路而已。
- Python中没有常量的概念,如果定义一个常量,通常建议全部标识符均使用大写表示,例如:PIE=3.1415926