Py03: Python as a calculator

Py03: Python as A Calculator


In many ways, it's a dull language, borrowing solid old concepts from many other languages & styles: boring syntax, unsurprising semantics, few automatic coercions, etc etc. But that's one of the things I like about Python. 

- Tim Peters.

Python is a powerful yet simple language. This post will guide you some basic python operators and operations. Let's start by creating two variables.


 >>> variable_one = 1
 >>> variable_two = 2

So, here we've declared two variables named variable_one and variable_two both having value 1 and 2.

We've many operators in python, let's go through some of them one by one.

  1. Addition operator (+)
    •  >>> variable_three = variable_one + variable_two
    •  variable_three is equals to 3
      2. Subtraction operator (-)
    •  >>> variable_three = variable_one - variable_two
    •  variable_three is equals to -1

      3. Multiplication operator (*)
    •  >>> variable_three = variable_one * variable_two
    •  variable_three is equals to 2
      4. True Division operator (/)
    •  >>> variable_three = variable_one / variable_two
    •  variable_three is equals to 0.5
      5. Floor Division operator (//)
    •  >>> variable_three = variable_one // variable_two
    •  variable_three is equals to 0
    • (//) Floor division operator truncates the decimal part (part after the decimal point) return only integer part (part before the decimal point) from float value
    • For example, 25/2 is equals to 12.5 but 25//2 is equals to 12 (0.5 portion is removed)
      6. Modulus operator (%)
    •  >>> variable_three = variable_two % variable_one
    • variable_three is equals to 0
    •  >>> variable_three = 12 % 5 is equals to 2 because when 12 is divided by 5, 2 remains as the remainder.
# Note: Python operators are really special. How? We'll meet them again in more detail in future posts.

Python also supports compound assignment operators. Compound assignment operators are used when we have to assign the value of operation to one of the operands of the operation.

Compound assignment operators are used when we have to perform an operation like this:
    •  >>> variable_one = variable_one + variable_two

                Here, we've increased the value on variable_one by adding variable_two to it. But this and other similar operations where we want to assign the value of the operation to one of the operand can be done using in a much simpler way by using Compound Assignment operators. Compound Assignment Operators modify the desired operand with the result of the operation.:
    1. Compound Assignment Addition operator (+=)
      •  >>> variable_one += variable_two
      • value of variable_one is now 3 (1 + 2)
          2. Compound Assignment Subtraction operator (-=)
      •  >>> variable_one -= variable_two
      • value of variable_one is now -1 (1 - 2)

               3.  Compound Assignment Multiplication operator (*=)

      •  >>> variable_one *= variable_two
      • value of variable_one is now 2 (1 * 2)

               4.  Compound Assignment Division operator (/=)

      •  >>> variable_one /= variable_two
      • value of variable_one is now 0.5 (1 / 2)

               5.  Compound Assignment Floor Division operator (//=)

      •  >>> variable_one //= variable_two
      • value of variable_one is now 0 (1 // 2 = 0)

               6.  Compound Assignment Multiplication operator (%=)

      •  >>> variable_one *= variable_two
      • value of variable_one is now 1 (1 % 2 == 1)
      • 1 % 2 equals to 1 because the way this operation evalutaed is 1 = 2 * 0 + 1,  which gives us the answer 1 (one)

Comparison Operators

Python also provides comparison operators, which helps us to evaluate the comparison relation between the numbers:


Above table shows some of the comparison operators and their example in python.
You can also combine multiple operators to perform bigger logical operations, which leads us to two new Python operators 'and' and 'or'.
'and' operator is used when you want a condition to be True only when all of the conditions are True.
'or' operator is used when you want a condition to be True, only when any of the conditions is True.

So, if I want to check variable_two (= 5) against two conditions, first whether it is smaller than variable_three (= 10) and second whether it is greater than variable_one (= 0), then it can be done in the following manner:

  >>> variable_one = 0
 >>> variable_two = 5
 >>> variable_three = 10
 >>> # First way
 >>> variable_one < variable_two < variable_three
 True
 >>> # Second way
 >>> variable_one < variable_two and variable_one < variable_three
 True
 >>> # Third Example
 >>> variable_three == variable_three or variable_one < variable_two
 True
 >>> # Fourth way
 >>> variable_one > variable_two or variable_one < variable_three

 True

Here, in the first way we've combined multiple conditions into single statement. Unlike many other languages Python support combining multiple conditions which leads to better readability which is one of the top priority of Python language design.

In the third example, we've used equality test operator which would always return true as both of the operands are the same variable, but since we've used or operator and the first condition turns out to be True, the second condition, variable_one < variable_two, will not be evaluated by the interpreter.

Remember, the first way can only be used when you have to perform and operation on the conditions. There are a ton of other operators available in Python, which we'll discuss in future when we will study Python Data Model.

In next post, we'll talk about datatypes in Python and will play more with Python variable/objects. Objects!!! you read it right, hang on till some more posts, we'll be discussing more on this later.

No, it's not an illusion, you've already read above lines in previous post but I thought it was necessary to make the reader aware of various operators in Python, which will be helpful for studying and performing various operations on the datatypes.

Try It Yourself:
Prepare a script for using all the discussed mathematical and comparison operators and print their result using print function.


Happy Pythoning!!

Comments