Hello guys welcome to my article, in this post we’re gonna be learning more about basic math operators.
Python Numbers:
A programming language needs to have support for numbers to carry out calculations. To do this in Python, the numbers are categorized into different data-types.
In this tutorial, you will look at how numbers work in Python, how to assign them to variables, and how to do basic operations using those variables.
Let’s see the basic calculations:
>>> 5 + 4
9
>>>
so in the last tutorial we can saw that you can add two numbers together five plus four is equals nine, simple enough. Now you can also use minus to subtract that’s you know kind of intuitive.
However if you never program before I’ll talk to you guys about multiplication and division whenever you want to multiply two numbers you use the asterisks which is above the a on your keyboard then you can just do three times twenty is equals sixty, easy enough.
>>> 3 * 20
60
>>>
Now whenever you want to divide use the forward slash
>>> 8 / 4
2.8
>>>
now just like in real life computer programming in Python also has orders of operation. So,
>>> 8 + 2 * 10
28
>>>
I think the result is 100. Okay we got 28 and that’s because just like in real life it follows the orders of operation at multiplies and divides first.
Now if you ever wanted to run this like you thought it would you can put them in parentheses so,
>>> (8 + 2) * 10
100
>>>
and again anything in parentheses happens first takes precedence over anything else.
Now this is pretty simple stuff so far however the one thing that may confuse you is whenever you’re using division, and this is the not only Python but all computer programming languages. Computers handle a division and the kind of weird way and you may get some unexpected results.
Now Python is pretty good at just simple division so,
>>> 18 / 4
4.5
>>>
now there are a couple cool techniques that we can use and this is actually helpful in a lot of cases.
There are sometimes that we want to round down or in other words drop this decimal place, so we’re gonna say
>>> 18 // 4
4
>>>
whenever we’ll use two forward slashes what this does is it takes the result and rounds it down to the nearest whole number in other words, is pretty much takes the decimal place and drops it.
Now if we only want to access the remainder such as
>>> 18%4
2
>>>
so what this does is whenever you use the percent sign, is the modulus it’s called the percent sign it gives you remainder, simple enough.
Well, another cool thing that you can do with Python is really easily calculate the power or the exponents. So say that you wanted to do
>>> 5 * 5 * 5
125
>>>
or, you can do this
>>> 5**3
125
>>>
his is pretty much the same thing, it’s pretty much saying five to the power of three, so again double asterisk means calculate the powers.
Alright so that’s pretty much the core basic of how to use simple math operators in Python. So now I’ll teach you guys about variables.
What is variable ?
Variable is just like a placeholder for something else, just like a math class.
Take X and you can name your variables pretty much anything you want, make sure you start them with letters and they can also include numbers on it. Don’t include anything like a stupid symbol, pretty much start it with a letter and you can also have numbers in as well those are like a very basic rule.
>>> tuna = 5
>>>
tuna it’s equal to the value of five, now we can use it in place of any equation, so for example
>>> 20 + tuna
25
>>>
you can actually use all variables in your equations.
>>> bacon = 20
>>> bacon / tuna
4.0
>>>
so if you guys ever wanted to know what bacon / tuna is it’s 4.0.
Pretty interesting but now we know the basic math operators also some cool or you know extra things like exponents and stuff, and also how to divide bacon over tuna.
Numbers:
What are numbers in Python? All numbers that are handled will belong to some type. A type is a way to define various data structures and containers and define the functionality associated with them. Types are implemented in Python as classes. If you want to verify if an integer belongs to the class int you can use isinstance. You will need to pass number as the first argument and class name as the second argument, and it will check if the object is an instance of the class. This is done by the following construct.
isinstance(object, class)
This checks if the object belongs to the class. We will throw more light on objects and how they belong to a class, or “object instantiation”, in the objects and class tutorial. For now, just remember that an integer number is categorized into int class and hence if you check whether the number 2 is an instance of the class int, the interpreter will return True.
>>> isinstance(2, int)
True
Numeric types and operations using numbers
There are three numeric types in Python: int for integers, float for decimal numbers, and complex for complex numbers. General mathematical operations such as addition, subtraction, and multiplication can be done on them.
Operations when the type are the same: When the type of the input numbers are the same the result will be a number of the same class that the input numbers belong. This is true except the case of division as will be shown in the following examples.
For example, you can assign the integers 2 and 3 to variables int_var and int_var1and then print the sum of the two.
>>> # assign the integer 2 to a var int_var
>>> int_var = 2
>>> # assign the integer 3 to a var int_var1
>>> int_var1 = 3
>>> # print the sum of the two variables int_var and int_var1
>>> print(int_var + int_var1)
5
Similarly, assign two decimal numbers to two variables, say, float_var1 and float_var2, and print the sum of the two.
>>> # assign a decimal number to a variable float_var
>>> float_var = 5.6
>>> # assign another decimal number to a variable float_var1
>>> float_var1 = 6.7
>>> # print the sum of the two decimal numbers
>>> print(float_var + float_var1)
12.3
You can finally check if the same operation can be done on complex numbers. Assign two complex numbers to two variables. say, complex_var and complex_var1 and print the sum of the two.
>>> # assign a complex number to a variable complex_var
>>> complex_var = (1 + 2j)
>>> # assign another complex number to a variable complex_var1
>>> complex_var1 = (2 + 3j)
>>> # print the sum of the two complex numbers
>>> print(complex_var + complex_var1)
(3+5j)
In case of division, dividing two integers will always result in a float.
>>> # divide two integers and assign it to a variable result
>>> result = 3 / 2
>>> # print the result
>>> print(result)
1.5
>>> # check if the result belongs to the class float
>>> print(isinstance(result, float))
True
Operations between numbers of different types: The operations work when operate of variables that are of different numeric types as well.
For example you can add two numbers, one with class int and another with class float. The result will be in float. This is done by changing the integer to a float and then adding them.
>>> # add two numbers, one is int and the other is float
>>> result = 2 + 5.1
>>> # print the result, note that the result is a float
>>> print(result)
7.1
>>> # check that the result is an instance of float
>>> print(isinstance(result, float))
True
Other operations like adding int to complex or float to complex works in similar manner. The results of both the operations belong to the complex class.
>>> # add two numbers belonging to complex and int class
>>> result = (2 + 3j) + 4
>>> # print the result
>>> print(result)
(6+3j)
>>> # check if the result is of class complex
>>> print(isinstance(result, complex))
True
>>> # add two numbers belonging to complex and float class
>>> result = (2 + 3j) + 4.12
>>> # print the result
>>> print(result)
(6.12+3j)
>>> # check if the result is of class complex
>>> print(isinstance(result, complex))
True
Similarly, other operators are also supported. You can check more on operators in our basic programming tutorials.