Python 3, an Illustrated Tour Transcripts
Chapter: Language syntax
Lecture: Strict ordering
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Another thing to be aware of is how Python handles comparisons in Python 3. In Python 2 you could compare an integer and a string
0:10
and Python would have some arbitrary, well, not arbitrary but to humans perhaps seemingly arbitrary or confusing ordering for those.
0:19
In Python 3 we're a little bit more strict and Python is going to complain about those if I try and compare those we'll get a type error
0:26
and it says that that operation is not supported between those two. So if you've got something that you need to compare
0:32
typically you'll use the sorted function and you'd pass a key function in there and the key function would do some conversion
0:42
to another type that would allow you to compare these so I could compare these as integers, I could also compare them as floats.
0:48
I could compare them as string, that sort of thing. In this course, we've talked about dictionary ordering
0:54
and how in Python 3.6 as an implementation detail in the CPython interpreter dictionaries maintain the key insertion order.
1:02
In Python 3.7 this is actually going to be part of the language what that means is that any other implementations of the interpreter
1:10
should follow suit and also sort keys as well. In Python 3.6 they don't necessarily have to but people are taking advantage of that.
1:18
They like the functionality so that, they like the functionality. In Python 3.7 this will be part of the language proper.
1:23
We just got an example here of a dictionary that has name, age and address in there note that name came in first and age and address
1:29
and we would print out the dictionary or if we loop over the keys, we will get them back in the same order of that insertion.