AI 生成: 一个简洁优雅的个人博客网站,包含首页文章列表、文章详情页和关于页面。使用 Python Flask 构建,支持 Markdown 文章渲染。

This commit is contained in:
admin
2026-09-28 16:47:07 +08:00
commit 53852dbbb0
11 changed files with 678 additions and 0 deletions
+48
View File
@@ -0,0 +1,48 @@
---
title: 5 个实用的 Python 小技巧
date: 2024-02-20
summary: 分享几个日常开发中非常实用的 Python 技巧,让你的代码更简洁。
---
## 1. 使用 enumerate 获取索引
```python
fruits = ['apple', 'banana', 'cherry']
for i, fruit in enumerate(fruits):
print(i, fruit)
```
## 2. 列表推导式
用一行代码完成过滤和映射:
```python
squares = [x * x for x in range(10) if x % 2 == 0]
```
## 3. 使用 zip 同时遍历
```python
names = ['Alice', 'Bob']
ages = [25, 30]
for name, age in zip(names, ages):
print(f'{name} is {age}')
```
## 4. 字典的 get 方法
避免 KeyError:
```python
config = {'debug': True}
level = config.get('level', 'INFO')
```
## 5. 使用 f-string 格式化
```python
name = 'World'
print(f'Hello, {name}!')
```
这些小技巧虽然简单,但能让代码更 Pythonic。