Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Changing Python Versions

00:00 Changing Python Versions. Sometimes code that works perfectly fine in one version of Python breaks in a newer version. This is due to official changes in language syntax, the most well-known of which is the print statement, which went from being a keyword in Python 2 to a built-in function in Python 3.

00:23 This is one of the examples where the error message provided along with the SyntaxError really does shine. Not only does it tell you that you’re missing parentheses in a print call, but it also provides the correct code to help you fix the statement, probably because this was an extremely common error for people to make during the transition to Python 3.

00:42 Another problem you might encounter is when you’re reading or learning about syntax that’s valid in a newer version of Python, but isn’t valid in the version you’re writing in. An example of this is the f-string syntax, which doesn’t exist in Python before version 3.6.

00:59 So here, running Python 2.7…

01:07 As you can see, in Python 2.7 the f-string syntax is not present and it just provides a generic invalid syntax message. The problem, in this case, is the code looks perfectly fine but it was run with an older version of Python—2.7, in this case.

01:24 So when in doubt, double check which version of Python you’re running. Python 3.8 introduced SyntaxWarning. You’ll see this warning in situations where the syntax is valid but it still looks suspicious.

01:37 An example of this would be if you were missing a comma between two tuples in a list. This would be valid syntax in Python versions before 3.8, but the code would still raise a TypeError because a tuple is not callable.

01:53 The helpful message accompanying the new SyntaxWarning even provides a hint, perhaps you missed a comma, to point you in the right direction.

Become a Member to join the conversation.