Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

GCD and LCM & New HTTP Status Codes

Here are resources for more information about GCD, LCM, and HTTP status codes:

00:00 In the previous lesson, I showed you the prefix and suffix removal functions for the str (string) library and the new graphlib topological graph sorting feature. In this lesson, I’ll be talking about changes to GCD, and the addition of LCM to the math library, and new HTTP status codes. Before I get going here, I want to warn you a little bit.

00:22 I had a horrendous experience in the eighth grade covering GCD and LCM. It went on and on forever, and the teacher was horrific, so if you hear a little tremble in my voice as I go through this, it’s PTSD from middle school. That being said, what’s a GCD? That’s the Greatest Common Divisor, or the largest number that divides two other numbers. For example, the GCD of 49 and 14 is 7.

00:51 7 * 2 = 14, 7 * 7 = 49, and no number larger than 7 goes into both 14 and 49. A common companion to GCD is LCM, or Least Common Multiple. This is the smallest number that can be divided by two other numbers.

01:11 For example, the LCM of 49 and 14 is 98. 98 is 49 * 2, and 98 is 14 * 7. No number smaller than 98 can be divided by both 49 and 14 evenly. GCD and LCM are both very common functions in cryptography.

01:34 The math library in Python has a gcd() function, but it didn’t have an lcm() function. The gcd() function in Python 3.9 is now capable of doing GCD of multiple numbers, and the lcm() function has been added to the library.

01:52 Let me demonstrate these two functions to you. You need to import them from the math library. Using the same numbers as before, calling gcd() on 49 and 14, you get the expected result of 7. Calling lcm() on 49 and 14, you get 98. Prior to Python 3.9, if you wanted to do the GCD of more than two numbers, you had to combine things.

02:15 You could either build yourself a little for loop, or you could use reduce() from the functools library.

02:29 reduce() calls a function—in this case, gcd()—on multiple numbers. It takes the GCD of 273 and 1729, computes the result, and then does the gcd() of that result and the next number in the list—in this case, 6048.

02:47 The end result is 7. As of Python 3.9, you no longer need to do this. You can just call the gcd() function directly.

02:57 When you call a web page on a web server, the content is returned back with some metadata. One of the pieces of that metadata is a response code. The collection of possible HTTP response codes is stored in the HTTPStatus object in the http library. In keeping up with this standard, some new codes have been added to Python 3.9.

03:19 103 Early Hints is a response header indicating that headers are coming before the final message. This allows a web server to send information ahead of time, reducing the amount of time that a user has to wait for the first amount of information to show up on the screen.

03:38 The ability for the browser to ask for early response headers introduces a possible replay attack, particularly during TLS handshake. In order to get around this, web servers also have the ability to tell a client to wait. The status code 425 Too Early is the server indicating to the browser that it’s asked for Early Hints too early, and it has to wait a little longer.

04:03 You can see these status codes by importing it from the library.

04:09 One of the codes that’s fairly familiar to web programmers is 200, which is Success.

04:15 That’s also known as OK.

04:19 By accessing the actual response and asking for the description, you get English text that explains what the response code is for.

04:28 Here’s one of the new additions, Early Hints—accessing it using its name, EARLY_HINTS.

04:36 There’s its value, 103, and you can also ask by number, using the callable on the object instead. Once you have the HTTPStatus object, you can use the number or the keyword to get at any of the response statuses, as well as their values and their English-language descriptions. One more status was added, but it needs a little bit of an explanation first.

05:00 A few years back as part of an April Fools’ joke, the IETF introduced RFC 2324—that’s the Hyper Text Coffee Pot Control Protocol. This protocol added new HTTP methods like BREW to control your coffee pot, and otherwise mostly just reused the existing status codes—with one exception.

05:22 Response code 418 I'm a teapot indicates that the device you’ve asked to brew coffee is a teapot—and you shouldn’t brew coffee in a teapot.

05:31 It would ruin the teapot! In the spirit of this silly little bit of fun, several libraries—including requestsand some servers have Easter eggs in them that will respond with 418.

05:43 If you want to see one of them in action yourself, go off to google.com/teapot. The IETF standards body attempted to remove 418, but as often happens on the internet, calmness happened—no, wait.

05:57 Outrage ensued. In response, 418 is now reserved. It doesn’t have to be implemented as I'm a teapot, but it can’t be reused for other things. And in case you were curious—yes.

06:10 HTTPStatus now includes 418 I'm a teapot. Let’s just hope that the Python people don’t go off and implement Internet Protocol over Pigeon—a previous April Fools’ joke.

06:25 That’s the last of the key parts of changes to Python 3.9 that I’m going to talk about. In the next lesson, I’ll briefly discuss how soon you should upgrade.

varelaautumn on Nov. 30, 2020

I was thinking all these updates in 3.9 seem kind of obscure and useless to me…

But that was before you told me they’ve added a status code for a teapot making an HTTP request.

Become a Member to join the conversation.