#
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. Open it up in your editor, let's run it and make sure that it runs.

0:11
Okay, it looks like there's one failure, that's because there's one test function, cool. Let's go through it. Lazy range, get the 100th item

0:20
from the vals variable, store it in the variable named hun. So we have a vals variable here and it's range from 42 up to but not including two million

0:30
jumping by 32 and we want the 100th item here, let's try and see if we can slice it off hun is equal to vals 100, let's run that and see if it works.

0:43
Okay, it looks like that worked, so the range function even though it's lazy in Python 3, allows you to do some slicing off of it,

0:53
let's do a map, find the 100th item from mapping fn, which is this function right up here, to vals using the map function

1:02
store the result in hun function. So we say map we're going to map a function fn to vals. And we want the hundredth guy from that.

1:13
So hun_fn is equal to that and let's see if we can slice off the hundredth guy. We'll run it and we get an error, map is not subscriptable.

1:24
So even though range is lazy, map is also lazy, but map doesn't support this index operation, so what we would need to do to get the hundredth item

1:39
is do something like this, seq is equal to the map of that, and then for i in range 100 hun_fn equals next seq

2:02
let's see if that works and I have a typo here let's change that and run it again. Okay, and so it looks like that works

2:13
so this just shows you something that you may need to do in this case I need to jump through a little hoop

2:18
and call next 100 times to get this item from my sequence here because it's lazy. Now there are pluses and minuses to this,

2:28
in Python 2 map materialized the list for you and you can slice a list but if you have an arbitrary long list it might take a bunch of memory

2:35
so there are tradeoffs depending on what behavior also I could course this into a list and do that but again, we're going from 42 to 2 million

2:46
so this is pretty big I might not want to do that. Sorting, sort the nums list as if they were integers

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

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

3:05
Okay, so in Python 2, Python 2 would allow us to sort lists with arbitrary types and a Python 3 wants to be a little bit more explicit.

3:14
So if we just say ordered = sorted nums, let's run that and see what happens I get a type error, less than operation is not supported

3:29
between instances of string and int that's because I've got a string in here and I've also got integers in here.

3:35
So it wants me to sort these as if they were integers so one thing we can do is we can cast them to integers

3:42
so we could make a little for loop or do a list comprehension and cast them to integers. But they key parameter in the sorted function

3:51
will allow us to apply an arbitrary function to an item that needs to be sorted and will sort based on that.

4:02
And this gives us the original ordered will now give us back the original list but sorted as if they were integers. So let's run and see if it works.

4:13
Okay, it looks like that worked. So this is sorting that list as if they were integers. Name leakage, sum the square of the numbers in nums,

4:24
store the result in square sum so I'm going to put them right here in this space

4:30
and note that I've got some variables here that are just floating around that maybe someone created or maybe I created if I'm typing code

4:39
and if I want to square a bunch of numbers and I've got them in sequence, one way to do that is to use a list comprehension,

4:47
I can say nums is equal to, or I've already got nums, so maybe sq is equal to num squared for num in nums. And then I want to sum that,

5:05
and so I'm going to say sq.sum is equal to the sum of this whole guy here. Let's run that see if it works.

5:18
Okay, I got an error here, unsupported operand for ** a string and an int, again I've got this string in here, so maybe I want to corse these to ints.

5:31
Let's corse them to ints, and then do that see if that works. Okay, it looks like it worked this might just seem like a silly thing

5:42
but you'll note that in this case I used num as my variable in my list comprehension here

5:51
and in Python 2 if I used num here when I get out of this list comprehension the value of no will be the last value of this list comprehension.

5:59
In Python 3 that behavior is changed and there is no "leakage" of this variable into the surrounding scope here so num stays at 42.

6:10
So I put an X in here and a num in here in case you used those in a list comprehension.

6:15
Now if you did these in another way, if you made a for loop and used num in there then you would overwrite this num guy

6:22
so it's just something to be aware of if you're not familiar with that in Python 2 on list comprehensions,

6:28
note that generator expressions and set and dictionary comprehensions behave as in Python 3, there's no leakage there

6:36
but in the list comprehension there is. So this video showed some of the things that changed in Python 3, we have laziness as a general theme

6:46
so range and map are both lazy they support somewhat different interface so be aware of that.

6:54
Sorting, when you sort different types, you need to be specific and make sure that they support sorting

7:01
and so we can use the key parameter of the sorted function to help enable that and there's no name leakage in list comprehensions now.