Python Basics : Part 1 – Arithmetic

Python and R are the two widely used programming languages for data science. I am going to start with some basic python coding on the jupyter notebook which should help us better understand how tough or easy it is to code.

Python has two basic number types : Integer and floating point. So pretty much any arithmetic function can be done in python on these data types. Here are some examples to get started with :

You can try these out on your local session of jupyter notebook

Addition –

In [1] : 1+1
Out[1] : 2

Subtraction –

In [2] : 1-1
Out[2] : 0

Multiplication –

In [3] : 2*2
Out[3] : 4

Division –

In [4] : 1/2
Out[4] : 0

Note that 1/2 in Python 2.7 will throw 0 as the output where as Python 3.6 will throw 0.5 as the output. Python 2.7 expects the denominator to be present in floating point format for the correct results.

In [5] : 1/2.0
Out[5] : 0.5

Exponent –

In [6]: 2**4
Out[6]: 16

Modulus –

In [7]: 4%2
Out[7]: 0

The modulus function helps identifying if the number is even or odd based on the output. MOD the input number by 2 and if the output is 0 then the input number is even else its odd.

In [8]: 5%2
Out[8]: 1

You can try out more of such arithmetic operations and let me know in the comments sections below. Let’s now move on to the topic of variable assignments in python.