20个硬核Python脚本

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

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

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

1、Hello World

print("Hello, World!")

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

2、变量和数据类型

name = "Alice"
age = 30
height = 175.5
is_student = True

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

3、列表

fruits = ["apple", "banana", "cherry"]
fruits.append("date")
print(fruits)

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

4、字典

person = {"name": "Alice", "age": 30, "city": "New York"}
print(person["name"])

5、循环

for i in range(1, 6):print(i)

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

6、条件语句

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、函数

def greet(name):return f"Hello, {name}!"message = greet("Alice")
print(message)

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

8、模块导入

import mathprint(math.sqrt(16))

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

9、异常处理

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

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

10、文件操作

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、日期和时间

from datetime import datetimenow = datetime.now()
print(now)

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

12、随机数生成

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

13、正则表达式

import retext = "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请求

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

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

15、CSV文件处理

import csvwith 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处理

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

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

17、爬虫 - BeautifulSoup

from bs4 import BeautifulSoup
import requestsurl = "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、多线程

import threadingdef 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

from selenium import webdriverdriver = webdriver.Chrome()
driver.get("https://www.example.com")

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

20、REST API - Flask

from flask import Flask, jsonifyapp = 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个更难一点的脚本实例,后续会发布出来。敬请期待

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/8951.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

运行时数据区-基础

运行时数据区-基础 为什么学习运行时数据区Java内存区域(运行时数据区域)和内存模型(JMM) 区别组成部分(jdk1.7 / jdk1.8)从线程隔离性分类与类加载的关系每个区域的功能参考文章 为什么学习运行时数据区 …

在安卓应用中实现Socket通信:创建服务端和客户端

介绍:本文主要介绍在安卓中使用Socket创建服务端和客户端进行通信,服务端可以管理多个客户端连接,完善的异常处理,接口回调,可以满足大部分需求,更多功能自行拓展… 关于Socket套接字: 是网络上…

linux开发笔记(F1C200S)折腾weston桌面

参考文章: 1、嵌入式桌面(1)——weston桌面_qt weston-CSDN博客 2、https://blog.51cto.com/u_16213414/9171009 3、weston.ini: configuration file for Weston — the reference Wayland compositor | weston File Formats | Man Pages …

文献速递:深度学习医学影像心脏疾病检测与诊断--基于深度学习的PET图像重建与运动估计

Title 题目 Deep Learning Based Joint PET Image Reconstruction and Motion Estimation 基于深度学习的PET图像重建与运动估计 01 文献速递介绍 正电子发射断层扫描(PET)成像是一种非侵入性成像技术,通过使用放射性示踪剂在活体内可视化…

架构师:搭建Spring Security、OAuth2和JWT 的安全认证框架

1、简述 Spring Security 是 Spring 生态系统中的一个强大的安全框架,用于实现身份验证和授权。结合 OAuth2 和 JWT 技术,可以构建一个安全可靠的认证体系,本文将介绍如何在 Spring Boot 中配置并使用这三种技术实现安全认证,并分析它们的优点。 2、Spring Security Spri…

营销H5测试综述

H5页面是营销域最常见的一种运营形式,业务通过H5来提供服务,可以满足用户对于便捷、高效和低成本的需求。H5页面是业务直面用户的端点,其质量保证工作显得尤为重要。各业务的功能实现具有通用性,相应也有共性的测试方法&#xff0…

基于C++函数基础中的形参与实参

在C中,函数的形参(形式参数)是在函数定义时声明的参数,而实参(实际参数)是在函数调用时传递给函数的值或变量。 形参的作用是定义函数在执行时所需要的输入,它们在函数体内被当做局部变量使用。…

[1726]java试飞任务规划管理系统Myeclipse开发mysql数据库web结构java编程计算机网页项目

一、源码特点 java试飞任务规划管理系统是一套完善的java web信息管理系统,对理解JSP java编程开发语言有帮助,系统具有完整的源代码和数据库,系统主要采用B/S模式开发。开发环境为 TOMCAT7.0,Myeclipse8.5开发,数据库为Mysql…

VS code放大缩小

​ 放大 ctrl和一起按 缩小 ctrl和-一起按 上面是键盘组合方式,如果需要Ctrl滚轮实现代码的缩放,可以这样。 在文件-->首选项-->设置-->用户设置: 在搜索栏输入mouseWheelZoom 选中即可。 ​ 输入 mouseWheelZoom 进行搜素 特…

linux 使用intel oneapi报错报错

使用intel oneapi 2024.1.0 时经常报这个错误 因为当前 intel2024.1.0没有在使用 需要改回2024.0.0并安装适配的torch的包来运行

Linux系统执行apt update报错 暂时不能解析域名“xxxx.xxx.com”

一、错误重现 错误:1 http://mirrors.aliyun.com/ubuntu jammy InRelease 暂时不能解析域名“mirrors.aliyun.com” 错误:2 http://mirrors.aliyun.com/ubuntu jammy-updates InRelease 暂时不能解析域名“mirrors.aliyun.com” 错误:3 http://mirrors.aliyun.com/ubuntu j…

1688工厂货源API接口:用于商品采集、商品搜索、商品详情数据抓取

item_get 获得1688商品详情item_search 按关键字搜索商品item_search_img 按图搜索1688商品(拍立淘)item_search_suggest 获得搜索词推荐item_fee 获得商品快递费用seller_info 获得店铺详情item_search_shop 获得店铺的所有商品item_password 获得淘口令…

机器学习:基于TF-IDF算法、决策树,使用NLTK库对亚马逊美食评论进行情绪分析

前言 系列专栏:机器学习:高级应用与实践【项目实战100】【2024】✨︎ 在本专栏中不仅包含一些适合初学者的最新机器学习项目,每个项目都处理一组不同的问题,包括监督和无监督学习、分类、回归和聚类,而且涉及创建深度学…

外贸人必备的9类工具合集-36个提高效率神器推荐!

做外贸,你需要的不只是一些基本的知识,还得有多种技能傍身,比如找新客户、写有效的邮件、分析竞争对手,还有在社交媒体上做营销但你可能发现了,有的人做外贸轻轻松松就能收到很多询盘,而有的人忙得团团转却…

python实现txt文件内容对比功能

欢迎关注我👆,收藏下次不迷路┗|`O′|┛ 嗷~~ 目录 一.前言 二.代码 三.演示 四.代码分析 一.前言 内容对比是一种常见的信息分析和研究方法,主要涉及对不同来源、类型或版本的内容进行比

HTTP请求三方接口绕过https证书检查

问题:在http请求https接口过程中经常会遇到SSL证书检查或者证书过期 ** sun.security.validator.ValidatorException: PKIX path validation failed: java.security.cert.CertPathValidatorException: validity check failed ** 解决办法:绕过证书检查…

[NSSRound#1 Basic]sql_by_sql

[NSSRound#1 Basic]sql_by_sql 这题没啥难的&#xff0c;二次注入盲注的套题 先注册&#xff0c;进去有个修改密码 可能是二次注入 修改密码处源码 <!-- update user set password%s where username%s; -->重新注册一个admin-- 获得admin身份&#xff08;原理看sqli-l…

分享10个高质量宝藏网站~

分享一波高质量宝藏网站~ 这10个宝藏网站&#xff0c;个个都好用到爆&#xff0c;娱乐、办公、学习都能在这里找到&#xff01; 1、Z-Library https://zh.zlibrary-be.se/ 世界最大的免费电子书下载网站&#xff01;电子书资源超千万&#xff0c;不过这个网站不太稳定&#…

MongoDB Atlas Vector Search与Amazon Bedrock集成已全面可用

亮点前瞻 ●MongoDB Atlas Vector Search知识库与Amazon Bedrock的最新集成&#xff0c;将极大加速生成式AI应用的开发。 ●诺和诺德利用MongoDB Atlas Vector Search与Amazon Bedrock集成&#xff0c;加速构建AI应用程序。 MongoDB&#xff08;纳斯达克股票代码&#xff1a…

springboot 整合 knife4j-openapi3

适用于&#xff1a;项目已使用shiro安全认证框架&#xff0c;整合knife4j-openapi3 1.引入依赖 <!-- knife4j-openapi3 --> <dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-openapi3-spring-boot-starter</artifa…