【强推】10个有趣的Python程序

1. 引言

Python程序有许多模块和第三方包,这非常有助于高效编程。了解这些模块的正确使用方法是很重要的,在本文中,主要介绍一些非常实用的一些Python常见的模块。
闲话少说,我们直接开始吧。 :)

2.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())   

输出如下:
在这里插入图片描述

3.手写文本图像

实现一个程序将给定文本转换为手写笔记形式

为了完成上述功能,需要第三方程序包pywhatkit,可以使用pip install pywhatkit进行安装。这个软件包有很多其他功能,比如在谷歌上搜索等。

样例代码如下:

import pywhatkit
pywhatkit.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 some of the oops concepts.Python program has many modulesand packages, which helps with efficient programming.
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文件目录下。

4.实现电脑关机

实现一个程序来将电脑自动关机

实现上述功能需要用到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')  

注意事项如下:

执行此程序之前,请确保保存并关闭所有文档。运行此程序会导致计算机关闭后,未保存的数据可能会丢失。

5.打印日历

实现打印指定月份和年份日历的程序

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))  

运行结果如下:
在这里插入图片描述

6.画一个饼图

实现在饼图中用百分比表示每月费用的程序

在下述程序中,我们使用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()

运行结果如下:
在这里插入图片描述

7.弹出告警框

实现一个程序来显示带有消息的告警框效果

下述程序使用第三方库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}')  

输出如下:
在这里插入图片描述

8.文本转语音

实现一个程序来将文本转化为语音

为了实现将文本转化为音频,需要使用pip install pyttsx3 来安装一个转换库。这个库有很多模块,我们还可以尝试改变音频的声音、音量和速度。
样例代码如下:

import pyttsx3
engine = pyttsx3.init()
engine.say('This is a python example in MEDIUM')
engine.runAndWait()                                                             

上述代码运行后,输出是一个女性声音,将对应的文字转化为音频播放。

9.截图

实现一个python程序,来实现截取屏幕截图的功能

如下代码所示,我们使用python库pyautogui 来实现截屏功能。代码如下:

import pyautogui
screenshot = pyautogui.screenshot()
screenshot.save("screenshot.png")                                                 

上述代码运行后,输出文件截图保存在 python 源文件目录下。我们可以尝试使用 time.sleep() 语法来延迟屏幕截图。

10.网络监测

实现一个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}') 

运行结果如下:
在这里插入图片描述

11.用 Python 绘制图形

实现一个程序,使用turtle 制作螺旋图形

在下述程序中,我们使用 Python中的Turtle 绘制了一个螺旋图。要安装该库,可以使用pip install PythonTurtlePython 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")                                                                    

运行结果如下:

在这里插入图片描述

12.总结

本文重点汇总了使用Python中的第三方库来实现常见场景下的一些简单有趣的功能,并给出了样例程序和相应的解释。

您学废了吗?

在这里插入图片描述
关注公众号《AI算法之道》,获取更多AI算法资讯。

参考

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>