Date:

Share:

Python Tutorials – Episode 3 – Arithmetic Operations

Related Articles

Now that your machine is ready, you really want to do some programming, right? Yeah, figured much. But lets bring you up with arithmetic operations.
On Windows, open CMD or Powershell. Then type ‘python’ and press ‘Enter’. Something similar to following two images should look like if your work in Episode 2 was correctly done.
On Ubuntu, press Alt+CTRL+t to open terminal and same as it opens in windows, a terminal should open up.  Then type ‘python’ and press ‘Enter’. Something similar to following two images should look like if your work in Episode 2 was correctly done.
On Mac, press command and search for terminal. Then type ‘python’ and press ‘Enter’. Something similar to following two images should look like if your work in Episode 2 was correctly done.

Now Type following code and press Enter. You will see exactly the same things as in the following Images

print ('hello world')
This is how python should look like in CMD after typing a print function
This is how powershell looks like after typing a print function

From here on, code is same on all platforms whether Mac, Windows or Linux

Arithmetic Operations

Python has simplest arithmetic operations known to me compared to other programming languages

Addition

Take two or more numbers and add them just as same as you would do in second grade school.

>>>2 + 3
5
>>>2 + 5 + 7 + 2 + 9
25
>>> 1.9 + 2.4 + 3 + 9
16.3

Similarly you can keep on going with the numbers in any combinations for simple additions

Subtraction

Take two or more numbers and subtract one from the other as you would do in second grade of school.

>>>4 - 2
2
>>>5-2-4-6-9
-12

Multiplication

Multiplication is also easy. A fourth grader can easily do it. Except instead of using ‘x’ we use ‘*’

>>>3*5
15
>>>9*4*2
72
>>>5.3*2.3*1.2*4
58.51199999999999

Division

Division is also easy in similar fashion as above. Here instead of using ‘÷’ we use ‘/’

>>>6/2
3
>>>9/3/2
1.5
>>> 11 / 6
1.8333333333333333

Modulus

In other words the remainder during the division is calculated with this operator. Its symbol is ‘%’ in Python and many other programming languages.

>>> 5 % 3
2
>>> 9 % 6
3
>>> 15 % 9
6

Exponents

Exponents can simply be explained as power of any number or multiplying a given number by itself for any other given number of times.
For example, if you want to multiply 3 by itself for 5 times, you would use  
35 in your arithmetic problems. What it does is it multiplies 3 by itself 5 times like 3 x 3 x 3 x 3 x 3 and resulting answer is 243.
To use this type of arithmetic in python, we use ‘**’ as our operator.

>>> 3 ** 5
243
>>> 19 ** 5
2476099
>>> 9 ** 11
31381059609

Floor Division

This might be tricky to those of you who are not good in Mathematics or have no programming experience prior to this.
When you divide a larger number by a smaller number, there is a chance that larger number is not direct multiple of smaller number and you are left with some remainder. When using division shown above, that remainder is also divided by the given smaller number as shown in third example above where 11 is divided by 6. What this operator does is that it stops the division when remainder is smaller than divider. 
The symbol used for floor division is ‘//’ in python and many other programming languages

>>> 11 // 6
1
>>> 16 // 3
5
>>> 11 // 3
2
>>> 15 // 4
3

Mix of Arithmetic Operators

In python, you can also use mix of arithmetic operators and get the required result

>>>2 - 3 + 5 - 4
0
>>>9 - 5 + 1 -3 
2
>>>6 / 3 * 3 /3 + 9 ** 2 
83

In Python, you can make some complex arithmetic operations and get the desired result by using parenthesis.

>>> (2 ** 3) + ((84 - 16) / (15 + 2)) - ((9 // 7) * (2 * 6)) + (4 + 5)
9.0

Using Variables with Arithmetic

Assuming that you have some programming experience before this, I won’t dive into concept of variables. But if you still have questions or are new to programming, ask in comments. Variables in Python do not require you to tell them specifically what you are going to store in them.
Same as above, you can do even more complex mathematical operations by using variables.

>>>a = 5
>>>b = 3
>>>a+b
8
>>>c = 2.1
>>>a * b * c
31.5

and so on. You can use variables in arithmetic in any way you want.

Step 16: Integrating Shopify with Social Media Platforms

how to integrate your Shopify store with popular social media platforms like Facebook, Instagram, and Pinterest.

Step 3: Understanding Shopify’s Dashboard: A Beginner’s Walkthrough

Now that you've successfully set up your Shopify account following our previous guide, "Setting Up Your Shopify Account: Step by Step Guide," it's time...

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Popular Articles