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