一、Web 开发(使用 Flask 框架创建一个简单的 Web 应用)
from flask import Flaskapp = Flask(__name__)def hello_world():return 'Hello, World! This is a simple Flask web app.'if __name__ == '__main__':app.run()
二、数据科学与数据分析(使用 Pandas 读取 CSV 文件并进行简单分析)
import pandas as pddata = pd.read_csv('your_data.csv')# 查看数据的前几行print(data.head())# 计算某一列的平均值column_mean = data['column_name'].mean()print(f"The mean of the column is {column_mean}")
三、人工智能与机器学习(使用 Scikit-learn 进行简单线性回归)
from sklearn.linear_model import LinearRegressionimport numpy as np# 生成一些随机数据X = np.array([1, 2, 3, 4, 5]).reshape(-1, 1)y = np.array([2, 4, 5, 4, 6])# 创建线性回归模型model = LinearRegression()# 训练模型model.fit(X, y)# 预测新数据new_X = np.array([6]).reshape(-1, 1)prediction = model.predict(new_X)print(f"Prediction for new data: {prediction}")
四、自动化脚本(批量重命名文件)
import osfolder_path = 'your_folder_path'for filename in os.listdir(folder_path):if filename.endswith('.old_extension'):new_filename = filename.replace('.old_extension', '.new_extension')os.rename(os.path.join(folder_path, filename), os.path.join(folder_path, new_filename))
五、游戏开发(使用 Pygame 绘制一个简单的移动方块)
import pygame# 初始化 Pygamepygame.init()# 设置屏幕尺寸screen_width = 800screen_height = 600screen = pygame.display.set_mode((screen_width, screen_height))# 设置方块的初始位置和速度block_x = 400block_y = 300block_speed = 5# 游戏循环running = Truewhile running:for event in pygame.event.get():if event.type == pygame.QUIT:running = Falsekeys = pygame.key.get_pressed()if keys[pygame.K_LEFT]:block_x -= block_speedif keys[pygame.K_RIGHT]:block_x += block_speedif keys[pygame.K_UP]:block_y -= block_speedif keys[pygame.K_DOWN]:block_y += block_speed# 绘制屏幕和方块0, 0))(255, 0, 0), (block_x, block_y, 50, 50))pygame.display.flip()# 退出 Pygamepygame.quit()
以上代码仅供参考,仅适合学习使用

