Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Before we dive into Comprehensions, let’s quickly review the functionality that it’s designed to improve: loops.
Resources
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
[MUSIC]
0:00
Hey there, I'm Rachel,
a Python teacher here at Treehouse and
0:09
welcome to the Python
comprehensions workshop.
0:13
In this workshop, you'll learn about
one of Python's most powerful features,
0:17
comprehensions.
0:21
It's a concise and readable way to
create new lists, dictionaries, and
0:23
sets from existing data
without using loops.
0:28
It allows for more elegant and
efficient code and
0:32
is widely used in the Python community.
0:35
We'll start with the refresher on loops,
0:38
then we'll dive into the basics
of list comprehensions.
0:40
Once we've got the basics,
we'll explore conditional, dictionary, and
0:45
set comprehensions.
0:49
Finally, we'll book and
0:51
our learning with some challenges
that we'll walk through together.
0:52
By the end of this workshop,
you'll have a solid understanding of
0:56
Python comprehensions and how to use them
to write more elegant and efficient code.
1:00
Let's get started.
1:05
Before we dive into comprehensions,
1:07
let's review the functionality that
is designed to improve the for loop.
1:09
First, let's look at iterables,
this is the name given to objects
1:15
that have a defined sequence of values
that can be accessed one by one.
1:19
This welcome banner is a great
example of a string and
1:24
these name tags are a great
example of a list.
1:29
Using a Python for loop,
we can iterate through the objects and
1:33
access each value to perform an action,
like,
1:38
changing each letter to uppercase,
or capitalizing names.
1:42
To follow along, start the workspace
associated with this video or
1:49
download the starter files if
you'd like to work locally.
1:53
Let's open loops.py,
the first block of code is the for
1:57
loop to capitalize each name
in our list of name tags.
2:03
We have a variable holding
the original list,
2:09
and a new variable holding an empty
list ready to be populated.
2:12
Then, we have a full loop that
iterates through old name tags and
2:16
gives each name tag a temporary
variable name of name tag.
2:20
With each loop,
the name tag value is capitalized and
2:25
then appended to new name tags.
2:29
The second block of code is the for
2:32
loop to change each letter in
our welcome banner to uppercase.
2:34
We've got the original banner in
a variable called old_banner, and
2:40
a new_banner variable that is
initialized with an empty string.
2:44
Then, we have a for
loop that iterates over the old-banner and
2:49
gives each value a temporary
variable name, letter.
2:53
With each letter,
2:58
we add it's uppercase version to the
new_banner string using the upper method.
2:59
Let's run this file.
3:06
We use python3 and then the file name.
3:08
Now a handy command line tip for you, if
you enter the first part of a file name,
3:11
like 1, and then hit Tab, the command
line will auto-complete for you.
3:17
So let's run the file.
3:22
And we can see a list of capitalized
names as a result of the first loop, and
3:25
WELCOME printed as a result
of the second loop.
3:29
Now, would you believe it if I told you
that these full lines can be condensed
3:34
into just one?
3:38
That is what Python comprehensions is for.
3:40
In the next video, we'll dive right
into comprehension syntax, so
3:44
you can get started making
your code more efficient.
3:48
If this refresher was a bit overwhelming,
be sure to check the teachers notes,
3:52
you'll find some resources on for loops.
3:57
It's important to have a good
understanding of loops before proceeding
4:01
to comprehensions, so take your time and
we'll be here when you get back.
4:05
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up