Python程序有许多模块和第三方包,这非常有助于高效编程。了解这些模块的正确使用方法是很重要的,在本文中,主要介绍一些非常实用的一些Python常见的模块。
包含编程资料、学习路线图、源代码、软件安装包等!【[点击这里]】!
Python伪信息生成器
创建一个程序,生成虚假数据,如姓名、电子邮件或包含个人所有信息的详细虚假个人资料。
- Faker是一个python软件包,可以在终端中使用**pip install Faker**进行安装。每次运行以下程序faker generator时,都将产生不同的随机数据。
from faker
import Faker
fake = Faker()
print(fake.name())
print(fake.email())
print(fake.country())
print(fake.profile())
- 上述代码输出如下:
手写文本图像
实现一个程序将给定文本转换为手写笔记形式
- 为了完成上述功能,需要第三方程序包pywhatkit,可以使用pip install pywhatkit进行安装。这个软件包有很多其他功能,比如在谷歌上搜索等。
- 样例代码如下:
import pywhatkitpywhatkit.text_to_handwriting
('''Learning Python from the basics is extremely important.Before starting to learn python,understanding a base language like c is a must and someof the oops concepts.Python program has many modulesand packages, which helps with efficientprogramming.Understanding these modules and 1proper usage of many syntax and libraries is recommended.In this article, a few modules and packages are used in the program. Python includes tons of libraries and some of them are quiet intresting''')
- 输出如下:
- 输出以图像文件形式保存在当前python文件目录下。
实现电脑关机
实现一个程序来将电脑自动关机
- 实现上述功能需要用到OS库,可以使用pip install os 进行安装。我们可以使用该库来实现关闭,重启,或者设置关闭重启倒计时等功能。
- 样例代码如下:
import os
shutdown = input("Do you want to shutdown your computer (yes / no): ")
if shutdown == 'yes':
os.system("shutdown /s /t 1")
else:
print('Shutdown is not requested')
- 注意事项如下:
执行此程序之前,请确保保存并关闭所有文档。运行此程序会导致计算机关闭后,未保存的数据可能会丢失。
打印日历
实现打印指定月份和年份日历的程序
- Python中有一个内置模块calendar,它可以帮助访问日历。在这个模块中有很多方法,在下述程序中,我们试图打印一年中指定月份的日历。
import calendar
year =int( input("Enter the year of the required calendar "))
month = int( input("Enter the month of the required calendar "))
print(calendar.month(year,month))
- 输出如下:
画一个饼图
实现在饼图中用百分比表示每月费用的程序
- 在下述程序中,我们使用matplotlib来画饼图。可以使用pip install matplotlib安装此库。有了这个模块,可以用python编写许多交互式视觉效果。
- 样例代码如下:
import matplotlib.pyplot as plt
Partition = 'Holidays', 'Eating_Out', 'Shopping', 'Groceries'
sizes = [250, 100, 300, 200]
fig1, ax1 = plt.subplots()
ax1.pie(sizes, labels=Partition, autopct='%1.1f%%', shadow=True, startangle=90)
ax1.axis('equal')
plt.show()
- 运行结果如下:
弹出警告框
实现一个程序来显示带有消息的告警框效果
- 下述程序使用第三方库pyautogui来显示告警框。通常来说,可以使用pip install pyautogui来安装它。这个模块有很多方法,比如使用python程序来控制鼠标和键盘。
- 样例代码如下:
import pyautogui
num=int(input("Enter a value to divide 100"))
if num == 0:
pyautogui.alert(" Alert!!! 100 cannot be divided by 0")
else:
print(f'The value is {100/num}')
- 输出如下:
文本转语音
实现一个程序来将文本转化为语音
- 为了实现将文本转化为音频,需要使用pip install pyttsx3 来安装一个转换库。这个库有很多模块,我们还可以尝试改变音频的声音、音量和速度。
- 样例代码如下:
import pyttsx3
engine = pyttsx3.init()
engine.say('This is a python example in MEDIUM')
engine.runAndWait()
- 上述代码运行后,输出是一个女性声音,将对应的文字转化为音频播放。
截图
实现一个python程序,来实现截取屏幕截图的功能
- 如下代码所示,我们使用python库pyautogui 来实现截屏功能。代码如下:
import pyautogui
screenshot = pyautogui.screenshot()
screenshot.save("screenshot.png")
- 上述代码运行后,输出文件截图保存在 python 源文件目录下。我们可以尝试使用 time.sleep()语法来延迟屏幕截图。
网速监测
实现一个python程序,用来检测互联网上传和下载速度
在下述程序中,为了监测互联网速度,我们使用了_speedtest_ 库,要安装此第三方库,可以使用 pip install speedtest-cli * 语法进行安装。
- 代码如下:
import speedtest
speed = speedtest.Speedtest()
download_speed = speed.download()
upload_speed = speed.upload()
print(f'The download speed is {download_speed}')
print(f'The uplaod speed is {upload_speed}')
- 运行结果如下:
用 Python 绘制图形
实现一个程序,使用turtle 制作螺旋图形
- 在下述程序中,我们使用 Python中的Turtle 绘制了一个螺旋图。要安装该库,可以使用pip install PythonTurtle。Python Turtle主要用于绘制视觉图形,以及图形的形状颜色设置。
- 样例代码如下:
import random
import turtle
colors = ['red','cyan','pink' ,'yellow', 'green','orange']
t = turtle.Turtle()
t.speed(10)
turtle.bgcolor("black")
length=100
angle =50
size=5
for i in range(length):
color=random.choice(colors)
t.pencolor(color)
t.fillcolor(color)
t.penup()
t.forward(i+50)
t.pendown()
t.left(angle)
t.begin_fill()
t.circle(size)
t.end_fill()
turtle.exitonclick()
turtle.bgcolor("black")
- 运行结果如下:
总结
- 最后希望你编程学习上不急不躁,按照计划有条不紊推进,把任何一件事做到极致,都是不容易的,加油,努力!相信自己!
文末福利
- 最后这里免费分享给大家一份Python全套学习资料,希望能帮到那些不满现状,想提升自己却又没有方向的朋友,也可以和我一起来学习交流呀。
包含编程资料、学习路线图、源代码、软件安装包等!【[点击这里]】领取!
- ① Python所有方向的学习路线图,清楚各个方向要学什么东西
- ② 100多节Python课程视频,涵盖必备基础、爬虫和数据分析
- ③ 100多个Python实战案例,学习不再是只会理论
- ④ 华为出品独家Python漫画教程,手机也能学习