Let’s make a Calculator

Great! Now let’s do some math and get our hands dirty. In python, Maths operations are very simple to perform. You can add, subtract, multiply, divide and even do exponentiation.


addition = 72 + 23
subtraction = 108 – 204
multiplication = 108 * 0.5
division = 108 / 9

exponentiation = 10**2

modulo = 3%2

print addition,subtraction,multiplication,division,modulo

NOTE : print statements can be used with commas to print the variables with a tab to differentiate between different variables.

Question : Try to make a function of every operation that takes two parameters and apply operation on those parameters and return the result

Hint : I have defined one function for your help

def addition(a,b):

return a+b

Multi-Line Statements

Python uses continuation character (\) to denote that the line should continue. If suppose we want to add three numbers and assign it to one variable. Multi-Line Statements are generally used when line has exceeded a certain width of page to make the code easily viewable.

addition = variable1 + \
variable2 + \

Standard Data Types

There can be different types of data. For example, a person’s weight is stored as numeric value or a decimal value, his or her name is stored as string. To handle different types of data python have different data types to handle each type of data.

Python has five standard data types

  • Numbers
  • String
  • List
  • Tuple
  • Dictionary

Calculator Generalization

You might have wondered that we have assigned the inputs and we are not taking values from the user directly. Well, There is also a way to take inputs from the user directly from the console using input() function.

Recommended Post   How to Install Python on Your System?

The following program displays the prompt asking a value from the user by displaying the message on the console stating “Press the enter key to exit”.

>>> input(“Press the enter key to exit.”)

However, for different versions of python, there are different ways to take the input.

Python 2

>>> variable1 = input(‘enter a number: ‘)

enter a number: 123

>>> type(variable1)
<type ‘int’>

>>> variable1 = raw_input(‘enter a number: ‘)

enter a number: 123

>>> type(variable1)
<type ‘str’>

Python 3

>>> variable1 = input(‘enter a number: ‘)

enter a number: 123

>>> type(variable1)
<class ‘str’>

Since Python 3.x doesn’t evaluate and convert the data type, you have to explicitly convert to ints, with int, like this

>>> x = int(input(“Enter a number: “))

>> y = int(input(“Enter a number: “))


Question: Make a calculator to take values from user and perform all different types of operations.

Content Protection by DMCA.com