Learning Python Through Projects Series 2 — Building a Simple Calculator #PythonbeginnerSeries
Welcome to the second of the “Learning Python Through Projects series”.If you have not seen the first article yet, I embed the link(here) as well as included the link at the end of the article for reference.
Let’s recap. So previously, we explored the concepts listed below while building the ‘ Madlib’ game.
1. How to comment in Python using the ‘#’ symbol.
2. How to use the input() function.
3. What a Variable is.
4. One basic Data type in Python- Strings.
5. And finally the print() function.
Great!
In this project, we will be building a “Simple Calculator”, while also exploring some further concepts on the Python Programming Language.
So we all do some basic calculations daily. You do not have to be a salesperson or someone who has a lot to do with balancing financial records. One way or the other, we all carry out these tasks daily, directly or indirectly. Either through analyzing daily expenses or money for groceries, for the bus/train/cab(taxi), for lunch, rent, for some treat or money for a pair of shoes. There is some calculation going at one point. Wouldn’t it be nice to have that process automated?
Although, the focus today is not to create an application. The focus is on gaining intuition on a few concepts in Python. Later on, we are going to revisit this project and improve its functionality.
For this project, we will be exploring the following concepts;
- Using the float() and int() function
- Arithmetic Operators
- Expressions
- String Concatenation
Just like earlier done for the previous project, we will first get into building the project, Then explain the concepts afterward.
Let’s Dive in.
STEPS:
1. Open a code editor of choice. I will be demonstrating using the default Python IDLE.
2. Create a new file, save it as ‘simplecalc.py’
3. Create two variables using the input function. Include the float function outside the input function as shown below:
4. Using the print statement, include the following arithmetic expressions for Addition, Subtraction, Division(Regular Division, Floor Division and Modulus Division), Exponentiation, and Multiplication.
5. Save and Run the program.
Great Job!
Now Let’s take a look at the concepts used.
- The Float() function: So in the first project(link referenced below), we saw how to use the input() function to collect responses from a user and store in a variable for further use. Recall that the input function only outputs String-like Data type. So in order to convert that output into a numeric data type (either a float or an integer), an additional function need to be included to enclose the input function. The two Python functions which can be used here are either of the “float()” function or the “int()” function. For this use-case, the float function was used, however, it can also be replaced with the “int()” function if preferred. The int() function is short for ‘integer’. These are the two Basic numeric data types in Python used for Arithmetic Operations.
Python automatically recognizes numeric datatypes, just like every other programming language. Hence, we do not need to enclose them in quotes as for the String Datatype. However, if a numeric value were to be enclosed in quotes, Python Automatically recognizes that as a string and no more a numeric value.
*Take Note that Arithmetic Operations can only be carried out on Numeric Datatype*. Although, there is an exception. We will see that soon.
So in Python, there are seven(7) basic Arithmetic Operators. The Addition, Subtraction, Multiplication, Exponentiation, Divison, Modulus Divison, and finally Floor Division. Let’s Take a Look at each of the operators.
- Addition Operator: This is represented by the plus ‘+’ or addition sign in Python. It is used for carrying out addition operations.
- Subtraction Operator: represented by the hyphen(-) symbol in Python, it is used for basic subtraction operation.
- Multiplication Operator: represented by the asterisk(*) symbol and not ‘x’ symbol in Python. It is used for basic multiplication operations.
- Exponentiation Operator: represented by the double asterisk symbol (**) used for increasing a variable/numeric data type to a given number.
- Division Operator: represented by the single forward slash ‘/’ symbol. It is used for a simple division operation. Please take note that the backward slash ‘\’, signifies something else in Python, not division.
- Modulus Operator: represented by the percent symbol ‘%’ in Python. This is quite different from a regular division in Python. When this symbol is used, the remainder of the division operation is returned and the integer value is ignored. Take a look at the example below:
- Floor Division: This is represented by the double forward slash ‘//’ symbol in Python. Just as the name signifies, ‘Floor division’ meaning just the integer value is returned as an output, while the remainder or values after the decimal point is ignored. Take a look at the example below to see the difference in the output returned using the three division operators(/,//,%).
- Expressions: In Programming, expressions are statements that evaluate to some value. These statements could be string data types, variables, or numeric data types. They could also consist of operators such as the Arithmetic operators and/or the comparison operators. In Python, just like every other programming language, Expressions are the ‘simplest type’ of instructions. It is the basic building block of all programs. For example, simply typing ‘5 + 2’ on the Python Shell and pressing the Enter button is a simple Expression in Python. An expression with a command for a addition operation. Also typing ‘print(‘Hello World!’), is an expression.
For the print statements included at the end of the program, were expressions. Expressions for Python to carry out some basic operations with the Arithmetic operators included.
- String Concatenation: Finally, we will be talking about what it means to concatenate strings in Python. Remember when said earlier that Arithmetic operators(+, -,*,**,/,//,%) cannot be used on String Data type, but there is an exception? Well, The exception is called ‘String Concatenation’. For this operation, only two operators can be used on strings, the addition operator ‘+’ and the multiplication operator (*). The addition operator works by adding two strings together, while the multiplication operator replicates a string to a certain number. Let’s see how this works.
For the example above, notice two strings ‘Hi’ and ‘Diyyah’ were added together using the Addition operator.
When using the multiplication Operator, a String and a given number is used, never two strings together. Take a look at the example below:
Great! I have included the link to the Github repository for the code used below. In the repository, you will also find further examples included, such as calculating the area/volumes of shapes in Python, conversion from a unit in temperature to another et cetera.
Also, find below, links to further resources as well as the first article for this series. I hope you find these resourceful.
Thank you for reading!
First Article on This Series:
Find Me on Social Media:
Twitter: @diyyah92
LinkedIn: https://www.linkedin.com/in/aminah-mardiyyah-rufa-i
FURTHER READING