预计阅读时间:2 分钟
Python 基础 - 实战入门指南
Python 是最受欢迎的编程语言之一,本文带你快速入门!
目录
- 为什么学 Python?
- 开发环境
- 基础语法
- 数据类型
- 实战案例
- 学习路径
1. 为什么学 Python?
| 领域 | 应用 |
|---|---|
| Web 开发 | Django、Flask |
| 数据分析 | Pandas、NumPy |
| AI/ML | TensorFlow、PyTorch |
| 自动化 | 脚本、办公自动化 |
2. 开发环境
# 安装 Python
# Windows: https://www.python.org/downloads/
# Mac: brew install python3
# Linux: sudo apt install python3
# 创建虚拟环境
python -m venv venv
source venv/bin/activate # Linux/Mac
venv\Scripts\activate # Windows
# 安装依赖
pip install pandas numpy matplotlib
3. 基础语法
# 变量
name = "suisui"
age = 25
is_student = True
# 列表
fruits = ["apple", "banana", "orange"]
# 循环
for fruit in fruits:
print(fruit)
# 函数
def greet(name):
return f"Hello, {name}!"
# 列表推导式
squares = [x**2 for x in range(10)]
4. 数据类型
# 字符串
text = "Hello Python"
print(text.upper()) # HELLO PYTHON
# 列表
numbers = [1, 2, 3, 4, 5]
print(numbers[1:3]) # [2, 3]
# 字典
person = {"name": "suisui", "age": 25}
print(person["name"]) # suisui
5. 实战案例:批量重命名文件
import os
def batch_rename(folder, prefix="file_"):
for i, filename in enumerate(os.listdir(folder), 1):
ext = os.path.splitext(filename)[1]
new_name = f"{prefix}{i:03d}{ext}"
os.rename(os.path.join(folder, filename),
os.path.join(folder, new_name))
print(f"重命名: {filename} -> {new_name}")
# 使用
batch_rename("/path/to/files", prefix="photo_")
6. 学习路径
Python 基础
↓
函数 → 模块 → 面向对象
↓
↓
Web开发 数据分析 自动化
Django Pandas Selenium
推荐资源
标签: #Python #入门 #实战
本文由 suisui 发布