Build up your first Python Calculator — Tutorial for complete beginners
It’s much more easy than you expect!
Last time we’ve learned how to run Python with Colab within 1 minute. This time I’m gonna teach you how to build your own calculator, no experience is completely okay. It’s super easy and fun to make it! So Let’s start learning with me :)
Step1. New notebook project
Create a new notebook on Google Colab to start our new project!
Step2. Create Operation Functions
For a basic calculator, we must need the following operations:
- Addition
- Subtraction
- Multiplication
- Division
For each operation, we define a function that can take two numbers as input and return the result.
Step3. Tell user the instructions
Simply print out four operation that our calculator can do.
In this block, we first let the user to pick one operator then type in the numbers. Then, the user can input two numbers separated with a space. Finally, we compared the input choice op to call the corresponding functions and print out the answer.
While loop: allows code to be executed repeatedly in the block.
Split: a method that divides a string into a list. For example, it splits the string “1 2” into a list of [‘1’,’2’]. We can therefore put the input numbers into the corresponding variables.
Run the code and you can try it with yourself! Here is an example output.
The complete code is provided here as well. Feel free to comment below to let me know if you have any questions!