Python程序有有許多有助于高效編程的模塊和第三方包,了解這些模塊的正確使用方法是很重要的,下面本篇文章就來給大家總結(jié)分享10個有趣且實用的Python模塊,一起看看他們的功能吧!
閑話少說,我們直接開始吧。 :)
1.Python偽信息生成器
創(chuàng)建一個程序,生成虛假數(shù)據(jù),如姓名、電子郵件或包含個人所有信息的詳細(xì)虛假個人資料。
Faker是一個python軟件包,可以在終端中使用pip install Faker
安裝。每次運行以下程序faker generator時,都將產(chǎn)生不同的隨機數(shù)據(jù)。
from faker import Faker fake = Faker() print(fake.name()) print(fake.email()) print(fake.country()) print(fake.profile())
輸出如下:
2.手寫文本圖像
實現(xiàn)一個程序?qū)⒔o定文本轉(zhuǎn)換為手寫筆記形式
為了完成上述功能,需要第三方程序包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''')
輸出如下:
輸出以圖像文件形式保存在當(dāng)前python文件目錄下。
3.實現(xiàn)電腦關(guān)機
實現(xiàn)一個程序來將電腦自動關(guān)機
實現(xiàn)上述功能需要用到OS庫,可以使用pip install os
進行安裝。我們可以使用該庫來實現(xiàn)關(guān)閉,重啟,或者設(shè)置關(guān)閉重啟倒計時等功能。
樣例代碼如下:
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')
注意事項如下:
執(zhí)行此程序之前,請確保保存并關(guān)閉所有文檔。運行此程序會導(dǎo)致計算機關(guān)閉后,未保存的數(shù)據(jù)可能會丟失。
4.打印日歷
實現(xiàn)打印指定月份和年份日歷的程序
Python中有一個內(nèi)置模塊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))
運行結(jié)果如下:
5.畫一個餅圖
實現(xiàn)在餅圖中用百分比表示每月費用的程序
在下述程序中,我們使用matplotlib來畫餅圖??梢允褂?code>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()
運行結(jié)果如下:
6.彈出告警框
實現(xiàn)一個程序來顯示帶有消息的告警框效果
下述程序使用第三方庫pyautogui
來顯示告警框。通常來說,可以使用pip install pyautogui
來安裝它。這個模塊有很多方法,比如使用python程序來控制鼠標(biāo)和鍵盤。
樣例代碼如下:
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}')
輸出如下:
7.文本轉(zhuǎn)語音
實現(xiàn)一個程序來將文本轉(zhuǎn)化為語音
為了實現(xiàn)將文本轉(zhuǎn)化為音頻,需要使用pip install pyttsx3
來安裝一個轉(zhuǎn)換庫。這個庫有很多模塊,我們還可以嘗試改變音頻的聲音、音量和速度。
樣例代碼如下:
import pyttsx3 engine = pyttsx3.init() engine.say('This is a python example in MEDIUM') engine.runAndWait()
上述代碼運行后,輸出是一個女性聲音,將對應(yīng)的文字轉(zhuǎn)化為音頻播放。
8.截圖
實現(xiàn)一個python程序,來實現(xiàn)截取屏幕截圖的功能
如下代碼所示,我們使用python庫pyautogui
來實現(xiàn)截屏功能。代碼如下:
import pyautogui screenshot = pyautogui.screenshot() screenshot.save("screenshot.png")
上述代碼運行后,輸出文件截圖保存在 python 源文件目錄下。我們可以嘗試使用 time.sleep()
語法來延遲屏幕截圖。
9.網(wǎng)絡(luò)監(jiān)測
實現(xiàn)一個python程序,用來檢測互聯(lián)網(wǎng)上傳和下載速度
在下述程序中,為了監(jiān)測互聯(lián)網(wǎng)速度,我們使用了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}')
運行結(jié)果如下:
10.用 Python 繪制圖形
實現(xiàn)一個程序,使用turtle 制作螺旋圖形
在下述程序中,我們使用 Python中的Turtle
繪制了一個螺旋圖。要安裝該庫,可以使用pip install PythonTurtle
。 Python Turtle
主要用于繪制視覺圖形,以及圖形的形狀顏色設(shè)置。
樣例代碼如下:
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")
運行結(jié)果如下:
總結(jié)
本文重點匯總了使用Python中的第三方庫來實現(xiàn)常見場景下的一些簡單有趣的功能,并給出了樣例程序和相應(yīng)的解釋。
【