变量与常量

变量的作用

  昵称,代指内存里某个地址中保存的内容存储程序的中间结果,方便后面的程序调用

  官方介绍 :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.

声明 变量

  变量名 + 赋值符号 + 变量的值

  eg : name = alex

变量定义的规则

  1. 变量名只能是 字母、数字或下划线的任意组合
  2. 变量名的第一个字符不能是数字
  3. 以下关键字不能声明为变量名
  4. ['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']
  5. 官方推荐用下话线的方式来命名如:age_of_you = 23

变量的赋值
  name1 = "anna"  name2 = "wuan"
  变量name1,变量name2分别指向一块内存地址
  name1 = anna
  name2 = name1
  变量name1,name2指向同一内存地址,修改name1的值,那么的内容不变

不使用中间变量交换两个变量的值:
b, a = a, b
常量

  常量即指不变的量,如pai 3.141592653..., 或在程序运行过程中不会改变的量

  在Python中没有一个专门的语法代表常量,程序员约定俗成用变量名全部大写代表常量

原文地址:https://www.cnblogs.com/leiyiming/p/12880239.html