Fundamentals of Programming 1 of n
9 min read
This series is for absolute beginners. No assumption is made that you have any programming knowledge. I only ask you to be open to using Repl.it a simple online IDE (integrated Development Environment). I ask you to not be intimidated. Hopefully you can enjoy the ride.
Programming or coding is a means to an end. You don't write a computer program without a purpose. Most programming 101-level books jump into listing various things in programming and if you are not immersed in the world already, you feel lost. It's not you. It's them and how the information in presented.
Let's start with the problem statement.
You sells apples and want to figure out the profit (or loss) you are making. And you want to write a computer program to help you determine the profit/loss.
The first step is to understand the problem and come up with a solution. In programming parlance, the solution is often referred to as an algorithm. So what's our algorithm? From basic math/business class, profit is how much money you are left with after you sell something. If you sold the apple for more than what you bought it for, you make a profit. If you sell for less, you take a loss.
The algorithm boils down to this.
- Note the purchase price
- Note the selling price
- profit or loss per apple sold = selling price - purchase price
- Multiply the number from step 3 with the number of apples sold
Let's write some code. Don't be intimated. You are in command. The computer exists to serve you.
Login to your Repl.it account. You should see a screen similar to this
+ New Repl on the top left. You should see a popup like below.
Python from the first drop down. Click
Create repl And this brings you to the IDE like the one shown below
Do not be intimidated. You don't need to understand anything that's specific to
Python or programming. This IDE is a means to an end and our goal is to learn concepts.
To help you get familiar with the controls in the IDE, the most relevant ones for our discussion, let's perform the standard programming ritual of printing
In the middle part of the screen where the tab title says
main.py type in the following
Run button and you should see
Hello World! on the right side.
You are a programmer!!
Again, don't worry if you don't understand anything you did at this point. This is just a ritual. And like most rituals, we just perform it just because
that's the way! Just remember the statement
Now, let's get on with why we are here. We want to write a program that computes the profit (or loss) of selling apples.
We buy apples at $1 each and sell them at $2 each (yeah! 100% profit!). What is the profit when we sell 2 apples?
If this were a math assignment, you would write it out as
(2 - 1 ) x 2
In programming the
* character is used to denote multiplication. So this becomes
(2 - 1) * 2
"Hello World!" with
(2 - 1) * 2 and click
Run. You should see
2 printed on the right.
Without the context if anyone looks at
2 in the right most pane, they have no idea what it represents. Let's correct that. Add a
print("Profit") statement on line 1. Now when you run the program it will display
2 on two separate lines.
Next, let's say your friend also wants in on the fun. They say what if I sell 5 apples. You say, fine. Change the
5. When your friends looks at the program they cannot immediately figure out which
2 to change. Is it the first one or the second one? You know which one because you created the program. However, if it was a complex formula pretty soon even you won't be able to tell the various numbers apart.
We need to make the program clearer. More Readable. Anyone should be able to look at the code and deduce what's going on.
If you formulate the problem as a mathematical formula (remember algebra?), you express it like so
pl = (s - p) * n where
profit or loss,
purchase price and,
n represents the number of apples sold.
pl = (s - p) * n is not clear without a legend describing what each alphabet represents. You can probably make is clear like so
profit_or_loss = (sale_price - purchase_price) * number_of_apples_sold
You just learned one of the core concepts of programming.
variables. In mathematical formula above
n are variables. They are names we attribute to things we want to represent in a formula. Anyone who wants to solve the formula simply replaces variables with values they want to plug in. When programming, it is a best practice to use meaningful words as variables instead of single characters like you do in mathematical formulas.
Let's rewrite our program with variables. Type the lines as shown below. We will get to the red squiggles later.
Run button. Uh! Oh! Error!!
What's going on? We are using
Python as the programming language and
Python doesn't understand that you want to create variables. Each programming language has it's own rules on how to create variables. In
Python you make your intent known by giving the variable a value. The act of giving a variable a value is called an
assignment. You are assigning a value to a variable.
Variables can "hold" different types of information. Since we are dealing with numbers we assign numeric values to our variables. Add the assignment like so
Run. Bingo! Now we are back in business. Assign different values to the variables and check out the result. You can even assign values like
2.75. Can variables hold non-numbers? Of course they can. These types of values are called strings and are usually enclosed with
"". Remember the ritual? We had
print("Hello World!"). "Hello World!" is a string. Make the change like shown below to add a string variable to your program.
In the following posts we will work on expanding the program and learn other core concepts of programming. Stay tuned!