变量是任何编程语言中不可或缺的一部分,在定义变量时,不同的编程语言有不同的规则和方法,以下是Python编程的变量声明、命名规则以及具体的使用方法。
声明变量
#_*_coding:utf-8_*_
name = "Alex Li"
上述代码声明了一个变量,变量名为: name,变量name的值为:"Alex Li"
变量定义的规则:
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']
变量的赋值
name = "Alex Li"
name2 = name
print(name,name2)
name = "Jack"
print("What is the value of name2 now?")