Fundamentals of Programming 3 of n
10 min read
Here is where we left off.
To compute the profit for each new fruit sold, we copy pasted lines 1 to 6, changes all
new fruit sold. Notice a pattern here? Check of the
print_message variable. We are not creating a new variable for each new fruit we sell by are reusing the same and changing its value. Can we do the same for an array. YES!
One thing to note here. The fruit array has different types of information. The first 3 are numbers and the 4th one is a string. Python allows you to do this, but not all programming languages do. The goal here is not teach you programming language nuances, but to teach you the core concepts.
Notice we changed the name of the variable from
fruit. This makes the code generic for any new fruit we sell. Now for Oranges and Peaches, we simply copy lines 1 to 6, changes the values in the array and the printed message.
Now for each new fruit you want to sell, you need to change only 2 lines of code.
Although this is better, we lost a bit of readability in our program. Previously, the array variables were named
apple, orange, or peach. Looking at the array you could tell that the information contained inside in the array belonged to a type of fruit. That information can only be gleaned by looking at the value of the
print_message variable. Can we include the name of the fruit inside the array? Yes!
Look at the print message. It says
Profit Apples. We have the information of what is sold in the array. Can we use that and build the print message without having to type it each time for a new fruit sold?
In mathematics the
+ symbol is used to add 2 numbers. In programming we can use the same symbol to add or
join text together. If you have 2 string variables
variable1 with a value of
variable2 with a value of
World! you can make the string
HelloWorld! by "adding"
variable2 like this
print(variable1 + variable2)
Notice a space
` after the wordProfit` to make the printed message look like a regular statement. Now when you sell different fruits, you just copy lines 1 to 6 and change 1 line of code instead of 2.
Before we get to the next important concept, a short lead up.
A computer program exists to serve a purpose. In our example it is helping compute the profit or loss of a fruit sale transaction. The program uses the information it has to perform the computation. The information is often referred to as
data and the computation as
operation(s). It is called operation because not all steps a program performs are pure mathematical computations. Some are decision tasks.
If you look at our code the information is available in the various
fruit arrays. Line 3 is where we are processing the data (information) to get new information. It would be nice if we can separate out the data and the processing logic into different areas for better code readability. Remember the code we write is read by other programmers and we should strive to make is as readable as possible. Let's move all the data lines to the top of the program.
If you run the program you see
Peaches printed out 3 times. We have a problem. What's happening is we are using a single array variable
fruit created on
line 1 and immediately changing the values on lines
2 and 3. Essentially, when the program gets to the processing part, it just sees
So what we need is a structure that can hold data for multiple fruits. You are a smart person. So you ask, can I create an
arrays? A multi-dimensional array?
You already know how to extract elements of an array. But if you look at what's printed, the entire array is. For profit computations, we need individual elements of the array? How do you access that? Use another set of
 as shown on
To access other elements of the array containing data for
apple, copy line 10 onto line 11 and change the values in the second
Now that we have learned how to create multi-dimensional array and extract data from it, let's compute the profit for apples.
This code is similar to the one you created earlier. You are accessing the
fruits and accessing various elements of the apple array via
fruits, fruits etc.
Onto oranges now. Copy lines 7 to 11 onto 13 and change all
And if you want to compute the profit for peaches, copy lines 7 to 11 onto 19 and change all
fruits. And as you add more fruits, you add another array to the
fruits array with the data for the fruit and then copy paste the lines 1 to 11.
Wait a minute. This is unfair you say. The computer was supposed to help you do more work with less effort. What you have been doing so far for each new fruit is a lot of repetitive work. Why can't the computer handle the repetitive work? Isn't is supposed to be good at this?
What we want is an ability to repeatedly run lines for code for each fruit array in the fruits array. We don't care how many fruit arrays are in the fruits array. For each such array we want to run lines 7 to 11. What this allows us to do is, when we have a new fruit to sell, we just add it as another array in the fruits array and not make any more changes to program.
loops. In computer programming, a
loop is a concept where you ask the computer to run one or more lines of code repeatedly. You are asking the computer to "loop" over the lines of code inside the loop. You don't want to run the loop forever. You need a way to stop the loop. The action of stopping the loop is often called
terminating the loop.
Each programming language has its own loop construct, but most of them are similar. How you write the code, often called
the syntax, might differ from one programming language to another, similar to how you express concepts in different human languages. Here is how you create a loop in
Python. Pretty straightforward.
Don't sweat the
syntax too much. We are here to learn concepts. What the code above is doing is this. For each fruit in our fruits
array we print out the
fruit array. So now when you have a new fruit all you have to do is add it to the array and the new fruit will be printed.
Armed with that, let's replace the
fruit is an array of single dimension.
Line 9 - 12 should be familiar to you.
Next up we will try to make this code more readable and remove some of the ambiguity. Stay Tuned!