# Python tutorials #1 – basic calculator

This is the first of my Python tutorials! Python is a programming language but unlike C++ and Java, is easier to learn but can still be used to create amazing programs such as web browsers, calculators, word processors, and more. But unlike Scratch, a GUI used to make games and other projects aimed at beginners and young children, you still must master Python to create amazing applications using it.

So, here’s my first tutorial: a basic calculator that can be used to add, subtract, multiply, and divide two numbers. Unlike the calculator app preloaded on your PC or phone however, it is CLI based so you won’t have a friendly GUI calculator but rather one where you type two numbers, what you want to do with them, then get the result printed to the screen.

This guide assumes that you are using the newer Python 3.x. The code may work on Python 2.x, but has not been tested, so if you need to stick to Python 2.x for some reason, you may have to make adaptations to the code for it to work properly.

Fire up your Python IDE (such as IDLE3 or Geany) and enter in the following code:

``` first_number = input("Enter in first number: ") second_number = input("Enter in second number: ") print(int(first_number) + int(second_number)) ```

This basic block of code will ask you to enter in the first number, then store that first number, then enter the second number, store that second number, and then show the answer. The int around the variables converts them into intergers so they can be added together as numbers, not as characters. Without this 5 + 10 would equal 510, not 15.

This, however, is a little restricted. You can only add the two numbers, not subtract, multiply, or divide. Asking the user for what they want to do with the two numbers and then doing that expands the calculator’s functionality. For example:

``` first_number = input("Enter in first number: ") second_number = input("Enter in second number: ") option_choice = input("Add, subtract, multiply, or divide? ") if 'dd' in option_choice: print(int(first_number) + int(second_number)) elif 'ubtract' in option_choice: print(int(first_number) - int(second_number)) elif 'ultiply' in option_choice: print(int(first_number) * int(second_number)) elif 'ivide' in option_choice: print(int(first_number) / int(second_number)) ```

We are not doing if statements to see if, for example, option_choice equals ‘add’ as Python is case sensitive in this area. Checking for ‘dd’ (end of ‘add’) ensures that the program will work regardless of whether A is capital or not.

So there you have it! A basic calculator made in Python. Currently lacks support for decimals however, so if you try entering decimals the program will error out. This guide is aimed at beginners, so this calculator will likely be expanded on in a future guide. Enjoy!

-Chas 😎