Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Creation5:09 with Craig Dennis
Lists have a literal syntax much like strings. Let's explore!
Easter egg! In the
import this and you'll see
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than right now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
So you know how we can create a string by placing some characters in quotes?
Well, this is known as a string literal.
Well, there's a list literal as well.
And it's created by surrounding your items and hard brackets and
you can put your objects inside those brackets and separate them by commas.
Why don't you click the launch works space button before I say something offensive in
Okay, so let's get exploring.
My favorite place to explore is the REPL, so let's launch the Python shell, so
we'll type python.
All right, so let's get exploring.
So I can create new list with an opening hard bracket and a closing hard bracket
and that is an empty list, just like a double quoted empty string.
Let's look at how to add items to a list.
You really just put data in here.
Let's create another of these empty lists and let's put some data in there.
So, for instance, if I was making a list of programming languages that you
could learn here on Treehouse, now obviously let's start with Python.
And that's a string and then we can add a comma, to separate our values, right?
And of course we teach many more languages,
but let's just stop here for right now.
So I'm gonna press enter and you'll see that we got a list object created.
And as you know, we can label that object so that we can keep it around.
So let's create our variable and assign it a new list.
Let's keep with our languages idea, so let's say languages = and
we also teach C#, we teach Ruby, and we teach Swift and there's even more.
So, now that we have a list we can actually check and
see how many items it has.
Just like we use the LEN function to see how many letters were in a string,
we can use the same function to count the items.
So, we can say len of languages, and we should see 3.
Pretty cool, isn't it?
You can pass just about any object to that len function, and
it will most likely give you the answer that you're looking for.
There is a saying in the Python world, there should be one,
and preferably only one obvious way to do it.
And this definitely falls into that Zen of Python.
And therefore, it can be said that the use of the len function is Pythonic,
more in the teacher's notes.
Let's take a look at some coercion too.
So, you might remember that any non-empty string when coerced, to a Boolean is true.
Well, the same holds true for lists, right?
So, if we say what's the Boolean value of languages.
We'll see that it's true and just like how empty string coerce the false so
do empty lists.
So we say bool and we give that an empty list with no items.
It's gonna give us a false, emptiness is falsey.
Sometimes you'll see lists used used in an expression, like if list
this can basically be read like, if there are items in this list, then do this code.
You could also create a list from any iterable which we'll talk about more
here in a bit.
But one data type that we know for sure is iterable is a string, right?
We can iterate through each letter using the for n loop like the banner at a party.
So you can pass in an iterable.
So let's use it, so we'll do this.
We'll say banner = list and
let's say that make this banner was congratulations was the text there.
So we're gonna pass, we're gonna coerce an iterable,
a string iterable, into a list and watch what happens.
So now if we take a look at banner,
you'll see that each element of the list is a letter.
All right, so, I'm in the process right now of putting together a meeting
to talk about some future courses.
And I wanna invite some fellow teachers.
Let's do this, let's make a new file called meeting.py.
So I say a New File, meeting.py,
so what I'll do is I'll create a variable named attendees.
And I'm going to assign it a list.
And that list will have, it will have Ken, Elena, and Treasure.
Awesome, so now let's print out how many attendees we have.
So we'll say, print there are and
we'll just use this multi-parameter
version of print, attendees currently.
Let's go ahead, I'm gonna drop out of this and let's run that file.
So we'll say python meeting.py,
there a three attendants currently, awesome.
Now, one thing I'd like to point out is that lists are mutable,
meaning we can change them.
We can add an attendee to this list if we want to, and in fact,
Ashley just pinged me on Slack and told me she wants to be a part of this meeting.
So let's take a quick break and then come back and learn how to add items to a list.
You need to sign up for Treehouse in order to download course files.Sign up