站長資訊網
        最全最豐富的資訊網站

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

        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 來實現截屏功能。代碼如下:

        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模塊,看看他們的功能吧!

        10.用 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")

        運行結果如下:

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

        總結

        本文重點匯總了使用Python中的第三方庫來實現常見場景下的一些簡單有趣的功能,并給出了樣例程序和相應的解釋。

        贊(0)
        分享到: 更多 (0)
        網站地圖   滬ICP備18035694號-2    滬公網安備31011702889846號
        主站蜘蛛池模板: 亚洲福利精品电影在线观看| 亚洲精品国产高清嫩草影院| 欧美精品黑人粗大视频| 98精品国产自产在线XXXX| 亚洲精品成人a在线观看| 日韩精品国产自在久久现线拍| 亚洲AV第一页国产精品| 久久国产精品久久久| 亚洲国产精品视频| 国产精品五月天强力打造| 欧洲精品99毛片免费高清观看| 亚洲线精品一区二区三区影音先锋| 无码精品久久久久久人妻中字| 996久久国产精品线观看| 中文精品久久久久人妻不卡| 久久国产成人亚洲精品影院| 国产在线精品一区二区高清不卡 | 久久亚洲国产欧洲精品一| 热re99久久精品国99热| 亚洲午夜精品一级在线播放放| 国产女人精品视频国产灰线| 综合人妻久久一区二区精品| 蜜桃麻豆www久久国产精品 | 国产一区二区精品尤物| 四虎4hu永久免费国产精品| 国产成人精品日本亚洲专一区| 国产内地精品毛片视频| 国产VA免费精品高清在线| 久久精品国产福利国产琪琪| 亚洲精品白浆高清久久久久久| 国产精品亚洲精品观看不卡| 久久国产乱子精品免费女| 精品无码国产一区二区三区AV| 国产伦精品一区二区免费| 桃花岛精品亚洲国产成人| 久久国产精品久久国产精品| 久久久国产精品福利免费 | 久久久久久久亚洲精品| 国产免费久久精品99久久| 国产午夜精品一本在线观看| 国产精品美女WWW爽爽爽视频|