Fundamentals of Programming 3 of n


10 min read

Here is where we left off.

Screen Shot 2021-02-13 at 12.11.18 PM.png

To compute the profit for each new fruit sold, we copy pasted lines 1 to 6, changes all apples to 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!

Screen Shot 2021-02-14 at 1.53.32 PM.png

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 apple to 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.

Screen Shot 2021-02-14 at 1.56.09 PM.png

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!

Screen Shot 2021-02-14 at 2.03.08 PM.png

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 Hello and variable2 with a value of World! you can make the string HelloWorld! by "adding" variable1 and variable2 like this print(variable1 + variable2)

Screen Shot 2021-02-14 at 2.08.06 PM.png

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.

Screen Shot 2021-02-15 at 10.37.52 AM.png

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 peaches.

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 array of arrays? A multi-dimensional array?

Screen Shot 2021-02-15 at 10.46.54 AM.png

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 line 10

Screen Shot 2021-02-15 at 10.50.45 AM.png

To access other elements of the array containing data for apple, copy line 10 onto line 11 and change the values in the second []

Screen Shot 2021-02-15 at 10.55.07 AM.png

Now that we have learned how to create multi-dimensional array and extract data from it, let's compute the profit for apples.

Screen Shot 2021-02-15 at 10.55.07 AM.png

This code is similar to the one you created earlier. You are accessing the array for apple using fruits[0] and accessing various elements of the apple array via fruits[0][0], fruits[0][1] etc.

Onto oranges now. Copy lines 7 to 11 onto 13 and change all fruits[0] to fruits[1]

Screen Shot 2021-02-17 at 11.58.59 AM.png

And if you want to compute the profit for peaches, copy lines 7 to 11 onto 19 and change all fruits[0] to fruits[2]. 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.

Welcome to 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.

Screen Shot 2021-02-20 at 9.33.46 AM.png

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.

Screen Shot 2021-02-20 at 9.37.00 AM.png

Armed with that, let's replace the print statement with code to compute the profit. Remember, "inside" the loop you are handed a fruit array. So you don't need [][]. fruit is an array of single dimension.

Screen Shot 2021-02-20 at 9.46.28 AM.png

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!