第1章:变量
在Python中,变量就像 现实生活中装东西的盒子,可以用来存放各种数据,每个盒子都有一个名字。
# 创建两个变量(小盒子)。
a = 1;
b = 2;
这里我们建立了两个变量(小盒子) ,它们的名字分别是 a 和 b,它们分别存放数据 1 和 2。
1.1 变量的命名规则
给变量起名字就像给自己的孩子起名字一样,需要遵循一些规则:
● 允许使用的字符:字母、数字、下划线(_)。
● 不能以数字开头:1name (错误),name1 (正确)。
● 区分大小写:Age 和 age 是两个不同的变量。
● 避免使用关键字:如 if、for、while 等(编程语言保留字)。
● 好的变量名应该见名知意,让人一看就知道这个变量是干什么的。
# 正确的变量名。
student_name = "小明";
age = 18;
_total_score = 95;
# 错误的变量名。
1st_place = "张三"; # 不能以数字开头。
class = "三年级"; # 使用了关键字。
#——————————————————————————————————#
# 好的变量名。
student_name = "李华";
total_score = 650;
is_passed = True;
# 不好的变量名。
a = "李华"; # 不知道a代表什么。
ts = 650; # 缩写不明确。
ip = True; # 意义不明确。
常用的命名风格:
● 下划线命名法(snake_case) :student_name、total_score。
● 驼峰命名法(camelCase) :studentName、totalScore。
1.2 变量的数据类型
Python变量的数据类型是动态变化的,我们无需提前声明变量类型。Python会根据你给变量赋的值自动确定其类型。
# 整数类型(int)。
A = 18;
B = 100;
print(type(A));
print(type(B));
# 浮点数类型(float)。
A = 1.75;
B = 65.5;
print(type(A));
print(type(B));
# 字符串类型(str)。
A = "张三";
B = '北京大学';
print(type(A));
print(type(B));
# 布尔类型(bool)。
A = True;
B = False;
print(type(A));
print(type(B));
# 空值类型(None)。
A = None;
print(type(A));
运行结果:
<class 'int'>
<class 'int'>
<class 'float'>
<class 'float'>
<class 'str'>
<class 'str'>
<class 'bool'>
<class 'bool'>
<class 'NoneType'>
变量的值是可以改变的,就像盒子里的东西可以随时更换。上面的例子中A变量的值先是18,后面又是1.75,数据类型也随着数据的变化而变化。 type() 函数来查看变量的数据类型。
变量A和变量B的数据类型依据其赋值而改变。这在C语言里是不能实现的。
1.3 数据类型转换
有时候我们需要在不同类型之间进行转换:
# 字符串转整数。
age_str = "18"; #这里的18是字符串。
print(age_str);
print(f"age_str的类型是:{type(age_str)} ");
age_int = int(age_str);
print(age_int); # 18。
print(f"age_int的类型是:{type(age_int)} ");
# 整数转字符串。
score = 95;
print(score);
print(f"score的类型是:{type(score)} ");
score_str = str(score);
print(score_str); # "95"。
print(f"score_str的类型是:{type(score_str)} ");
# 整数转浮点数。
count = 10;
print(count);
print(f"count的类型是:{type(count)} ");
count_float = float(count);
print(count_float); # 10.0。
print(f"count_float的类型是:{type(count_float)} ");
# 浮点数转整数(会丢失小数部分)。
price = 9.99;
print(price);
print(f"price的类型是:{type(price)} ");
price_int = int(price);
print(price_int); # 9。
print(f"price_int的类型是:{type(price_int)} ");
运行结果:
18
age_str的类型是:<class 'str'>
18
age_int的类型是:<class 'int'>
95
score的类型是:<class 'int'>
95
score_str的类型是:<class 'str'>
10
count的类型是:<class 'int'>
10.0
count_floatr的类型是:<class 'float'>
9.99
price的类型是:<class 'float'>
9
price_int的类型是:<class 'int'>
1.4 变量的作用域
变量的作用域指的是变量在程序中的可见范围:
# 全局变量 - 在整个程序中都可以访问。
global_var = "我是全局变量";
def test_function():
local_var = "我是局部变量"; # 局部变量 - 只在函数内部可以访问。
print(f"函数内部访问local_var:{local_var}"); # 可以访问。
print(f"函数内部访问global_var:{global_var}"); # 也可以访问全局变量。
test_function();
print(f"函数外部访问global_var:{global_var}");
print(f"函数外部访问local_var:{local_var}"); # 这里会报错,因为local_var只在函数内部可见。
运行结果:
函数内部访问local_var:我是局部变量
函数内部访问global_var:我是全局变量
函数外部访问global_var:我是全局变量
Traceback (most recent call last):
File "C:\Users\ZhuanZ\Desktop\4+1板块\第2板块\啊.py", line 13, in <module>
print(f"函数外部访问local_var:{local_var}"); # 这里会报错,因为local_var只在函数内部可见。
NameError: name 'local_var' is not defined. Did you mean: 'global_var'?
如果需要在函数内修改全局变量,必须使用 global 关键字声明,例如 global global_var。
1.5 常量
与变量相对的是常量。常量就像 上了锁的小盒子,一旦赋值就不应该再修改:
# 常量的命名习惯:全部使用大写字母。
PI = 3.14159;
MAX_SPEED = 120;
COMPANY_NAME = "LuoXingLong";
# 虽然技术上可以修改,但按照约定不应该修改。
# PI = 3.14; # 不应该这样做!
Python 中没有真正的常量机制,全大写只是告诉程序员‘不要修改’,但技术上仍可修改”。
1.6 变量的内存管理
在Python中,我们不需要手动管理内存,Python会自动处理:
# Python会自动回收不再使用的变量。
name = "小明";
name = "小红"; # 原来的"小明"会被自动回收。
# 使用id()函数查看变量的内存地址。
a = 10;
print(id(a)); # 输出内存地址(每次运行可能不同)。
本章笔记:
● 变量是装数据的“小盒子”,用来存储各种信息。
● Python 变量的数据类型是动态的,会根据赋值自动确定。
● 变量命名规则:字母、数字、下划线,不能以数字开头;区分大小写;不能使用关键字(如 if、class) 。
● 常用命名风格:下划线命名法(snake_case) 和 驼峰命名法(camelCase) 。
● 主要数据类型:整数(int)、浮点数(float)、字符串(str)、布尔值(bool)、空值(None) 。
● 不同类型之间可以相互转换(如 int()、str()、float())。
● 变量的值可以修改;常量(全大写命名)按约定不应修改,但 Python 并不强制。
● 好的变量名应该见名知意,便于理解。
● 作用域:全局变量在整个程序中可见,局部变量仅在函数内可见;修改全局变量需使用 global 关键字。
● Python 会自动管理内存(垃圾回收),无需手动释放;可用 id() 查看变量内存地址。