- Create a python module called Bank.
- Add functions: deposit(amount) withdraw(amount)
- Create one more python module called Customer
- 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)
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!

TomergelistsinPython,youcanusethe operator,extendmethod,listcomprehension,oritertools.chain,eachwithspecificadvantages:1)The operatorissimplebutlessefficientforlargelists;2)extendismemory-efficientbutmodifiestheoriginallist;3)listcomprehensionoffersf

In Python 3, two lists can be connected through a variety of methods: 1) Use operator, which is suitable for small lists, but is inefficient for large lists; 2) Use extend method, which is suitable for large lists, with high memory efficiency, but will modify the original list; 3) Use * operator, which is suitable for merging multiple lists, without modifying the original list; 4) Use itertools.chain, which is suitable for large data sets, with high memory efficiency.

Using the join() method is the most efficient way to connect strings from lists in Python. 1) Use the join() method to be efficient and easy to read. 2) The cycle uses operators inefficiently for large lists. 3) The combination of list comprehension and join() is suitable for scenarios that require conversion. 4) The reduce() method is suitable for other types of reductions, but is inefficient for string concatenation. The complete sentence ends.

PythonexecutionistheprocessoftransformingPythoncodeintoexecutableinstructions.1)Theinterpreterreadsthecode,convertingitintobytecode,whichthePythonVirtualMachine(PVM)executes.2)TheGlobalInterpreterLock(GIL)managesthreadexecution,potentiallylimitingmul

Key features of Python include: 1. The syntax is concise and easy to understand, suitable for beginners; 2. Dynamic type system, improving development speed; 3. Rich standard library, supporting multiple tasks; 4. Strong community and ecosystem, providing extensive support; 5. Interpretation, suitable for scripting and rapid prototyping; 6. Multi-paradigm support, suitable for various programming styles.

Python is an interpreted language, but it also includes the compilation process. 1) Python code is first compiled into bytecode. 2) Bytecode is interpreted and executed by Python virtual machine. 3) This hybrid mechanism makes Python both flexible and efficient, but not as fast as a fully compiled language.

Useaforloopwheniteratingoverasequenceorforaspecificnumberoftimes;useawhileloopwhencontinuinguntilaconditionismet.Forloopsareidealforknownsequences,whilewhileloopssuitsituationswithundeterminediterations.

Pythonloopscanleadtoerrorslikeinfiniteloops,modifyinglistsduringiteration,off-by-oneerrors,zero-indexingissues,andnestedloopinefficiencies.Toavoidthese:1)Use'i


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version
Visual web development tools

Atom editor mac version download
The most popular open source editor
