python基础语法 - 变量

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.

 

声明变量:

#_*_coding:utf-8_*_
 
name = "Alex Li"

 

上述代码声明了一个变量,变量名为: name,变量name的值为:"Alex Li"

 

变量定义的规则:

  • 变量要具有描述性
  • 变量名只能是 字母、数字或下划线的任意组合
  • 变量名的第一个字符不能是数字
  • 保留字符不能声明为变量名

变量的赋值:

name = "Alex Li"
 
name2 = name
print(name,name2)
 
name = "Jack"
 
print("What is the value of name2 now?")

  输出结果:Alex Li  Jack

常量:不变的量

例:pie = 3.141592653.... 

在python里所有的变量都是可变的,所以通常用全部大写的变量名来代表此变量为常量。

 

posted @ 2018-02-07 02:36  皮蛋小顽童  阅读(92)  评论(0编辑  收藏  举报