Home > Backend Development > Python Tutorial > Predefined Modules in python

Predefined Modules in python

Linda Hamilton
Release: 2024-11-25 06:30:12
Original
732 people have browsed it

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)
def withdraw(amount):
    print("Enter the withdraw amount:",amount)
Copy after login

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)

Copy after login

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


Copy after login

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)

Copy after login

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

624367
Copy after login

2.math:

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

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

Copy after login

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

5.0
Copy after login

3.os:

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

import os
print(os.getcwd())  
Copy after login

It will display the currentb working directory.

/home/prigo/Desktop
Copy after login

4.sys

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

import sys
print(sys.argv)  
Copy after login

It will display the filename.

['one.py']
Copy after login

5.datetime

Used for manipulating dates and times.

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

Copy after login

It will display the current date and time.

2024-11-22 00:59:19.436950

Copy after login

6.time

Provides time-related functions, including time measurements and pauses in execution.

import time
time.sleep(2)  # Sleep for 2 seconds
Copy after login

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

Copy after login

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

Copy after login

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)
Copy after login

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

Copy after login

The above is the detailed content of Predefined Modules in python. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template