变量
一句话解释
变量 是 named 引用 到 值 存储 in memory, 创建 使用 = assignment 运算符.
用大白话说
变量 in Python 是 name 那个点 到值. 你创建 one 使用 assignment 运算符 =: age = 25 创建变量 named age that holds the value 25.
Python variables are dynamically typed — the same variable can hold different types at different times. You do not declare the type; Python infers it from the value. Variable names must start with a letter or underscore and cannot be keywords.
Variables are more like labels than boxes. The variable name is a label attached to a value object in memory. When you reassign, the label points to a new object.
示例
name = "Alice"
age = 30
pi = 3.14159
print(name, age, pi)
# Reassignment changes the reference
age = "thirty"
print(age) # "thirty"变量 是 创建 使用 = and 能 是 reassigned 到 任何 类型.
常见混淆
容易混淆的对象: 常量
关键区别: 变量 能 是 reassigned. 常量 (通过 约定, ALL_CAPS names) 应该 不 是 更改, 虽然 Python 做 不 enforce 这个.
相关术语
相关资源
相关课程
- 变量
学习 Python 变量.
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。