Dealing with Data

Dealing With Data

This is Part V of Understanding Programming: A Primer, an exclusive series for EDUKWEST designed to teach you the basics of computer programming. This series is designed to be a tutorial and you will be the most successful if you work through the tutorial with your text editor open. Key in the code as it appears and try to make it run correctly yourself. You’ll find that by the end of this series, you’ll be able to develop simple programs and understand the process of computer programming.

In the last article in the series we examined control structures– conditionals and loops— that are critical in most programming languages. They allow your programs to make decisions based on the criteria that you set as the developer. In this article we’re going to switch gears and examine data and how it’s stored. We’ll take a close look at three data structures: lists, tuples and dictionaries. You may not have given much thought to data before. The easiest way to conceptualize data is as organized information.

Different types of information lends itself to being organized different ways.  For example, if you your data is a grocery list, then a list would be optimal.  However if you wanted to store student grades on different assignments over a period of time, a list wouldn’t work well as all.  There are many different data structures used by programmers and each is optimized for a different type of data.

Let’s start with that grocery list.

Lists

We’ve all made a grocery list before, but you likely haven’t stored it in Python code.  This following line of code will do just that:

groceries = [ “apples” , “potatoes” , “bread” , “milk” , “butter” , “oranges” , “salt” , “sugar” , “cereal” ]

Unlike variables which store a single unit of data, we’ve stored multiple elements in our list. I’m sure you can imagine a number of situations in which this can be convenient.  In this case, all of the members of our list are strings.  You might also create a list using floating point numbers, integers or some combination. Here is a list of floating point values.

gpas = [ 3.25, 2.26, 1.99, 3.55, 4.0, 3.21, 2.56, 3.06, 2.72 ]

Now, here’s where it gets more interesting.  Because lists are considered objects in the Python programming language there are a number of build in commands that you can use to work with lists. This is actually a really big deal because there are baked in functions that do things like count the members of the list or sort them.  You could do this with your own Python code, but, trust me, it would be a lot of work.

Note:  We’ll talk more about objects in the next chapter, so if this concept is confusing– Don’t worry.  More to come.

Before we get into the really fancy stuff, let’s take a look at how we extract data from the list. Key in the two lists above and a few more lines of code.


groceries = [ “apples” , “potatoes” , “bread” , “milk” , “butter” , “oranges” , “salt” , “sugar” , “cereal” ]
gpas = [ 3.25, 2.26, 1.99, 3.55, 4.0, 3.21, 2.56, 3.06, 2.72 ]
print groceries[0]
print groceries[4]
print gpas[3]


Once you’re sure you’ve typed the code correctly in your text editor, execute the code with the Python command.  You’re output should look like this:

Figure 1

Figure 1.1:  Python list program output using the Koding .com environment

Pay special attention to the print statements.  You’ll note that each print statement references one of our lists and a specific index within that list surrounded by brackets.  Lists in Python, like most languages, are indexed, meaning that each member of the list is assigned a number used to reference it. The first member of a list is always index zero.

Our grocery list would have the following indexes:

Index Member
0 apples
1 potatoes
2 break
3 milk
4 butter
5 oranges
6 salt
7 sugar
8 cereal

When combined with the print command the general form of retrieving data from a list is as follows:

print listname[index]

If you’ve worked with other programming languages, you may think that lists are very similar to the concept of arrays– and you’d be correct.

One thing to keep in mind is that your list is only as long as your make it.  If you try to access a non-existent list index your program is going to exit with an error.  Let’s add one line to our current program:

print gpas[17]

When you run the program you should see output similar to the following:

Figure 2

Figure 1.2:  Using an index that is out of range results in an error.

Editing List Members

You can also use the index of a list member to change the values of different members in the list.  This is done with an assignment statement as if assigning  a value to a standard variable.  The following statement will change the zeroth index of the gpa list:

gpas[0] = 4.0

Note that the indexes of the other members of the lists are not affected.

Using List Functions

Earlier I mentioned that lists are objects, which allows you to use a number of functions associated with the list object.  You might want to take a look at the official Python documentation and see the functions that you can use with lists.  The documentation is available at https://docs.python.org/2/tutorial/datastructures.html.

Let’s add two more lines of code to demonstrate the use of the append() function.

groceries.append(“chicken”)

print groceries

We use the append() function with the dot notation and associate it with the list name.  In this case we’re adding “chicken” in the first available ined of the groceries list.  The length of the list is effectively expanded with the use of the append() function.

Once you add the new code to your program, run the program in your browser and you should see output similiar to the following screenshot.

Figure 3

Figure 1.3:  Note that when output with the print command, ‘chicken’ appears at the end of the groceries list.

Let’s try one more list function.  The sort() function does exactly what you think it does– It sorts the members of the list.  It’s usage is similar to usage of the append function.  Add the following two lines of code to your program:

groceries.sort()

print “Sorted: “ , groceries

Run your program with the Python command.  Your output should look something like this:

Figure 4

Figure 1.4:  After the sort() function runs the groceries list is reindexed.  Apples is now in the zeroth index, bread in the first index and so on.

Tuples

Tuples are very similar to lists with one important exception: Tuples are non-dynamic.  Once a tuple is defined the values within it cannot be changed. Let’s start a new program in which we’ll define a tuple.


widths = (600, 800, 1024, 1280, 1366, 1920)
print widths
print widths[2]
widths[0] = 500


Run the code using the Python command and carefully examine the output.

Figure 5

Figure 1.5 : Note that the we’re able to access the complete tuple, a single index within the tuple, but when reassigning a value within a tuple, an error is generated. A tuple is non-dynamic and once initiated, the values within it cannot be changed.

Tuples are for storing data that does not changed over the life of a program.  For example in the program above, the screen widths are unlikely to change.

Dictionaries

Dictionaries are another data structure. Dictionaries are perfect for storing paired data, sometimes known as key, valuedata.  In this type of data instead of a numerical index, each data point is indexed by a key which is determined by the programmer.  State names and state capitals or towns and zip codes are both examples of where dictionaries can be useful to store data.

Consider the following program:


states = {      ‘Connecticut’ : ‘Hartford’,
‘New York’ : ‘Albany’,
                           ‘Mississippi’ : ‘Jackson’,
                           ‘Maine’ : ‘Augusta’,
                           ‘Montana’ : ‘Helena’,
                           ‘Texas’ : ‘Austin’ }

print states[‘Maine’]


When you run the program with the python command you’ll see the output “Augusta”.  Essentially the key (or index) for “Augusta” is “Maine”. Note the print command and how that’s used to access the value in the dictionary.

Just like lists Python dictionaries are an object and have functions that can be associated with them.  Let’s take a look at one of these functions.  Update your code so it appears as follows:


states = {      ‘Connecticut’ : ‘Hartford’,
‘New York’ : ‘Albany’,
                           ‘Mississippi’ : ‘Jackson’,
                           ‘Maine’ : ‘Augusta’,
                           ‘Montana’ : ‘Helena’,
                           ‘Texas’ : ‘Austin’ }

print states[‘Maine’]
print “Values:” , states.values()
print “Keys:” , states.keys()


Run your code on the command line using the Python command. Your result should appear similar to the following screenshot:

Figure 6

Figure 1.6:  Output demonstrating two of the functions used with dictionaries– values() and keys()

You’ll note that the values() function returns all of the values in the dictionary while the keys() function returns all of the keys.

This short tutorial can only give a high altitude introduction to working with data in programming. Almost every program, to some degree, works with data. Being effective at working with data is an important part of any programmers toolbox.

Please contact the author at mark@learntoprogram.tv  if you are interested in this and similar lessons for classroom use.

If you are interested in a formal version of this course for yourself or others, LearnToProgram has a special discount for EDUKWEST readers. Programming for Absolute Beginners is the starting point for our Development Club which provides over 30 self-paced courses, and as many live courses as you want to take.  For more information and to access the EDUKWEST reader discount, visit:

https://learntoprogram.tv/course/ultimate-monthly-bundle/?coupon=EDUKWEST

Mark Lassoff is founder of LearnToProgram, Inc., a Connecticut-based company that publishes online courses that teach mobile, web, and game development. Over 100,000 people have learned programming from Mark through online and live classes and free tutorials.