#bank.py def deposit(amount): print("Enter the deposit amount:",amount) def withdraw(amount): print("Enter the withdraw amount:",amount)
Create the python module file name is called bank.py
The two functons are
deposit(amount): This function takes one parameter amount and prints the message indicating the deposit amount.
withdraw(amount): This function also takes one parameter amount and prints the message indicating the withdrawal amount.
#customer.py import bank bank.deposit(1000) bank.withdraw(500)
Create the another python module file name is called customer.py
Using this import keyword we can import the bank module. so we can access the deposit() and withdraw() functions from the customer.py
Enter the deposit amount: 1000 Enter the withdraw amount: 500
Python predefined modules:
1.random:
The random module allows you to generate random numbers, shuffle data, and select random elements from sequences.
import random otp = random.randint(100000,999999) print(otp)
random.randint(a, b) returns a random integer between a and b (inclusive).
624367
2.math:
The math module provides functions for basic mathematical operations and constants.
import math print(math.fabs(-5))
math.fabs(x): Returns the absolute value of x.
5.0
3.os:
Provides functions for interacting with the operating system (e.g., file handling, directories).
import os print(os.getcwd())
It will display the currentb working directory.
/home/prigo/Desktop
4.sys
Provides access to system-specific parameters and functions, such as arguments passed to the script.
import sys print(sys.argv)
It will display the filename.
['one.py']
5.datetime
Used for manipulating dates and times.
import datetime now = datetime.datetime.now() print(now)
It will display the current date and time.
2024-11-22 00:59:19.436950
6.time
Provides time-related functions, including time measurements and pauses in execution.
import time time.sleep(2) # Sleep for 2 seconds
7.csv
For reading and writing CSV files.
import csv with open('data.csv', 'w', newline='') as file: writer = csv.writer(file) writer.writerow(['Name', 'Age']) writer.writerow(['Alice', 25])
8.numpy
A powerful library for numerical operations on arrays and matrices.
import numpy as np arr = np.array([1, 2, 3, 4]) print(np.mean(arr)) # Mean of the array
9.pandas
Used for data manipulation and analysis, especially for structured data like tables.
import pandas as pd df = pd.DataFrame({'Name': ['Alice', 'Bob'], 'Age': [25, 30]}) print(df)
10.matplotlib
A popular plotting library for creating static, interactive, and animated visualizations.
import matplotlib.pyplot as plt plt.plot([1, 2, 3], [4, 5, 6]) plt.show()
The above is the detailed content of Predefined Modules in python. For more information, please follow other related articles on the PHP Chinese website!