#
Python 3, an Illustrated Tour Transcripts

Chapter: Language syntax

Lecture: Walk-through: Other Changes

Login or
purchase this course
to watch this video and the rest of the course contents.

0:01
In this video we're going to look at other test.py.

0:03
Open it up in your editor, let's run it and make sure that it runs.

0:10
Okay, it looks like there's one failure,

0:13
that's because there's one test function, cool.

0:15
Let's go through it. Lazy range, get the 100th item

0:19
from the vals variable, store it in the variable named hun.

0:23
So we have a vals variable here

0:25
and it's range from 42 up to but not including two million

0:29
jumping by 32 and we want the 100th item here,

0:33
let's try and see if we can slice it off

0:36
hun is equal to vals 100, let's run that and see if it works.

0:42
Okay, it looks like that worked,

0:45
so the range function even though it's lazy in Python 3,

0:49
allows you to do some slicing off of it,

0:52
let's do a map, find the 100th item from mapping fn,

0:56
which is this function right up here, to vals using the map function

1:01
store the result in hun function.

1:03
So we say map we're going to map a function fn to vals.

1:09
And we want the hundredth guy from that.

1:12
So hun_fn is equal to that and let's see if we can slice off the hundredth guy.

1:18
We'll run it and we get an error, map is not subscriptable.

1:23
So even though range is lazy, map is also lazy,

1:28
but map doesn't support this index operation,

1:34
so what we would need to do to get the hundredth item

1:38
is do something like this,

1:43
seq is equal to the map of that,

1:47
and then for i in range 100 hun_fn equals next seq

2:01
let's see if that works

2:05
and I have a typo here let's change that and run it again.

2:09
Okay, and so it looks like that works

2:12
so this just shows you something that you may need to do

2:15
in this case I need to jump through a little hoop

2:17
and call next 100 times to get this item from my sequence here

2:23
because it's lazy.

2:25
Now there are pluses and minuses to this,

2:27
in Python 2 map materialized the list for you and you can slice a list

2:32
but if you have an arbitrary long list it might take a bunch of memory

2:34
so there are tradeoffs depending on what behavior

2:37
also I could course this into a list and do that

2:41
but again, we're going from 42 to 2 million

2:45
so this is pretty big I might not want to do that.

2:49
Sorting, sort the nums list as if they were integers

2:53
store the result in ordered, hint look at the sorted and the keys parameter

2:58
look at sorted and the keys parameter,

3:00
so there's a built in function called sorted and it has a keys parameter.

3:04
Okay, so in Python 2, Python 2 would allow us to sort lists with arbitrary types

3:11
and a Python 3 wants to be a little bit more explicit.

3:13
So if we just say ordered = sorted nums, let's run that and see what happens

3:25
I get a type error, less than operation is not supported

3:28
between instances of string and int

3:31
that's because I've got a string in here and I've also got integers in here.

3:34
So it wants me to sort these as if they were integers

3:38
so one thing we can do is we can cast them to integers

3:41
so we could make a little for loop or do a list comprehension and cast them to integers.

3:47
But they key parameter in the sorted function

3:50
will allow us to apply an arbitrary function to an item that needs to be sorted

3:58
and will sort based on that.

4:01
And this gives us the original ordered will now give us back the original list

4:05
but sorted as if they were integers.

4:08
So let's run and see if it works.

4:12
Okay, it looks like that worked.

4:15
So this is sorting that list as if they were integers.

4:18
Name leakage, sum the square of the numbers in nums,

4:23
store the result in square sum

4:26
so I'm going to put them right here in this space

4:29
and note that I've got some variables here that are just floating around

4:33
that maybe someone created or maybe I created if I'm typing code

4:38
and if I want to square a bunch of numbers and I've got them in sequence,

4:43
one way to do that is to use a list comprehension,

4:46
I can say nums is equal to, or I've already got nums,

4:52
so maybe sq is equal to num squared for num in nums.

5:01
And then I want to sum that,

5:04
and so I'm going to say sq.sum is equal to the sum of this whole guy here.

5:13
Let's run that see if it works.

5:17
Okay, I got an error here, unsupported operand for ** a string and an int,

5:25
again I've got this string in here, so maybe I want to corse these to ints.

5:30
Let's corse them to ints, and then do that see if that works.

5:37
Okay, it looks like it worked this might just seem like a silly thing

5:41
but you'll note that in this case I used num

5:46
as my variable in my list comprehension here

5:50
and in Python 2 if I used num here when I get out of this list comprehension

5:55
the value of no will be the last value of this list comprehension.

5:58
In Python 3 that behavior is changed and there is no "leakage"

6:02
of this variable into the surrounding scope here

6:06
so num stays at 42.

6:09
So I put an X in here and a num in here

6:12
in case you used those in a list comprehension.

6:14
Now if you did these in another way, if you made a for loop and used num in there

6:18
then you would overwrite this num guy

6:21
so it's just something to be aware of if you're not familiar with that

6:24
in Python 2 on list comprehensions,

6:27
note that generator expressions and set and dictionary comprehensions

6:32
behave as in Python 3, there's no leakage there

6:35
but in the list comprehension there is.

6:38
So this video showed some of the things that changed in Python 3,

6:43
we have laziness as a general theme

6:45
so range and map are both lazy

6:49
they support somewhat different interface so be aware of that.

6:53
Sorting, when you sort different types, you need to be specific

6:58
and make sure that they support sorting

7:00
and so we can use the key parameter of the sorted function to help enable that

7:05
and there's no name leakage in list comprehensions now.