Fundamentals of Programming 2 of n
Let's sell some oranges too!
Now that you made tons of money selling apples, you decide to expand your business and want to sell oranges too. Here is where we left off.
The easiest way to add Orange profit computation is to copy lines 1 to 9 and paste on line 11.
If you look at the code you created you can't really tell if the first computation is for apples or oranges. The variable for the number sold says number_of_apples_sold
. For both apples and oranges. Also, when you run the program, you see Profit 2 times and you don't know which is which. These are easy to fix. Let's change the variable name and the message printed.
Much better. The program prints clear messages.
You make even more money and are ready to sell Peaches and even dragon fruit. You know the drill. Copy lines 1 to 9. Change the variable name number_of_apples_sold
to number_of_peaches_sold
and number_of_dragon_fruits_sold
, change the messages to Profit - Peaches
and Profit - Dragonfruits
.
But, you are a smart cookie and realize the futility of the above exercise as you become richer and add more fruits to sell. Technically you can continue copy pasting code. The program will work, but managing so many lines of code becomes really really hard and the chances of making a mistake is very high.
One trick you can employ is programming is to determine if there are any common patterns and then figure out if you can combine them into a repeatable set of code.
So what are we selling? Fruits. We buy a fruit at a price and sell them in certain quantities at a price for each. What are we computing? Profit. More specifically, profit of a sale. A transaction that has certain characteristics. Each sale has a fruit that we sell. To determine the profit (or loss) of a transaction, you need certain pieces of information. In the sale transaction there are 4 important pieces of information.
- Sale Price
- Purchase Price
- Number of fruits sold
- Fruit Sold (name of the fruit)
A transaction is a name we give to the action of selling fruits. It's an abstract concept. We recognize that a transaction has certain properties. We identified 4 such properties above. There probably are more, and they depend on what your needs are. In the current scenario, all we are trying to determine is the profit of a transaction.
In the current incarnation of our program we used 3 variables to represent each of the first 3 properties (sale price, purchase price and number of fruits sold) explicitly and the name was used implicitly. First set represents apples, second oranges, and so on. So far we have encountered 2 types of variables. One that holds numeric values and one that hold text or string values. Referring back to we see that for every fruit sold, we need a set of 3 pieces of information. We "encoded" this in 3 variables. Can we encode the information in a single variable?
The answer is YES WE CAN!
People, meet Mr. Array. In Python
you create a variable of the type array
by using the square brackets []
like this
arrayVariable = []
arrayVariable
is the name of your variable. []
represents an empty array. We have nothing in it. Let's put some items in it. For our profit computation we add the 3 pieces of information as 3 items to the array. The items in the array are called elements
of an array.
apple = [2, 1, 10]
In the example above we are creating an array variable
named apple
. The first element 2
represents the sale price
. The second element 3
represents the purchase price
and the third element 10
represents number of apples sold
. Let's check out how the program looks. Replace all the lines in your program on Repl.it as shown below. You are simply printing the array
your created.
To compute the profit, earlier we used variables. The formula for profit works with individual pieces of information. But now we have "encoded" the information as 3 elements of an array. How do we extract the individual pieces? You do so by extracting the item based on its position in the array.
A short detour. The decimal number system is based on 10 base digits. 0 to 9
. In most programming languages, positional counting is done starting from 0 instead of 1 (like IRL).
Getting back to our array, it has 3 elements. The 1st element 2
is at position 0
. The 2nd element 1
is at position
1 and the 3rd element 10
is at position 2. The total number of elements in the array is 3
but there's nothing at position 3
. The maximum position in the array is 1 less than the total number of items in the array. The number of elements in the array is also referred to as its length
. In our example the length of the array apple
is 3
.
To extract elements from the array, you use the array name followed by the square brackets []
and use the position value inside the []
. For example to extract the first element your write apple[0]
. Let's modify our program to print each element of the array.
Now that we learned to extract the individual pieces of information we can easily compute the profit like this
Let's update the program to compute profits for oranges and peaches. Copy lines 1 - 6
onto line 8. Change apple
to orange
. And again onto line 15 and change apple
to peach
. Your program should look like this.
The above method of copy pasting will work for a handful of fruits you sell. But even that is tedious. You have change several lines of code and if you make a mistake and forget to change an apple to an orange, your computation will be wrong.
In the next post let's see how we can reduce the number of line of code that needs to changes for each new fruit we add. Stay tuned!