DEV Community

Lakshmi Pritha Nadesan
Lakshmi Pritha Nadesan

Posted on

Predefined Modules in python

  1. Create a python module called Bank.
  2. Add functions: deposit(amount) withdraw(amount)
  3. Create one more python module called Customer
  4. From customer module, call deposit and withdraw functions of Bank module.
#bank.py
def deposit(amount):
    print("Enter the deposit amount:",amount)
    return(amount)
def withdraw(amount):
    print("Enter the withdraw amount:",amount)
    return(amount)
Enter fullscreen mode Exit fullscreen mode

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=bank.deposit(1000)
bank_withdraw=bank.withdraw(500)
bank_statement=bank_deposit-bank_withdraw
print("Balance amount:",bank_statement)
Enter fullscreen mode Exit fullscreen mode

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
Balance amount: 500
Enter fullscreen mode Exit fullscreen mode

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)

Enter fullscreen mode Exit fullscreen mode

random.randint(a, b) returns a random integer between a and b (inclusive).

624367
Enter fullscreen mode Exit fullscreen mode

2.math:

The math module provides functions for basic mathematical operations and constants.

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

Enter fullscreen mode Exit fullscreen mode

math.fabs(x): Returns the absolute value of x.

5.0
Enter fullscreen mode Exit fullscreen mode

3.os:

Provides functions for interacting with the operating system (e.g., file handling, directories).

import os
print(os.getcwd())  
Enter fullscreen mode Exit fullscreen mode

It will display the currentb working directory.

/home/prigo/Desktop
Enter fullscreen mode Exit fullscreen mode

4.sys

Provides access to system-specific parameters and functions, such as arguments passed to the script.

import sys
print(sys.argv)  
Enter fullscreen mode Exit fullscreen mode

It will display the filename.

['one.py']
Enter fullscreen mode Exit fullscreen mode

5.datetime

Used for manipulating dates and times.

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

Enter fullscreen mode Exit fullscreen mode

It will display the current date and time.

2024-11-22 00:59:19.436950

Enter fullscreen mode Exit fullscreen mode

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])

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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()

Enter fullscreen mode Exit fullscreen mode

Top comments (0)