Simple Calculator using Python

In this blog, we will create a simple calculator using python.

First of all we will start by asking user to enter two values. These two values would be supplied to function called calculator which performs addition, subtraction, multiplication and division and then display the result.

Asking to user to  enter first value

val1 = int(input('Please enter first value: '))

Asking user to enter second value

val2 = int(input('Please enter second value: '))

Passing these two values to function calculator

calculator(val1,val2)

Below is the code written inside calculator function. Two values (val1 and val2) has been assigned to (a,b) variables of function.

def calculator(a,b):
    c = a+b
    print('Addition of %s and %s is %s'%(a,b,c))
    c = a-b
    print('Subtraction of %s and %s is %s' % (a, b, c))
    c = a*b
    print('Multiplication of %s and %s is %s' % (a, b, c))
    c = a/b
    print('Division of %s by %s is %s' % (a, b, c))

Output

Please enter first value: 50
Please enter second value: 25
Addition of 50 and 25 is 75
Subtraction of 50 and 25 is 25
Multiplication of 50 and 25 is 1250
Division of 50 by 25 is 2.0

Complete code

def calculator(a,b):
    c = a+b
    print('Addition of %s and %s is %s'%(a,b,c))
    c = a-b
    print('Subtraction of %s and %s is %s' % (a, b, c))
    c = a*b
    print('Multiplication of %s and %s is %s' % (a, b, c))
    c = a/b
    print('Division of %s by %s is %s' % (a, b, c))

val1 = int(input('Please enter first value: '))
val2 = int(input('Please enter second value: '))
calculator(val1,val2)

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s