3、变量

变量的作用
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. 以下关键字不能声明为变量名

定义方式

驼峰体
AgeOfOldboy = 56
NumberOfStudents = 80
 
下划线(官方推荐)
age_of_oldboy = 56
number_of_students = 80
 

定义变量不好的方式举例

  • 变量名为中文、拼音
  • 变量名过长
  • 变量名词不达意

 

常量

常量即指不变的量,如pai 3.141592653..., 或在程序运行过程中不会改变的量
举例,假如老男孩老师的年龄会变,那这就是个变量,但在一些情况下,他的年龄不会变了,那就是常量。在Python中没有一个专门的语法代表常量,程序员约定俗成用变量名全部大写代表常量
AGE_OF_OLDBOY = 56
在c语言中有专门的常量定义语法,const int count = 60;一旦定义为常量,更改即会报错

猜你喜欢

转载自www.cnblogs.com/vettel2018/p/9956999.html