Skip to main content

A simple Calculator Using Python

Hello viewers,
Today I am gonna show you a simple code to help you make a calculator using Python programming language.

There are a number of ways of doing this.The man idea behind this code is to teach you how to write functions in Python.

The syntax goes like this

Syntax

def functionname( parameters ):
   "function_docstring"
   function_suite
   return [expression]
By default, parameters have a positional behavior and you need to inform them in the same order that they were defined.

Example

The following function takes a string as input parameter and prints it on standard screen.
def printme( str ):
   "This prints a passed string into this function"
   print str
   return

The calculator code here:

''' Program make a simple calculator that can add, subtract, multiply and divide using functions '''

# define functions
def add(x, y):
   """This function adds two numbers"""
   return x + y

def subtract(x, y):
   """This function subtracts two numbers"""
   return x - y

def multiply(x, y):
   """This function multiplies two numbers"""
   return x * y

def divide(x, y):
   """This function divides two numbers"""
   return x / y

# take input from the user
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")

choice = input("Enter choice(1/2/3/4):")

num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))

if choice == '1':
   print(num1,"+",num2,"=", add(num1,num2))

elif choice == '2':
   print(num1,"-",num2,"=", subtract(num1,num2))

elif choice == '3':
   print(num1,"*",num2,"=", multiply(num1,num2))

elif choice == '4':
   print(num1,"/",num2,"=", divide(num1,num2))
else:
   print("Invalid input")
Output
Select operation.
1.Add
2.Subtract
3.Multiply
4.Divide
Enter choice(1/2/3/4): 3
Enter first number: 15
Enter second number: 14
15 * 14 = 210
In this program, we ask the user to choose the desired operation. Options 1, 2, 3 and 4 are valid. Two numbers are taken and an if...elif...else branching is used to execute a particular section. User-defined functions add()subtract()multiply() and divide()evaluate respective operations.
If you have any doubts regarding the above code Feel free to comment in the comments section provided below.
Thank you! Stay tuned on this blog for more posts.

Comments

Popular posts from this blog

Kotlin :A New Beginning

Hello guys, You might have heard about a new language supported for the android Officially by Google,Named as Kotlin. Kotlin is a statically typed Programming language.By statically typed language i mean that the type checking is done during complile time. It was developed in July 2011 by Jetbrains in Russia,In 2017 Google declared Kotlin as the official langauge for android. However the Traditional Java developers are sticking to java. New programmers who want to make android apps,Web apps can use Kotlin as a language of preference. With this Post i welcome you to the world of Kotlin. There is no good book for kotlin but there is the official documentation on Kotlin.I refer the official Kotlin documentation. The main aim of this post if to Introduce you Kotlin and its Uses and also how to write programs in Kotlin. The official documentation can be found on the website   KOTLIN I belive that this post will help you to write codes in Kotlin. So lets begin Fir...

Assembly code generator for non parenthesized arithmetic expressions using Python Programming Language.

Hi guys, I hope you like the code I'm posting. This program is written in Python Programming language and it converts the non parenthesized arithmetic expressions to Assembly level code. Try a = b + c / d to test the code. And this should be the expected output: The Genereated code is: LD R0 ,c LD R1 ,d DIV R0 ,R0 ,R1 LD R1 ,b LD R2 ,temp ADD R1 ,R1 ,R2 ST temp , R1 Please Note: Do include a space (" ") between the operators and operands. And keep it in your mind that this is a code generator. Hence please do try it with valid expressions only. Upgrading the code for implementing the 'if' and looping constructs like 'while' and 'for' and sharing the modified code with us and other coding enthusiasts is really very well appreciated. I shall make another post about the explanation of the code soon. Until then, please do comment on our post and share our link to other coding lovers like yourself! I ho...

Using Java and Python simultaneously

Hi friends,                                                                                                                                    Firstly, I would like to thank you all for your love and support towards our blog.                   We here are happy to know that our previous posts on tutorials and mini projects have helped you a great deal.         So far many of you might have done some really amazing projects using a particular programming language. But in this post of mine, I would like to share with you my mini project for which I have collaborated two of the most am...