python 工具

Python 变量


1 变量的定义与使用

# 语法: 变量名 = 常量/变量名
# 定义变量时必须初始化
int1 = 25
int2 = int1

# 同时为多个变量赋值
a = b = c = 1

# 为多个对象指定多个变量 (用逗号,分隔)
a, b, c = 1, 2, "orange"

2 标识符命名规则 (标识符是用来命名变量、函数、类、模块等对象的名称)

  • 标识符的其他的部分由字母、数字和下划线组成。(在 Python 3 中,可以用中文作为变量名,非 ASCII 标识符也是允许的了)
  • 第一个字符必须是字母表中字母或下划线 _ 。
  • 标识符对大小写敏感。
  • 不能使用python关键字。
  • 不能包含空格或特殊字符(如 @, #, $, % 等)。
# Python 关键字
import keyword
print(keyword.kwlist)

# ['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']