Let us make a calculator !

Hope you have followed all my previous blogs. In this blog, we use all the concepts that we have learnt in the previous blogs to make a simple python calculator. This requires the concepts of user defined functions,numbers,strings and some basic input-output commands.

Hope you are excited to make your first Python calculator !!

So lets get started. In this calculator we perform basic operations of addition,subtraction,multiplication and division.


In the screenshot above, we define 4 functions called as addition,subtraction,multiplication and division which takes in 2 inputs called num1 and num2 and returns the particular result.

Screenshot (59)

In the screenshot above, we take in 2 inputs from the user for the 2 numbers or operands called as num1 and num2 . We use input(“Enter “) for this purpose. int(input(” Enter “)) makes whatever the user enters into an integer type. For example if the user enters 4.0 it converts it to 4.

After this we take in another input from the user called user_input for getting the choice of the user whether he/she wants to add or subtract or multiply or divide or exit the program.


In the above screenshot, we define a while(True) loop which runs infinitely unless you use break statement. We use if/elif/else ladder to perform particular operation on the numbers and print them out. If the user enters 5 or any other input we break out of the loop. We observe that in these function the actual parameters and the formal parameters are both the same (num1 and num2). We also observe that there is a function call happening at each print statement. For example when we print the sum, we call addition(num1,num2) function and pass num1 and num2 to addition function. num1 and num2 are actual parameters. The function computes the result and passes it back to the place where it is called from.

Lets see a few outputs.




So that was your first Python calculator.

Image courtesy: Google Images.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Powered by WordPress.com.

Up ↑

%d bloggers like this: