变量

1.变量定义:

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.


2.变量定义的规则:

       1)变量名只能是字母,数字,下划线的任意组合

       2)变量名的第一个字符不能是数字

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

 

  (变量命名时,约定俗成的规则:(1)尽量用英文来命名变量名(2)对于需要用到词组的时候,单词之间最佳的链接方法是用下划线)

  注:Python里没有常量,若要表示常量,就是将变量名全部大写,看到变量名是大写的时候尽量不要对其进行更改。

 

3.理解变量赋值

1 name="Wang"
2 name2=name
3 print("My name is",name2)
4 name="Whgvjp"
5 print(name,name2)

  这个时候print name2,name2并不会变成Whgvjp,原因是name2并非指向name指向的值,而是一开始name和name2都指向的Wang,为了加深理解,可以理解为name2是通过问路的形式,直接获取name指向的值,然后直接指向该值。

 

posted @ 2017-09-19 19:37  鱼龙夜落星斗南  阅读(130)  评论(0编辑  收藏  举报