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())
輸出如下:
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''')
輸出如下:
輸出以圖像文件形式保存在當前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))
運行結果如下:
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()
運行結果如下:
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}')
輸出如下:
7.文本轉語音
實現一個程序來將文本轉化為語音
為了實現將文本轉化為音頻,需要使用pip install pyttsx3
來安裝一個轉換庫。這個庫有很多模塊,我們還可以嘗試改變音頻的聲音、音量和速度。
樣例代碼如下:
import pyttsx3 engine = pyttsx3.init() engine.say('This is a python example in MEDIUM') engine.runAndWait()
上述代碼運行后,輸出是一個女性聲音,將對應的文字轉化為音頻播放。
8.截圖
實現一個python程序,來實現截取屏幕截圖的功能
如下代碼所示,我們使用python庫pyautogui
來實現截屏功能。代碼如下:
import pyautogui screenshot = pyautogui.screenshot() screenshot.save("screenshot.png")
上述代碼運行后,輸出文件截圖保存在 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'The download speed is {download_speed}') print(f'The uplaod speed is {upload_speed}')
運行結果如下:
10.用 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中的第三方庫來實現常見場景下的一些簡單有趣的功能,并給出了樣例程序和相應的解釋。
【