Python Coder | Simple | Data Structures | Explain | how | what | when


Do you want to move on to the neck level?
You understand strings and lists and even dictionaries, but you want to know
what to use, how to use it and when. To start off with what is a data structure?
It’s a type of store, a structure that holds items in Python. There are
different types of collections which is basically a form of storage that you can
add and remove items. Things like strings and lists we’ve also mentioned
dictionaries and sets and tuples. So strings you would think of normal
text that would be a string. A list, the same as in real life, a list, which really
is a collection of items. So here we have a collection of word,s if we had the
words with spaces like a normal sentence we would probably use a string, but in
this case we want the separate words, do you want a string or a list? Well in this
case we want the individual words we want a list. Here’s a collection of passport numbers so do we need a list?
Well the passport numbers need to be unique so we want a set so there’s no
duplicates, so not a list. Here’s some scores 81 79 73 and 78 but whose scores
are they? So in this instance, this case, we want the person’s name who got the score, so we can use a dictionary called scores that has if you like a label with their
name attached to the score, you can remember we have a dictionary of scores.
We mentioned sets which are like a list but they don’t have duplicates, you can’t
sort them. Tuples a bit like a list again don’t really have so much
functions and they are immutable, you can’t change the
items. So we have in computer science different data structures, not the data
structures we’ve seen like lists and dictionaries and strings that are built
into Python, but we do need these different types of storage, and they’re
based on real-life scenarios so for example a queue. With a queue of people we have
people join the back and then people leave off the front so that’s a queue. A
stack we add to a stake by put it on top and if we want to take something off we
have to take it off from the top. If we want whats on the bottom we have to take all
the things on top off first, that’s a stack. The heap is an ordered
queue by the way. So you think of like queue as someone entering it at the end
and back and exit in it and the front and a stack you enter you put on, the top,
and you take off the top, so these are slightly different and why is it
important? When you want to solve a problem you might need to store things
either temporarily over a long period of time. But you need the correct structure
the correct storage so what does that mean? OK. Let’s have a problem and try
to solve it. Here’s a maze. The entry is to the left as you look at it so we
enter and we choose our option either to go left or right and then we follow
through in the maze and then we have another option to go left or right
column through the maze again another option to go left or right
if we choose the wrong option we end up in a dead-end so we have to go back to
our last choice. Hopefully, at some point, we get the maze correct and we come
out the exit this is a quite simple example of a maze. So let’s have a look.
Here we’ve got three places where we make a choice of left or right. So we’ve
got one two and three then exit so we’ve got a choice of left or right left the
right and left or right. So we choose left then left again, then we get
blocked ,dead end, so we go back and choose right and then left again and
then we come out of our exit. Let’s have a look at our data structures what
about a stack? Well we started off going left,
then we went left again then that left was wrong so we take that out and put in
right ,then we go left and then we had the exit, so the stack worked we had left
left then we took her out right the left to finish with the solution left right
left. So in terms of what we’re doing just for the stack first of all we push
so when we enter something’s called push so we got to push in left are you’re
going to push in left again then we’re going to pop out left because it was
wrong so that’s exiting so it’s pop and then
we’re going to push right and then we’re going to push left again and then we
have our correct solution left right left. So we can push and pop into our
stack and it works. What about our queue? So here we’ve got a queue going from
left to right so left is at the front of the queue right is the back of the queue.
So we’re going to push in our left then we’re going to push in another left
we’re going to pop that out goes out the back then we’re going to push in a right
and then we’re going to push in a left that gives us our left right left.
But that wasn’t, that didn’t work the right way because with a queue
remember when we get something when something leaves a queue when something
needs a queue it leaves from the fry so we pushed in left from the end of the
queue so there’s no one in the queue, so that’s the first item in the queue, left,
then we push in left again so that’s the second left the second one in the queue.
So when something exits from the queue when it’s popped it comes from the front
when someone’s in a queue and they want to leave the queue they leave from the
front so that how a queue works, so that left it’s wrong, that’s wrong. So the other
method of using a stack would work but this method of using a queue is not
appropriate to solving the problem with the maze. OK I hope you understood that
it was an example of how different structures can be used for different
things. Trees and graphs. So here’s a tree we have a as at the top the root and it
has children B and C C has a child F and B has two children D and E this is also
a data structure you use quite a lot so you might say in real life of a family
tree or an organizational chart but essentially it has something in the top
and this has children, you can call these nodes, and each node can have children,
can I have one or more and we go down the tree. Now a graph is different a
graph if you like is like a tree but the children can go back up so here we’ve
got several nodes and they’re all connected so this is kind of like a
a cyclic graph like a circle. Here we’ve got another graph so some nodes are
connected there some or not but it’s not a tree there’s not one at the top some
going down. So therefore if you like a parent can have children and those
children could be parents of other children so it’s not really a tree it’s
a graph. So a graph we can connect between any node to another node. So
these are two data structures we haven’t seen
yeah but just wanted to show you them and in real life for example we can have
maps so if you want to connect cities in the world that would be a graph or even
a diagram of an underground is a graph, yeah you can think of the stations as
nodes and the lines the train lines or even bus lines or the connecting lines
between these nodes so the graph has nodes and in between lines called arcs.
OK, that’s it for part 1 we’re going to explain a bit about what we’ve
introduced a bit more in part two. I hope you enjoyed today’s video and join us
for part so thank you. Don’t forget to subscribe if you haven’t already
bye for now.

2 thoughts on “Python Coder | Simple | Data Structures | Explain | how | what | when

  1. A bit of a delay and only an introduction to this subject but we hope to add more information in part B of this video soon. Let us know what you would like to see by adding comments. Thanks for watching.

Leave a Reply

Your email address will not be published. Required fields are marked *