给名称赋值
变量:给数据起一个名字。名字不变,存的数据可以更换。Python中变量也叫name(名称)。
赋值语法
变量名 = 值
= 是赋值符号,不是数学等于号,把右边的数据交给左边的名字保存。
示例代码
firstName = "Oliver"
lastName = "King"
print(firstName) # Output: Oliver
print(lastName) # Output: King
num = 12
print(num) # Output: 12
number = "Phoenix"
print(number) # Output: Phoenix
# Key distinction: quoted text versus variable
print("number") # With quotes → prints literal word: number
print(number) # No quotes → prints value stored inside variable: Phoenix
# Variables support reassignment, overwriting previous content
number = 12
print(number) # Output: 12
Code language: PHP (php)
print("number"):双引号,打印字符串文字number。print(number):没有引号,打印变量里面存的数据。
命名:见名知意
变量名字要写得看得懂,一眼知道存的是什么。
shoes_in_hourse = 2 #家里鞋子数量
shoes_will_buy = 1 #准备要买鞋子数量Code language: PHP (php)
- 多用下划线
_连接单词,不要用空格; - 不要中文做变量名;
- 不能数字开头。
总结
变量名 = 数据完成赋值;- 变量可以反复赋值,新值覆盖旧值;
- 引号包裹是文本字符串,不加引号才是变量;
- 命名尽量见名知意,方便读代码。
Previous: 八进制、十六进制格式化
Next: Python变量