Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 4: Journal app and file I/O
Lecture: Lists and for-in loops
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Now that we have the structure of our app in place, let's start creating our journal. So there are many different types of data structures
0:09
from various simple ones all the way up to rich hierarchy of classes that we could use in Python to represent this,
0:17
so let's start simple here and as we get into more complex applications we'll do something more new ones. So for our particular journal here
0:26
we are going to use just a data structure called a list. A lists just hold on to a bunch of pieces of data in particular order
0:32
you can access them in order it automatically grows, so in Python we create lists like this.
0:40
Usually you will see it written like that, open close brackets, that will create a new empty list,
0:45
you may also see it by calling the class name initializer like so, that mean exactly the same thing. So here we have our empty journal,
0:56
and we are going to need to work with the data here and we are going to work with the data there so let's pass this along,
1:03
I'll just go down here and make sure we can pass that data, like so. Now for listing entries I'll just cheat for a moment
1:11
and we'll make this nicer but we'll just print it out like so and for adding entries let's do a little bit of input
1:17
so we'll say something like this text = input(”type your entry”) So we'll get this user input and we are going to put it into our data,
1:31
and the way we add a new item is we say append text now we probably want to check this input to make sure that something is there
1:37
but let's just do this for a moment, want to list or add entry, let's list and see what it looks like, there is our empty list,
1:45
and let's add in new items say this is my first entry, and now let's list it and see if it's there, excellent,
1:52
there is my first entry, let's add another, say this doesn't look hard at all, woho!! Now we can list them again,
2:02
so here is my first entry and this doesn't look hard at all and Python even uses the same trick in the case where we have quotes to put double quotes
2:09
to make it easier for us to understand, excellent. You can see we are gathering our data, obviously it won't persist across runs yet, that's coming up
2:17
but the other thing that is not nice is this is not how our user wants to see it, maybe a programmer but definitely not a user.
2:24
So, let's do something nicer on this part. And that brings us to working with loops.
2:29
Now we could write a while loop and use a counter or something like that that would be wrong, it would be what is called non Pythonic code.
2:37
In Python we have a really nice way to work with collections and loops and those are called for-in loops
2:42
so we'll say for and then you name the particular instance of the piece of data you are taking from the collections
2:48
so I'll call it entry in data and write it like this, so for item in collection like so, and now you just work with it,
2:57
so let's say we'll just print it out for a moment, so we'll just say print(entry) and let's make it kind of obvious what is going on,
3:03
so we'll say something like your journal entries, and let's run it again. Start by listing them, your journal entries,
3:10
there are none, great, let's add, say entry 1, add another entry 2, and now let's list them, ok, your journal entries, entry 1, entry 2, very cool ha.
3:21
Now, there is a couple of things we might want to do, we might want to change the order so we see the newest ones first,
3:27
that's not the way paper journals work but that's the way a lot of digital ones would work, so let's come over here and we can change the order,
3:35
there is a really easy way to do that, in Python. So let's call or little reversed collection entries and you can just say reversed()
3:44
and you give it a collection and you get the list back in a different order. So now, we should if we run it, so we get items back in that order
3:53
so I'll say thing 1, thing 2, when I list it we should get thing 2 and then thing 1. Perfect.
3:59
Now maybe I'd like to number these like your first entry is this, your second entry is that, your third entry is such and such.
4:07
So let's go back to our list entries here. Now in many languages they use a index space loop
4:13
what is often called the for loop and it starts with numbers and you kind of increment those numbers and that makes it harder to access,
4:19
harder and more error prone to access the actual entry but it does make it easy to number things,
4:26
you might think but we have to fall back to the some kind of while loop, with the counter, not true,
4:31
Python has a really nice way to work with the index and the item at the same time,
4:35
so we can go to a collection, and we come over here and say enumerate, and what this will do is it will modify the collection that is returned,
4:43
not just return the items, but to pair them with the actual indexes. So let's just print this out and see what we get,
4:49
it's not going to be the same as it was before. Just to say that three things here,
4:56
now when we list them out, you'll see we get this sort of parenthesis and the index and the item, now in Python these are called tuples,
5:03
so let's go back and modify our loop to work with those tuples. So we can actually define two local variables here,
5:10
and the index and the entry and Python will take those two items from the tuple and assign them to these two variables
5:17
so we can also go over here and change this print statement, so this time we shall have the slightly nicer print out,
5:29
let's say thing 1, thing 2, thing 3, now if I list them, you'll see the 0 to item is thing 3, because we reversed it,
5:39
the first item is thing 2 and the second item is thing 1. Now typically people don't use zero based indexes but that's the way Python works,
5:48
let's fix it up one more time here, let's add one to our index. All right so it's more friendly for humans. Your journal entries, number 1, thing 3,
6:03
number 2, thing 2, number 3, thing 1. All reversed, excellent.