分享10個有趣又實用的Python模組,看看他們的功能吧!

青灯夜游
發布: 2022-07-14 21:09:36
轉載
5111 人瀏覽過

Python程式有許多有助於高效程式設計的模組和第三方包,了解這些模組的正確使用方法是很重要的,以下這篇文章就來給大家總結分享10個有趣且實用的Python模組,一起看看他們的功能吧!

分享10個有趣又實用的Python模組,看看他們的功能吧!

閒話少說,我們直接開始吧。 :)

1.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())
登入後複製

輸出如下:

分享10個有趣又實用的Python模組,看看他們的功能吧!

2.手寫文字圖片

實作程式將給定文字轉換為手寫筆記形式

為了完成上述功能,需要第三方程式包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''')
登入後複製

輸出如下:

分享10個有趣又實用的Python模組,看看他們的功能吧!
# 輸出以圖像檔案形式保存在目前python檔案目錄下。

3.實作電腦關機

實作一個程式來將電腦自動關機

實作上述功能需要用到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')
登入後複製

注意事項如下:

在執行此程式之前,請確保已儲存並關閉所有文件。執行此程式會導致電腦關閉後,未儲存的資料可能會遺失。

4.列印行事曆

實作列印指定月份和年份行事曆的程式

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))
登入後複製

運行結果如下:

分享10個有趣又實用的Python模組,看看他們的功能吧!

5.畫一個餅圖

實作在餅圖中用百分比表示每月費用的程式

在下述程式中,我們使用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()
登入後複製

運行結果如下:
分享10個有趣又實用的Python模組,看看他們的功能吧!

#6.彈出警告方塊

##實作程式來顯示帶有訊息的警告框效果

下述程式使用第三方函式庫

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}')
登入後複製

輸出如下:

分享10個有趣又實用的Python模組,看看他們的功能吧!

#7.文字轉語音

##實作程式來將文字轉換為語音

為了實現將文字轉換為音頻,需要使用
pip install pyttsx3

來安裝轉換庫。這個函式庫有很多模組,我們還可以嘗試改變音訊的聲音、音量和速度。 範例程式碼如下:

import pyttsx3
engine = pyttsx3.init()
engine.say('This is a python example in MEDIUM')
engine.runAndWait()
登入後複製

上述程式碼運作後,輸出是一個女性聲音,將對應的文字轉換為音訊播放。

8.截圖

實作一個python程序,來實現截取螢幕截圖的功能

如下程式碼所示,我們使用python函式庫
pyautogui

來實作截圖功能。程式碼如下:<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">import pyautogui screenshot = pyautogui.screenshot() screenshot.save(&quot;screenshot.png&quot;)</pre><div class="contentsignin">登入後複製</div></div>上述程式碼運行後,輸出檔案截圖保存在 python 原始檔目錄下。我們可以嘗試使用

time.sleep()

語法來延遲螢幕截圖。 9.網路監控

實作一個python程序,用來偵測網路上傳和下載速度

在下述程序中,為了監控網路速度,我們使用了
speedtest

函式庫,要安裝此第三方函式庫,可以使用pip install speedtest-cli 語法進行安裝。 程式碼如下:

import speedtest
speed = speedtest.Speedtest()
download_speed = speed.download()
upload_speed = speed.upload()
print(f&#39;The download speed is {download_speed}&#39;)
print(f&#39;The uplaod speed is {upload_speed}&#39;)
登入後複製

執行結果如下:

#

10.用 Python 绘制图形

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

在下述程序中,我们使用 Python中的Turtle 绘制了一个螺旋图。要安装该库,可以使用pip install PythonTurtlePython Turtle主要用于绘制视觉图形,以及图形的形状颜色设置。
样例代码如下:

import random
import turtle
colors = [&#39;red&#39;,&#39;cyan&#39;,&#39;pink&#39; ,&#39;yellow&#39;, &#39;green&#39;,&#39;orange&#39;]
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")
登入後複製

运行结果如下:

分享10個有趣又實用的Python模組,看看他們的功能吧!

总结

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

【相关推荐:Python3视频教程

以上是分享10個有趣又實用的Python模組,看看他們的功能吧!的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:csdn.net
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!