Unit3 Modules

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

08/03/2023, 22:20 Day6 - Jupyter Notebook

Working with Python modules

Pre-defined modules

Importing libraries

In [30]:

import math
from math import pi
from math import *
from math import pi as p
import math as m

In [31]:

print(math.pi)
print(pi)
print(pi)
print(p)
print(m.pi)

3.141592653589793
3.141592653589793
3.141592653589793
3.141592653589793
3.141592653589793

In [105]:

print(type(math)) # checking the type of class

<class 'module'>

In [42]:

print(dir(math)) # list of attribute names


print(math.__doc__) # description about the module
print(math.__loader__) # checking builtin module
print(math.__name__) # cheking name of the module

['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2',
'atanh', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs',
'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isn
an', 'isqrt', 'lcm', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'nextafter', 'perm', 'pi',
'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc', 'ulp']
This module provides access to the mathematical functions
defined by the C standard.
<class '_frozen_importlib.BuiltinImporter'>
math

Reading module contents

In [45]:

print(help(math))

Help on built-in module math:

NAME
math

DESCRIPTION
This module provides access to the mathematical functions
defined by the C standard.

FUNCTIONS
acos(x, /)
Return the arc cosine (measured in radians) of x.

The result is between 0 and pi.

acosh(x, /)
Return the inverse hyperbolic cosine of x.

asin(x, /)
R t th i ( d i di ) f

localhost:8888/notebooks/Python Bootcamp/Day6.ipynb# 1/3


08/03/2023, 22:20 Day6 - Jupyter Notebook

In [55]:

import numpy
help(numpy)

Help on package numpy:

NAME
numpy

DESCRIPTION
NumPy
=====

Provides
1. An array object of arbitrary homogeneous items
2. Fast mathematical operations over arrays
3. Linear Algebra, Fourier Transforms, Random Number Generation

How to use the documentation


----------------------------
Documentation is available in two forms: docstrings provided
with the code, and a loose standing reference guide, available from
`the NumPy homepage <https://www.scipy.org>`_.

Installing & Uninstalling modules

In [46]:

!pip install python-math

Collecting python-math
Downloading python_math-0.0.1-py3-none-any.whl (2.4 kB)
Installing collected packages: python-math
Successfully installed python-math-0.0.1

In [51]:

import math

In [52]:

# !pip uninstall python-math

In [53]:

import aas

---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_16552/2605444043.py in <module>
----> 1 import aas

ModuleNotFoundError: No module named 'aas'

User-defined modules
In [106]:

import simplemaths
simplemaths.pi

Out[106]:

3.14

In [92]:

from simplemaths import *

In [107]:

import simplemaths as s
print(type(s))

<class 'module'>

localhost:8888/notebooks/Python Bootcamp/Day6.ipynb# 2/3


08/03/2023, 22:20 Day6 - Jupyter Notebook

In [108]:

Strings.lowercase("PYTHON")

Out[108]:

'python'

In [90]:

from simplemaths import Strings as st

In [109]:

st.uppercase("python")

Out[109]:

'PYTHON'

In [111]:

s.add(10,30,40,50)

Out[111]:

130

In [112]:

s.sub(10,20)

Out[112]:

-10

In [113]:

s.pow(2,3)

Out[113]:

In [104]:

class Sample:
a = 10
b = 20
@staticmethod
def display():
return print(Sample.a,Sample.b)
if __name__=='__main__':
s = Sample()
s.display()

10 20

In [ ]:

localhost:8888/notebooks/Python Bootcamp/Day6.ipynb# 3/3

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy