DEV Community

Cover image for Learning Python- Intermediate course: Day 5, Exploring the math module

Posted on • Updated on

Learning Python- Intermediate course: Day 5, Exploring the math module

Today we will study how to import modules in Python, and explore the math module.

What are modules?

A module is a python file containing Python definitions and statements. A module it must end with the .py extension like all other Python files.

A module is basically a bunch of functions, variables and runnable code. By grouping code into modules makes the code easier to read, logically understandable and enhances flexibility.
You can get the official documentation of modules here

We will learn to create our own modules in the later parts, but today we explore one of the very useful Python module, that is the math module.

Importing modules.

For using the math module, we just need to type import math, and you are set to go! You can import any Python module using the import statement.

The math module

The math module in Python is a very useful module which lets us use mathematical functions in Python. It contains many useful mathematical functions and some constants. You can check the whole list here

Here I am presenting some of the very commonly used functions.

>>> import math # import the math module
>>> a=2
>>> b=3
>>> c=4.4
>>> math.ceil(c) # the ceiling function in Python gives the smallest integer greater than or equal to the number.
>>> math.floor(c) # the floor function in Python gives the greatesr integer less than or equal to the number.
>>> math.factorial(b)
>>> math.comb(b,a) # Return the number of ways to choose a items from b items without repetition and without order.
>>> math.comb(c,a) #Raises TypeError if either of the arguments are not integers
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'float' object cannot be interpreted as an integer
>>> math.comb(-b,a) #Raises ValueError if either of the arguments are negative.
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: n must be a non-negative integer
>>> math.gcd(a,b)
>>> math.exp(a) # e raised to the power a
>>> math.log(a) # logarithm to the base 10
>>> math.log2(a) # logarithm to the base 2
>>> math.log(a,b) # logarithm to the base b
>>> math.pow(a,b) # a^b
>>> math.sqrt(a) #square root of a
>>> math.sin(a) # trigoonomatric functions
>>> math.atan(a) # inverse trigonometric functions
>>> math.degrees(a) # convert to degrees
>>> math.radians(a) #convert to radians
>>> math.gamma(a) # gamma function.
>>> math.hypot(a,b) #hypotaneous calculation using the pythagores theorem.
>>> math.pi # mathematical constant pi
>>> math.e # mathematical constant e

Enter fullscreen mode Exit fullscreen mode

✌️So friends that's all for now. 😊 Hope you all are having fun.😎 Please let me know in the comment section below 👇. And don't forget to like the post if you did. 😍 I am open to any suggestions or doubts. 🤠 Just post in the comments below or gmail me 😉
Thank you all👍

Also please visit the Learning-Python repo made especially for this course and don't forget to star it too!

Discussion (0)