Python Mathematical Operations - Addition (+), Subtraction (-), Multiplication (*), Division (/), Exponentiation (**) and Remainder (%)

Computer ... have you stopped to think what it means?
It comes from computing, which means calculating.

Yes, basically what a computer do is this: compute. Lots and very fast.

In this tutorial of our Python Course, we will learn to add, subtract, multiply, divide, exponentiate and calculate the rest of the division (what the heck is that?)

How to Add in Python: +

The sum operator, in Python, is ... guess, the symbol: +
Surprise, huh?

Let's make a script that asks the user for a integer number, stores it in var1, then another integer, and stores it in var2.

Then we sum the two numbers and store in the variable sum, and we add the sum. Type and run the following code:
var1 = int( input("Type a integer:") )
var2 = int( input("Type another: ") )

sum = var1 + var2

print(soma)

Cool, right?

How to Subtract in Python: -

If you thought the subtraction symbol in Python was the -, congratulations, you are a serious candidate to win the next Nobel Prize.

Let's create a script that asks for two numbers, subtracts one from the other and displays the result:
var1 = int( input("Type a integer:") )
var2 = int( input("Type another: ") )
subt = var1 - var2 print(sub)

Note that you can only do the subtraction after giving the numbers.

If you do 'sub= var1 - var2' at the beginning, it will give you an error because Python still does not know which values are in var1 and var2, because you have not provided anything yet!



How to Multiply in Python: *

Finally something different! Yes, the multiplication symbol is not x, it is the asterisk *

var1 = int( input("Type a number:") )
var2 = int( input("Type another: ") )
res = var1 * var2 print(res)

Write the code above, run it several times, run tests, put your hand in the code, okay? Just keeping an eye on you will not make you a good Python programmer.

It is necessary to codify, that is, to enter the codes, use your hands!


How tp Divide in Python: /

The symbol of divide is the /
That is: 4/2 = 2

See the script that asks for two numbers to the user and displays the division of them:

var1 = int( input("Type a number:") )
var2 = int( input("Type another: ") )
div = var1 / var2 print(div)

Test: In the second variable, which is going to be the denominator, test put 0.
What happened? Because ?

Exponentiation in Python: **

Exponenciar, if you have forgotten, is the famous 'to the' and its symbol are two asterisks together: **

For example, 3 to 2:
3 ** 2 = 9, for 3x3 = 9
4 ** 3 = 4 * 4 * 4 = 64

3 to the 3:
3 ** 3 = 27, for 3x3x3 = 27

Run the following script:

var1 = int( input("Type a number:") )
var2 = int( input("Type another: ") )
exp = var1 ** var2 print(exp)


Test: Use huge, gigantic, hideous numbers.
So, did Python figure it out? Was fast? Gorgeous this Python, is not it?

Remainder: %

This operation you may not remember.
Let's go back to school, when we made the dividing sheets, remember?

It had the dividend, the divisor, the quotient, and the remainder, see:
Math operations in Python

To know the remainder of one number by another, we use the % operator
See the remainder of the even number division by 2, test:



var1 = int( input("Type a number:") )
var2 = int( input("Type another: ") )
res = var1 % var2 print(res)


Will always give 0 right?
Now test the remainder if an odd number by 2.

The remainder will always be one.

Let's use the remainder operator of the division for this, for example: finding even numbers. Let's use it to find prime numbers too!

You are a very important and useful operator in the programming world, okay?

Most Important Python Exercise

OK! Now, you will need to do this exercise.
Just continue on our course if you solve it.

Not matter if it gets big, ugly or confusing, but do this exercise.

Exercise: Create a program in Python that asks two numbers for the user.

Then you will show the sum, subtraction, multiplication, division, exponentiation, and remainder of the division of the first number by the second.

It has to be cute and organized like this, the result:

Free Python tutorial and course



No comments:

Post a Comment