20个硬核Python脚本

整理了一个覆盖面比较广泛的Python脚本示例,涉及到机器学习、数据处理、还有算法er可能会遇到自己写后台的一些案例。

另外,每个模块底部提供了对于官网文档,更加方便的查询具体的使用方法。

内容由简到难,如果对你有帮助的话希望点赞 收藏 谢谢。

1、Hello World

python 复制代码
print("Hello, World!")

官方文档: https://docs.python.org/3/

2、变量和数据类型

python 复制代码
name = "Alice"
age = 30
height = 175.5
is_student = True

官方文档: https://docs.python.org/3/tutorial/introduction.html#numbers

3、列表

python 复制代码
fruits = ["apple", "banana", "cherry"]
fruits.append("date")
print(fruits)

官方文档: https://docs.python.org/3/tutorial/introduction.html#lists

4、字典

python 复制代码
person = {"name": "Alice", "age": 30, "city": "New York"}
print(person["name"])

5、循环

python 复制代码
for i in range(1, 6):
    print(i)

官方文档: https://docs.python.org/3/tutorial/introduction.html#first-steps-towards-programming

6、条件语句

python 复制代码
x = 5
if x > 10:
    print("x is greater than 10")
else:
    print("x is not greater than 10")

官方文档: https://docs.python.org/3/tutorial/controlflow.html

7、函数

python 复制代码
def greet(name):
    return f"Hello, {name}!"

message = greet("Alice")
print(message)

官方文档: https://docs.python.org/3/tutorial/controlflow.html#defining-functions

8、模块导入

python 复制代码
import math

print(math.sqrt(16))

官方文档: https://docs.python.org/3/tutorial/modules.html

9、异常处理

python 复制代码
try:
    result = 10 / 0
except ZeroDivisionError:
    print("Division by zero is not allowed.")

官方文档: https://docs.python.org/3/tutorial/errors.html

10、文件操作

python 复制代码
with open("example.txt", "w") as file:
    file.write("Hello, File!")

with open("example.txt", "r") as file:
    content = file.read()
    print(content)

官方文档: https://docs.python.org/3/tutorial/inputoutput.html

11、日期和时间

python 复制代码
from datetime import datetime

now = datetime.now()
print(now)

官方文档: https://docs.python.org/3/library/datetime.html

12、随机数生成

python 复制代码
import random

random_number = random.randint(1, 100)
print(random_number)

13、正则表达式

python 复制代码
import re

text = "Hello, 12345"
pattern = r'\d+'
match = re.search(pattern, text)
if match:
    print(match.group())

官方文档: https://docs.python.org/3/library/re.html

14、Web请求

python 复制代码
import requests

response = requests.get("https://www.example.com")
print(response.text)

官方文档: https://docs.python-requests.org/en/master/

15、CSV文件处理

python 复制代码
import csv

with open("data.csv", "w", newline="") as file:
    writer = csv.writer(file)
    writer.writerow(["Name", "Age"])
    writer.writerow(["Alice", 25])

with open("data.csv", "r") as file:
    reader = csv.reader(file)
    for row in reader:
        print(row)

官方文档: https://docs.python.org/3/library/csv.html

16、JSON处理

python 复制代码
import json

data = {"name": "Bob", "age": 35}
json_data = json.dumps(data)
print(json_data)

官方文档: https://docs.python.org/3/library/json.html

17、爬虫 - BeautifulSoup

python 复制代码
from bs4 import BeautifulSoup
import requests

url = "https://www.example.com"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
print(soup.title.text)

官方文档: https://www.crummy.com/software/BeautifulSoup/bs4/doc/

18、多线程

python 复制代码
import threading

def print_numbers():
    for i in range(1, 6):
        print(f"Number: {i}")

def print_letters():
    for letter in "abcde":
        print(f"Letter: {letter}")

thread1 = threading.Thread(target=print_numbers)
thread2 = threading.Thread(target=print_letters)

thread1.start()
thread2.start()

官方文档: https://docs.python.org/3/library/threading.html

19、数据爬取 - Selenium

python 复制代码
from selenium import webdriver

driver = webdriver.Chrome()
driver.get("https://www.example.com")

官方文档: https://www.selenium.dev/documentation/en/

20、REST API - Flask

python 复制代码
from flask import Flask, jsonify

app = Flask(__name)

@app.route('/api', methods=['GET'])
def get_data():
    data = {'message': 'Hello, API!'}
    return jsonify(data)

if __name__ == '__main__':
    app.run()

官方文档: https://flask.palletsprojects.com/en/2.1.x/

还整理了30个更难一点的脚本实例,后续会发布出来。敬请期待

相关推荐
aloha_7897 分钟前
软考高项-第二章-信息技术发展
java·人工智能·python·学习
寒秋花开曾相惜11 分钟前
(学习笔记)3.8 指针运算(3.8.5 变长数组)
java·c语言·开发语言·笔记·学习
南境十里·墨染春水12 分钟前
C++笔记 构造函数 析构函数 及二者关系(面向对象)
开发语言·c++·笔记·ecmascript
Dxy123931021620 分钟前
Python如何删除文件到回收站
开发语言·python
AI-Ming23 分钟前
程序员转行学习 AI 大模型: 踩坑记录,HuggingFace镜像设置未生效
人工智能·pytorch·python·gpt·深度学习·学习·agi
阿_旭32 分钟前
基于YOLO26深度学习的【电力巡检异常检测与语音提示系统】【python源码+Pyqt5界面+数据集+训练代码】
人工智能·python·深度学习·电力巡检异常检测
斌味代码33 分钟前
RAG 实战:用 LangChain + DeepSeek 搭建企业私有知识库问答系统
开发语言·langchain·c#
zero159736 分钟前
Python 8天极速入门笔记(大模型工程师专用):开篇-学习大纲和学习准备
python·学习笔记·大模型编程语言
剑穗挂着新流苏31238 分钟前
205_深度学习的非线性魔法:多层感知机(MLP)与激活函数全解析
人工智能·pytorch·python·深度学习
懷淰メ1 小时前
python3GUI---基于PyQt5+YOLOv8+DeepSort的智慧行车可视化系统(详细介绍)
开发语言·yolo·计算机视觉·pyqt·yolov8·deepsort·车距