Python Math Functions

python-math-funtions-ipcisco-1

Python Math functions is one of the most used functions in Python Programming. In python there are different built-in math functions. Beside there is also a math module in python. With these functions, we can do different mathematical operations. Here, we will focus on Python Math and learn how to use some of these functions.

 

Here, we will learn Math in three parts. In the first part we will learn the basic math functions with different coding examples. In the second part, we will learn different Python Math functions. And in the last part, we will learn python math module.

 


You can also check Python Operators


 

Using Basic Python Math Operations

 

Here, firstly we will focus basic math operations used in python. We will learn these operations with different python coding examples. So what are these basic operations? These are:

 

  • + (addition)
  • – (subtraction)
  • * (multiplication)
  • / (divide)
  • % (difference)
  • ** (power)

 

Now, let’s start to learn these basic operations one by one.

 


You can also check Python While Loop


 

Addition

 

Below, we will add two numbers and find the result.

 

a = 5
b = 7
c = a + b
print(c)
12

 

python-plus-operator-ipcisco-1

 

We can do the same operation with the below code also.

 

a = 5
b = 7
print(a + b)

 

12

 

 

You can also watch the video of this lesson.


 

Subtraction

 

Now, let’s give an example for subtraction.

 

a = 16
b = 8
print(a - b)
8

 

python-substraction-operator-ipcisco-1

 

Below, we will use both subtraction and addition together.

 

a = 16
b = 8
c = 3
print(a - b + c)

 

11

 


 

Multiplication

 

Below, we will use multiplication and find the result.

 

a = 11
b = 5
print(a * b)
55

python-multiplication-operator-ipcisco-1

 

Now, let’s use multiplication with subtraction.

 

a = 11
b = 5
c = 3
print(a * b - c)

 

52

 


 

Division

 

In the below python coding example, we will use division.

 

a = 11
b = 5
c = a/b
print(c)
2.2

 

python-math-funtions-ipcisco-4

 

Below, we will use multiplication and division together.

 

a = 11
b = 5
c = 10
print(a/b*c)

 

22.0

 


 

Difference

 

Now, let ‘s use find the difference of a division operation.

 

a = 11
b = 5
print(a%b)

 

1

 

python-difference-operator-ipcisco-1

 

Now, let’s do an example that finds the even numbers in a list.

 

a = [1,2,3,4,5,6]
for x in a:
   if x%2==0:
      print(x)

 

2
4
6

 


 

Power

Lastly, let’s talk about power operator(**). Below we will calculate b power of a with power operator (**).

 

a = 2
b = 5
print(a**b)

 

32

python-power-operator-ipcisco-1

In our last example with math operators, we will use different operators together like below:

 

a = 2
b = 5
c = 15
d = 3
print(a**b+c/d)
37.0

 

 


 

Built In Python Math Functions

 

We have learned basic python math operations above. Now, let’s learn complex operations with python math functions. These functions can be used also as python for math.

 

min() & max() Functions

 

Firtslty, we will start with min() function and max() function. With the sefunctions, we will learn minimum and maximum numbers between the given numbers.

 

a = min(7,15,2)
b = max(8,28,12)
print(a)
print(b)
2
28

 

python-math-funtions-ipcisco-7

 


 

pow() Function

 

Now, let’s find a to the power of b with a built in math function. This function is pow() function.

 

a = 2
b = 5
print(pow (a,b))

 

32

 

python-math-module-pow-ipcisco-8


 

abs() Function

 

Another built-in math function is abs() function. This function returns with the positive value of a given number.

 

a = abs(-1.24)
print(a)
1.24

 

 

 


 

Python Math Module

 

In python there is a built-in module. This module is math module. We can extend math functions with this math module.

 

We need to import this module to use these math functions. To import this math module we can use “import” code like below:

 

import math

 

There are many math functions in math module. Some of these math functions are given below:

 

  • sqrt
  • factorial
  • ceil
  • floor
  • pi

 

There are also more complex functions in math module like:

 

  • cos()
  • sin()
  • comb()
  • gamma()
  • log()
  • radians()
  • tan()
  • fmod()
  • fsum()
  • fabs()

 

Here, we will give example for the simpler ones.

 


 

sqrt() Function

 

Firstly, we will start with math.sqrt() function. With this function we will find the square root of a number.

 

import math
x = math.sqrt(100)
print(x)

 

10.0

python-math-funtions-ipcisco-9


 

factorial() Function

 

Now, let’s find the factorial of a number with math.factorial() method.

 

import math
x=math.factorial(5)
print(x)
120

python-math-module-factorial-ipcisco-8

 


ceil() & floor() Function

Now, let’s learn two functions that rounds the given number. Here, there are two functions. One of them is ceil() method that rounds upwards to the nearest integer. The other is floor() method that rounds downwards to the nearest integer.

 

import math
x = math.ceil(1.7)
print(x)

 

2

  

python-math-module-ceil-ipcisco-11

 

import math
x = math.floor(1.7)
print(x)

 

1

 

python-math-module-floor-ipcisco-11


 

pi() Function

 

The other match function is math.pi() function. This is simply pi number.

 

import math

x = math.pi

print(x)

 

3.141592653589793

 


 

Important Functions For Python

 

In this lesson, we have learned Python math operations and python math functions. There are many math fucntions so we have showed examples for some of them only. If you need more complex mathematical functions, you can check all the functions in python math module.

 

Back to: Python Programming Course > Python Math

2 Responses to “Python Math Functions”


  • I’m new to programming but this makes it seem so simple and easy to understand. Please send more tutorials to broaden my knowledge.
    Thanks for the wonderful work.

    • gokhankosem / / Reply

      It is nice to hear this Tommy. There will be more Python lessons on IPCisco. Enjoy and good luck!

Leave a Reply

Your email address will not be published. Required fields are marked *

Python Programming Course

Collapse
Expand