搜尋
首頁後端開發Python教學python 中的預定義模組

Predefined Modules in python

  1. 建立一個名為 Bank 的 Python 模組。
  2. 新增功能: 存款(金額) 提款(金額)
  3. 再建立一個名為 Customer 的 Python 模組
  4. 從客戶模組呼叫銀行模組的存取款功能。
#bank.py
def deposit(amount):
    print("Enter the deposit amount:",amount)
def withdraw(amount):
    print("Enter the withdraw amount:",amount)

建立python模組檔案名稱為bank.py
這兩個函數是
Deposit(amount):此函數接受一個參數 amount 並列印指示存款金額的訊息。
withdraw(amount):此函數也接受一個參數amount,並列印指示提款金額的訊息。

#customer.py
import bank
bank.deposit(1000)
bank.withdraw(500)

建立另一個Python模組文件,文件名稱為customer.py

使用此導入關鍵字我們可以導入銀行模組。所以我們可以從 customer.py
存取 Deposit() 和 Deposit() 函數

Enter the deposit amount: 1000
Enter the withdraw amount: 500


Python 預設模組:

1.隨機:

隨機模組可讓您產生隨機數、打亂資料以及從序列中選擇隨機元素。

import random
otp = random.randint(100000,999999)
print(otp)

random.randint(a, b) 傳回 a 和 b(含)之間的隨機整數。

624367

2.數學:

數學模組提供基本數學運算和常數的函數。

import math
print(math.fabs(-5))

math.fabs(x):傳回x的絕對值。

5.0

3.os:

提供與作業系統互動的功能(例如檔案處理、目錄)。

import os
print(os.getcwd())  

它將顯示目前b工作目錄。

/home/prigo/Desktop

4.sys

提供對系統特定參數和函數的訪問,例如傳遞給腳本的參數。

import sys
print(sys.argv)  

它將顯示檔案名稱。

['one.py']

5.日期時間

用於操作日期和時間。

import datetime
now = datetime.datetime.now()
print(now) 

它將顯示當前日期和時間。

2024-11-22 00:59:19.436950

6.time

提供與時間相關的功能,包括時間測量和執行暫停。

import time
time.sleep(2)  # Sleep for 2 seconds

7.csv
用於讀取和寫入 CSV 檔案。

import csv
with open('data.csv', 'w', newline='') as file:
    writer = csv.writer(file)
    writer.writerow(['Name', 'Age'])
    writer.writerow(['Alice', 25])

8.numpy
用於數組和矩陣數值運算的強大庫。

import numpy as np
arr = np.array([1, 2, 3, 4])
print(np.mean(arr))  # Mean of the array

9.pandas
用於資料操作和分析,特別是表等結構化資料。

import pandas as pd
df = pd.DataFrame({'Name': ['Alice', 'Bob'], 'Age': [25, 30]})
print(df)

10.matplotlib
一個流行的繪圖庫,用於創建靜態、互動式和動畫視覺化。

import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
plt.show()

以上是python 中的預定義模組的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
Python:編譯器還是解釋器?Python:編譯器還是解釋器?May 13, 2025 am 12:10 AM

Python是解釋型語言,但也包含編譯過程。 1)Python代碼先編譯成字節碼。 2)字節碼由Python虛擬機解釋執行。 3)這種混合機制使Python既靈活又高效,但執行速度不如完全編譯型語言。

python用於循環與循環時:何時使用哪個?python用於循環與循環時:何時使用哪個?May 13, 2025 am 12:07 AM

UseeAforloopWheniteratingOveraseQuenceOrforAspecificnumberoftimes; useAwhiLeLoopWhenconTinuingUntilAcIntiment.forloopsareIdealForkNownsences,而WhileLeleLeleLeleLeleLoopSituationSituationsItuationsItuationSuationSituationswithUndEtermentersitations。

Python循環:最常見的錯誤Python循環:最常見的錯誤May 13, 2025 am 12:07 AM

pythonloopscanleadtoerrorslikeinfiniteloops,modifyingListsDuringteritation,逐個偏置,零indexingissues,andnestedloopineflinefficiencies

對於循環和python中的循環時:每個循環的優點是什麼?對於循環和python中的循環時:每個循環的優點是什麼?May 13, 2025 am 12:01 AM

forloopsareadvantageousforknowniterations and sequests,供應模擬性和可讀性;而LileLoopSareIdealFordyNamicConcitionSandunknowniterations,提供ControloperRoverTermination.1)forloopsareperfectForeTectForeTerToratingOrtratingRiteratingOrtratingRitterlistlistslists,callings conspass,calplace,cal,ofstrings ofstrings,orstrings,orstrings,orstrings ofcces

Python:深入研究彙編和解釋Python:深入研究彙編和解釋May 12, 2025 am 12:14 AM

pythonisehybridmodeLofCompilation和interpretation:1)thepythoninterpretercompilesourcecececodeintoplatform- interpententbybytecode.2)thepythonvirtualmachine(pvm)thenexecutecutestestestestestesthisbytecode,ballancingEaseofuseEfuseWithPerformance。

Python是一種解釋或編譯語言,為什麼重要?Python是一種解釋或編譯語言,為什麼重要?May 12, 2025 am 12:09 AM

pythonisbothinterpretedAndCompiled.1)它的compiledTobyTecodeForportabilityAcrosplatforms.2)bytecodeisthenInterpreted,允許fordingfordforderynamictynamictymictymictymictyandrapiddefupment,儘管Ititmaybeslowerthananeflowerthanancompiledcompiledlanguages。

對於python中的循環時循環與循環:解釋了關鍵差異對於python中的循環時循環與循環:解釋了關鍵差異May 12, 2025 am 12:08 AM

在您的知識之際,而foroopsareideal insinAdvance中,而WhileLoopSareBetterForsituations則youneedtoloopuntilaconditionismet

循環時:實用指南循環時:實用指南May 12, 2025 am 12:07 AM

ForboopSareSusedwhenthentheneMberofiterationsiskNownInAdvance,而WhileLoopSareSareDestrationsDepportonAcondition.1)ForloopSareIdealForiteratingOverSequencesLikelistSorarrays.2)whileLeleLooleSuitableApeableableableableableableforscenarioscenarioswhereTheLeTheLeTheLeTeLoopContinusunuesuntilaspecificiccificcificCondond

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用