1.2 变量
变量,其实就是我们生活中起别名和外号,让变量名指向某个值,格式为:【变量名 = 值】,以后可以通过变量名来操作其他对应的值。
name = "baiye" print(name) # baiye
age = 18 name = "baiye" flag = 1 > 18 address = "深圳南山" + "高新园" addr = "深圳南山" + "高新园" + name # "深圳南山高新园baiye" print(addr) print(flag)
age = 18 number = 1 == 2
注意:
- 给变量赋值 age=18
- 让age代指值 age = 18
变量名的规范
三个规范:
- 变量名只能由 字母、数字、下划线 组成。
- 不能以数字开头
- 不能是Python内置的关键字
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is',
'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
练习题
name = "史蒂夫" name0 = "sd" name_1 = "邓肯" _coach = "波波" _ = "库里" 1_year = "1999" #错误 year_1 = "1990" _1_year = "1990" nba-team = "勇士" #错误 new*name = "水花兄弟" #错误
两个建议:
- 下划线连接命名(小写)
my_name = "佩奇" brother_age = 29
- 见名知意
age = 19 color = "red"
current_user_name = "吕德华"