Numbers Math – Python Math algorithm

In our previous chapter we see how we can print with python. In this post we will learn about number and math. Every programming language has some kind of way of doing numbers and math. So does Python, it have math library to do Python Math algorithm. You don’t have to be a math expert if you want to use these math algorithm for your code.

Supported Symbol for math algorithm

This exercise has lots of math symbols.

  • + plus
  • minus
  • / slash
  • * asterisk
  • % percent
  • < less-than
  • > greater-than
  • <= less-than-equal
  • >= greater-than-equal

Its same in python as in any other language. You can use all most all the mathematics symbols in python for your calculation. Now lets see some example to learn it.

First exercise:-

Let see a code:-

print ("2 x 1 is ", 2 * 1 )
print ("2 x 2 is ", 2*2 )
print ("2 x 3 is ", 2+2 + 2 )
print ("2 x 4 is ", 18 % 10 )
print ("2 x 5 is ", 12 - 2 )
print ("2 x 6 is ", 24/2)

I want you to run this code as it is, just copy and past it in your program and run it. what is output

Here you can see that “2+2” is same as “2 + 2”, space between math algorithm symbol is ignored by python and it run the code. But it is advised that you put space so that it will be easy for you to under stand the code. Sometime for big algorithm it become hard to understand for us without space.

why 12.0 ?

Its because it is a floating number. Not a integer.

Floating? Integer? – What are these things?

In python or any programming language, number are marked as decimal or floating (let’s focus on these 2 for now). number 10 is a Integer but 10.0 is floating. Any number with “.” is considered as Floating number so i.e if you have 10/4 it will be 2.5 in case if we mention it as integer it will become 2 as Integer will ignore any values after decimal “.”;

So, Wait can I specify any number as floating or integer?

Yes, you can to specify any number as integer use function int() and to specify it as float use float(). now lets write our code once again.

print ("2 x 1 is ", 2 * 1 )
print ("2 x 2 is ", 2*2 )
print ("2 x 3 is ", 2+2 + 2 )
print ("2 x 4 is ", 18 % 10 )
print ("2 x 5 is ", float(12 - 2) )
print ("2 x 6 is ", int(24 / 2))

run this code and check output:-

Now we have converted 10 into a float and 12 in integer.

 

New Functions we used today:-

int()

Used to convert any values in integer i.e 10.0 into 10 and 2.5 into 2.

float()

Used to convert any values in float, i.e 10 into 10.0

Math algorithm

Learn math algorithm symbols and how python use them. Remember Python use BOADMAS algorithm for calculation.

 

Today’s Exercise:-

Use Math for fun with BOADMAS.

 

comments (2)

Leave me comment