In this lesson, we're going to talk
about the different data structures that you can use
when you're writing your own scripts and programs.
This includes data structures like JSON,
the JavaScript Object Notation format,
key value pairs, arrays, dictionaries,
Comma-Separated Values or CSVs,
lists, and trees.
The first data structure we have is known as JSON
or the JavaScript Object Notation format.
JSON is an open data file format
and data exchange standard that uses human-readable texts
to store and transmit data objects
that consists of different key value pairs and arrays
inside of that JavaScript notation.
JSON is very commonly used
in a lot of different electronic data interchanges
between clients and servers,
such as web applications with their servers
or different scripts with an underlying server.
JSON is a language-independent data format,
but it was originally developed for use by JavaScript.
These days though, JavaScript Object Notation or JSON format
is used by most modern programming languages,
including ones like Python, Ruby, and Perl,
when they're going to be able to send data
or receive data from different systems or different servers.
As that data comes in, it's easy to parse that format
and take the data and then load it up into other variables,
arrays, lists, dictionaries, and other data formats.
Let's take a look at a simple JSON representation of data
about a given person.
Here you can see there are a bunch of key value pairs.
Notice on the left side, we have the type of key,
and on the right side, we have the value.
So, firstName: Jason,
lastName: Dion,
isAlive: true,
children: true,
spouse: true,
address, and here we have an array
being kept inside of this key value pair.
Now, this array is going to contain
multiple sub-key value pairings.
For example, under address you might have,
streetAddress: 123 Main Street,
city: Davie,
state: Florida,
zip code: 33317,
and you can see how we break up these key value pairs
inside of this array
that is assigned to the address key value.
The second data structure we have are key value pairs.
Now, I've already showed you key value pairs
inside of the JavaScript Object Notation,
but let's cover what they are just a little bit more.
When you're dealing with key value pairs,
essentially this is going to be something on the left
defined as the key and something on the right
defined as the value.
In these key value pairs,
you are assigning some value to some type of title
or key that we might use as a variable name later on.
For example, in the JavaScript notation
that I just showed you, we had one piece of that
that was a single key value pair.
That single key value pair was, for example,
firstName colon Jason.
Jason was the value,
firstName was the key,
and you saw there were many different key value pairs
that were being shown inside that JavaScript data notation.
Now, it's also important to note
that when you are defining key value pairs,
you're going to do this in a particular format.
If you're using a string,
that value is going to be surrounded by quote marks.
If you're using a Boolean value like true or false,
you don't need those quote marks
because true and false are considered the data type
and the expected value to be received.
If you're dealing with something that's a number,
like a zip code, you can just put that as the number itself.
You don't need to put quotes around it.
When you enter numbers in this way without the quotes,
they are treated as integers,
and in that case,
you can use them for mathematical operations.
If you put quotes around them,
they're going to be treated as a text string
and you can't use mathematical operations on them.
The third data structure we have is arrays.
Now, arrays are a type of data structure
that are used to hold multiple values of the same type.
For example, if I wanted to hold all the different names
that a person has, such as their first name, middle name,
and last name, I could put that into an array
that contains three different values.
The first position will always be the first name.
The second position will always be their middle name,
and the third position will always be their last name.
That way, I can have somebody like John Michael Smith,
and if I want to call out the particular part of that array,
I can do that by putting in name bracket, one bracket,
and that'll pull out the second position,
in this case, Michael.
Now, why is the second position being pulled
when I use the number one?
Well, it's because computers always begin counting at zero,
so a three-item array is actually counted as zero, one, two,
for the first, second, and third position.
Normally, when you're setting the value of an array,
you're going to be setting those things as a list
with commas in between each value.
For example, if I wanted to set the name array,
I can set it as name equals John comma Michael comma Smith,
and that will put these things into position
zero, one, and two respectively inside of that array.
When you want to get data back out of an array,
you can do that as well
by simply putting the name of the array, in this case name,
and then a bracket
and the number that you want to get for the position.
So if I did name bracket two and end bracket,
that's going to be referring to the third name.
In this case, the person's last name, which was Smith.
The fourth type of data structure we have
is known as a dictionary.
Now, a dictionary looks a lot like an array,
but the difference is instead of storing a single value
inside of each position of the array,
we're going to store key value pairs.
So think about this like you would a phone directory,
you're going to have somebody's name and their phone number.
In this case, I wouldn't want to create an array
because I'd have different things in each position.
I might have one array for all of the names
and another array for all of the phone numbers,
but now I have to use both of those arrays
every time I want to find somebody's name and phone number.
Instead, it would be better to use a dictionary
where I have a key value pair stored
for that name and phone number.
For example, if I wanted to set up a dictionary
containing my phone book, I can do that
by typing in phone book equals curly bracket
and then each of the key value pairs separated by commas,
such as John colon 111-1111 comma,
then Mary colon 222:2222 comma,
Mark 333-3333
and then end the brackets at the end.
This gives me three people with their name and number
stored in a key value pair.
Now, if I wanted to find out information about Mark,
I could simply do that
by calling up phonebook bracket quote
and then the key value, in this case Mark,
and then end quote and end bracket.
That would return the value of Mark's phone number,
which would be 333-3333.
This is the idea of using a dictionary instead of an array.
I like to think about dictionaries
as essentially an array of key value pairs.
The fifth data structure we have is known as CSV
or comma-separated values.
Normally, comma-separated values are going to be stored
in a text delimited format
using a comma to separate each of those values.
Each line of that file is considered its own record,
and it's almost like a flat database.
You can read or write to these files
from many different programs, including Microsoft Excel
and many logging applications.
For this reason, it is a great way for you to send
and receive data across multiple systems
because almost every type of application
will support CSV import or export capabilities
instead of having to rely on a proprietary format
that may not work with other systems.
The biggest limitation with CSV files
is that everything is separated by commas,
and so if you have commas inside of the data itself,
this can actually throw off data inside of your records
if you don't escape those first.
Now, when you're dealing with CSV files,
these are very interoperable ways of working with things
because they can store things like numbers
and text in plaintext,
and you can have different areas for each of the fields
that all of those are separated by columns.
For example, let's take my phonebook example.
I can have a CSV file with all the people in my phonebook.
That would be John, Mary, and Mark.
With John, I would put John comma Smith comma 111-1111.
Then I go to the next line,
Mary comma Jones comma 222-2222.
Then I go to the third line,
Mark comma Williams comma 333-3333.
Now, I can import that data into any programming language
or script that I want,
and I can know when I read the first line
that is one record and it contains my first person.
I have three fields here, the first name,
the last name, and the phone number.
In some CSV files, you may use the first line
to actually define what those three categories are.
In this case, I might want to put
first comma last comma phone
and then have my three lines of data.
This way, whoever is importing that data will know
what columns to expect as they're reading the data,
and it's going to continually read until it reaches
that deliminator, which is the comma in a CSV file.
The sixth type of data structure we have is known as a list.
Now, a list works a lot like an array,
but the big difference is you can store different data types
inside of that list in a sequential manner.
When we used arrays, we had to have the same data type,
so everything in that array had to be a string
or had to be an integer or had to be a float,
but here on a list, I can actually mix and match things.
When you're using a list,
every element of the list is going to be called an index,
and the index values again start from zero
and goes up to the last element,
which is called the positive index.
Now, in some programming languages,
you can also use negative indexing,
which starts at negative one
and enables you to access elements from the last thing
to the first inside of that list.
Now, let's take a look at lists a little bit more
so we can understand how they work.
Let's say, for example, I'm going to create a list,
and I'm going to call it my_list equals bracket
and then I'm going to list out all
the data inside of my list,
so I might have things like one comma two comma three comma
quote example, end quote comma 3.14 bracket.
Now here I have five things in my list.
Notice that in my list, I have three integers, one string,
and one floating point decimal number.
Now that I have things in my list, I can read that data
or replace that data using different commands
in the programming language.
When we get into our programming languages,
we'll talk a little bit more about how you can do this.
But for now, just realize you're going to call them
almost like you would an array.
If I wanted to get out the word example from this list,
I would use my_list bracket three end bracket
and that's going to reference the fourth position
in this particular list.
The seventh data structure we have is known as trees.
Now, trees are non-linear data structures,
and they essentially have a root and nodes.
The root is going to be the baseline
for which all the other nodes are going to originate from.
All these extra nodes that come off of it
are going to be considered other data points
that are available to us.
Now, again, this is going to depend
on which language you're using,
whether or not they support trees, but if they do,
we essentially have parents at the root,
and then it goes down to the next node, which is a child.
That child can have other children,
in which case it itself is considered a parent,
and those underneath it
are considered children to that node.
As we continue to go down these nodes,
we can go down to the last node,
which is called the leaves or the end nodes.
Essentially, these trees are used to create a hierarchy
that can be used in a lot of different
real-world applications.
If you're programming webpages, for example, HTML uses trees
to distinguish which tag comes under which block,
and so it knows exactly where it is
in the directory structure
when it's displaying a particular webpage.
There's lots of different uses for using trees,
especially when you're searching or organizing data,
but again, it is only supported
by some of the programming languages
and not all of them out there.