Python has gained a lot of popularity and has become the go-to programming language because it’s easy to learn its syntax emphasizing readability and it’s a great tool for arithmetic expression.
In these tutorials, I will be showing you how to build a basic calculator with python. I will be showing you how to subtract 2 numbers, this tutorial will give you knowledge on how to get input from users. Once we have successfully built our calculator I will push my code to my git repository and I will show you how it is done.
Steps On How To Build a Calculator
Step 1: Creating Variables
The first we want to do is create 2 variables where we can store inputs from a user
num1 = input(“Enter a number: “)num2 = input (“Enter another number: “)
Step 2: Inputting Our Variables
They are two way to go about this, with either function int or float
the int function can only print out whole numbers, so once there’s an input with a decimal result is an error code. While with the function float () converts stored in a string into a decimal point; that means any number inputted or stored the function will interpret it with it having decimals (eg 10 =10.0)
My preferred method is using floats
num1 = input(“Enter a number: “)num2 = input (“Enter another number: “)result = float (num1) — float (num2)
Step 3: Print Out The Result
The print function is used to displace what we see on the screen i.e the final result/product
print (result)
When we join all these steps together
num1 = input(“Enter a number: “)num2 = input (“Enter another number: “)result = float (num1) - float (num2)print (result)
output
How To Push To Github
there are two methods using the command line or github and github desktop, for this tutorial I will be using github and github desktop
- download github for desktop
- if you are having issues setting up your githhub, I recommend you read this article, it’s give you a detail step on how to setup.
- sign in into your github account or signup if you don’t have an account.
- set up an connect git to your visual studio code.
- there are two ways here on how to push your code to github, you can push it through visual studio code or you sign in into your github and push your code. for this toturial i will be signing into my github to push my code.
- go to your repository and start a new one
- create repository
here is the link to my code
I hope this helps you understand variables and the use of arithmetic functions in the next article we will be looking at how to use loops to create a calculator with multi functions.
Thank you for taking your time to reading
if you have a comment, suggestion or just want to discuss on python please feel free to reach out. i hoping to learn from you.
you can find me here for interesting topics and updates twitter