Fundamentals of Programming 4 of n
Here is where we left off
We created a program to compute the profit (or loss) we incur selling various fruits. We separated the data
our program uses from the operations
it performs. The data
is captured in an array
of arrays
(lines 1 - 6). The operations
are on lines 8 - 12. The data
for ALL the fruits is in the array named fruits
. Inside the fruits array, we have an array for each fruit we sell. Each fruit array contains information or data for a kind of fruit. The data for fruit that we are interested in are the selling price of a fruit [0]
, the purchase price of the fruit [1]
, the number of fruits sold [2]
and the name of the fruit [3]
. []
is how we extract data for each fruit from the array.
How the data for each fruit is "encapsulated" in the fruit array is known to you and me. Because we created the program. If we hand this program to a friend, they have no idea how the data is arranged or what the program does or how it works.
There are a couple of ways in which we can make our program comprehensive. The simplest way is to add comments
or notes
to our program. Comments
are lines of text that are legible to a fellow human but not to the computer. In addition, we need to tell the computer to not try to run our comments like it does real computer code. Different programming languages use different techniques to mark lines in a program as comments. Python
uses #
. C#
uses // and /* */
. Let's add some comments.
Lines 1 - 5 are comments. Each line you want to exclude from being executed starts with the #
character.
Adding comments sure does help in comprehension. However, you may have to refer back to the comments each time you are unsure which element of the array represents a piece of information. For example, if you need to extract the name of the fruit, you need to remember to use [3]
. Would it not be easier to refer to the element by a name? You can consider the array
as a key-value
map. The index
of an element is the key
and the item
at that index
is the value
. The value
in the fruit array for the key3
is the name of the fruit. Most programming languages offer a data type that allows you to store key-value
pairs. Such a data type is called a Dictionary
and is similar to a word dictionary. A typical dictionary is an order list of words, orders by alphabetical order, with meanings. The word is the key and the corresponding meaning(s) is the value. In Python
dictionaries are created like shown below.
fruitDictionary = {
"name" : "Apple",
"Selling Price" : 2,
"Purchase Price": 1,
"Number Sold" : 5
}
And to extract an item from the dictionary, we use the name within []
similar to how we used indexes.
# extract the name of the fruit
fruitDictionary["name"]
Now let's change our code to use dictionaries instead of arrays.
The program is more readable. Notice that we used an array (fruits
) to store each fruit. Can we use a dictionary? Unfortunately not. This is because, a dictionary has to contain unique key
s. A key
may not repeat. If you think about it, it makes sense. A key be definition is a crucial piece of information that references a unique fruit. You can technically force the fruits
dictionary to contain unique keys. But that goes against the principle of using the right tools for the right thing. In our case, we need a data structure to hold the list of fruits. An array is a perfect tool in this case. Next, we need a data structure that we can extract bits of fruit data for computation. An array can work but is not the right tool because it holds lot of implicit information. A dictionary is perfectly suited to hold explicit information and offer the ability to extract information by name instead of indexes.
The syntax of extracting the keys and values from a dictionary in a loop differ from programming language to language. Here's how you do it in python
The variables
to extract the key
and value
don't have to be named key
and value
. They can be more descriptive.
Much cleaner. Thus far, we have learned a few concepts in programming. We learned about variables
and loops
. We learned there are different types of variables
, like int
, string
, array
and dictionary
. Each variable type is used to "encapsulate" information or data. Simple variables like int
and string
store simple values. Complex variables like array
and dictionary
store more pieces of information. Next up we will learn of another important concept. Stay tuned!