Python 基础语法
Miniconda 是一个 Anaconda 的轻量级替代,默认只包含了 python 和 conda,但是可以通过 pip 和 conda 来安装所需要的包。Miniconda 安装包可以到以下链接下载。Python语法
基础
基础数据结构
# 案例演示python的所有数据类型
# 1. 声明很多变量,并用到各种数据类型
# 整数
integer_variable = 42
# 浮点数
float_variable = 3.14
# 字符串
string_variable = "Hello, World!"
# 布尔值
boolean_variable = False
# 列表
list_variable = [1, 2, 3, 4, 5]
# 元组
tuple_variable = (1, "two", 3.0)
# 集合
set_variable = {1, 2, 3, 4, 5}
# 字典 Map
dictionary_variable = {'name': 'John', 'age': 30}
print(f"hahah {type(set_variable)}")
# 2. 打印这些变量的类型
print(f"整数变量类型: {type(integer_variable)}")
print(f"浮点数变量类型: {type(float_variable)}")
print(f"字符串变量类型: {type(string_variable)}")
print(f"布尔值变量类型: {type(boolean_variable)}")
print(f"列表变量类型: {type(list_variable)}")
print(f"元组变量类型: {type(tuple_variable)}")
print(f"集合变量类型: {type(set_variable)}")
print(f"字典变量类型: {type(dictionary_variable)}")
# 3. 编写几个类型转换案例
# 整数转浮点数
int_to_float = float(integer_variable)
print(f"整数 {integer_variable} 转换为浮点数: {int_to_float}")
# 浮点数转整数(直接截断小数部分)
float_to_int = int(float_variable)
print(f"浮点数 {float_variable} 转换为整数: {float_to_int}")
# 字符串转整数
string_to_int = int("123")
print(f"字符串 '123' 转换为整数: {string_to_int}")
# 字符串转浮点数
string_to_float = float("3.14")
print(f"字符串 '3.14' 转换为浮点数: {string_to_float}")
# 列表转元组
list_to_tuple = tuple(list_variable)
print(f"列表 {list_variable} 转换为元组: {list_to_tuple}")
# 元组转列表
tuple_to_list = list(tuple_variable)
print(f"元组 {tuple_variable} 转换为列表: {tuple_to_list}")
# 列表转集合(自动去重)
list_to_set = set(list_variable)
print(f"列表 {list_variable} 转换为集合: {list_to_set}")
# 字典的键转列表
dict_keys_to_list = list(dictionary_variable.keys())
print(f"字典 {dictionary_variable} 的键转换为列表: {dict_keys_to_list}")
# 字典的值转列表
dict_values_to_list = list(dictionary_variable.values())
print(f"字典 {dictionary_variable} 的值转换为列表: {dict_values_to_list}")
# 布尔值转整数(True 为 1,False 为 0)
bool_to_int = int(boolean_variable)
print(f"布尔值 {boolean_variable} 转换为整数: {bool_to_int}")
print(__name__)
运算符
算术运算符
a = 10
b = 3
# 加法
add = a + b
print(f"加法: {add}")
# 减法
sub = a - b
print(f"减法: {sub}")
# 乘法
mul = a * b
print(f"乘法: {mul}")
# 除法
div = a / b
print(f"除法: {div}")
# 取余(取两个操作数相除的余数)
mod = a % b
print(f"取余: {mod}")
# 取整(取两个操作数相除的整数部分)
floor_div = a // b
print(f"取整: {floor_div}")
# 指数(a 的 b 次方)
exp = a ** b
print(f"指数: {exp}")比较运算符
a = 10
b = 3
# 等于
equal = a == b
print(f"等于: {equal}")
# 不等于
not_equal = a != b
print(f"不等于: {not_equal}")
# 大于
greater = a > b
print(f"大于: {greater}")
# 小于
less = a < b
print(f"小于: {less}")
# 大于等于
greater_equal = a >= b
print(f"大于等于: {greater_equal}")
# 小于等于
less_equal = a <= b
print(f"小于等于: {less_equal}")逻辑运算符
a = True
b = False
# 逻辑与(and):如果两个操作数都为 True,则结果为 True,否则为 False
and_result = a and b
print(f"逻辑与: {and_result}")
# 逻辑或(or):如果两个操作数中至少有一个为 True,则结果为 True,否则为 False
or_result = a or b
print(f"逻辑或: {or_result}")
# 逻辑非(not):如果操作数为 True,则结果为 False,否则为 True
not_result = not a
print(f"逻辑非: {not_result}")赋值运算符
a = 10
b = 3
# 简单赋值
a = b
print(f"简单赋值: {a}")
# 加法赋值
a += b
print(f"加法赋值: {a}")
# 减法赋值
a -= b
print(f"减法赋值: {a}")
# 乘法赋值
a *= b
print(f"乘法赋值: {a}")
# 除法赋值
a /= b
print(f"除法赋值: {a}")
# 取余赋值
a %= b
print(f"取余赋值: {a}")
# 取整赋值
a //= b
print(f"取整赋值: {a}")
# 指数赋值
a **= b
print(f"指数赋值: {a}")成员运算符
a = [1, 2, 3, 4, 5]
b = 3
# in:如果在指定的序列中找到值,返回 True,否则返回 False
in_result = b in a
print(f"in 运算符: {in_result}")
# not in:如果在指定的序列中没有找到值,返回 True,否则返回 False
not_in_result = b not in a
print(f"not in 运算符: {not_in_result}")身份运算符
a = [1, 2, 3]
b = a
# is:如果两个变量引用的是同一个对象,返回 True,否则返回 False
is_result = a is b
print(f"is 运算符: {is_result}")
# is not:如果两个变量引用的不是同一个对象,返回 True,否则返回 False
is_not_result = a is not b
print(f"is not 运算符: {is_not_result}")位运算符
a = 6 # 二进制:0110
b = 3 # 二进制:0011
# 按位与(&):对应位都是 1 时,结果为 1,否则为 0
bitwise_and = a & b
print(f"按位与: {bitwise_and}")
# 按位或(|):对应位都是 0 时,结果为 0,否则为 1
bitwise_or = a | b
print(f"按位或: {bitwise_or}")
# 按位异或(^):对应位不同时,结果为 1,否则为 0
bitwise_xor = a ^ b
print(f"按位异或: {bitwise_xor}")
# 按位取反(~):将操作数的二进制位取反
bitwise_not = ~a
print(f"按位取反: {bitwise_not}")
# 左移(<<):将操作数的二进制位向左移动指定的位数,右侧用 0 填充
bitwise_left_shift = a << 2
print(f"左移: {bitwise_left_shift}")
# 右移(>>):将操作数的二进制位向右移动指定的位数,左侧用操作数的符号位填充
bitwise_right_shift = a >> 2
print(f"右移: {bitwise_right_shift}")控制
# 案例演示:Python 的所有流程控制语句
# 1. if-elif-else 条件语句
x = 10
if x > 10:
print("x 大于 10")
elif x == 10:
print("x 等于 10")
else:
print("x 小于 10")
# 2. for 循环
fruits = ["apple", "banana", "cherry"]
for abc in fruits:
print(abc)
# 3. while 循环
count = 0
while count < 5:
print(count)
count += 1
# 4. break 语句
numbers = [1, 2, 3, 4, 5]
for num in numbers:
print(num)
if num == 3:
break
# 5. continue 语句
for num in numbers:
if num == 3:
continue
print(num)
# 6. pass 语句
for num in numbers:
if num == 3:
pass # 占位语句,不做任何操作
print(num)
# 7. try-except-else-finally 异常处理
try:
result = 10 / 0
except ZeroDivisionError:
print("除数不能为零")
else:
print("没有发生异常,结果是:", result)
finally:
print("无论是否发生异常,都会执行")
# 使用 for 循环实现循环 10 次 0~9
for i in range(10):
print(f"这是第 {i + 1} 次循环: {i}")
# 使用 while 循环实现循环 10 次
count = 0
while count < 10:
print(f"这是第 {count + 1} 次循环")
count += 1
# range() 函数在 Python 中非常灵活,除了基本的使用方式,还有以下几种常见用法:
# 1. 指定起始值和结束值
# 从 5 开始,到 10 结束(不包含 10)
for i in range(5, 10):
print(i)
# 2. 指定起始值、结束值和步长
# 从 0 开始,到 10 结束(不包含 10),步长为 2
for i in range(0, 10, 2):
print(i)
# 3. 倒序遍历
# 从 10 开始,到 0 结束(不包含 0),步长为 -1
for i in range(10, 0, -1):
print(i)
# 4. 与 len() 结合遍历列表索引
fruits = ["apple", "banana", "cherry"]
for i in range(len(fruits)):
print(f"索引 {i} 对应的水果是: {fruits[i]}")
# 5. 创建列表
numbers = list(range(5))
print(numbers) # 输出: [0, 1, 2, 3, 4]
数据结构
函数
面向对象
包管理和虚拟环境
包仓库
# 官网
https://pypi.org/
# 配置清华加速源
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple常用库

Miniconda
https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Conda加速配置
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/Conda基础命令

Conda的使用示例
# 创建隔离环境
conda create -n myapp python=3.10
# 激活环境
conda activate myapp
# 安装依赖
pip install -r requirements.txt
# 生成文件
pip freeze > requirements.txt安装jupyterlab
# 官网
https://jupyter.org/install
# 安装
pip install jupyterlab
# 启动
jupyter lab