What do the Python VSCode syntax colors mean?

Question:

VSCode has syntax highlighting for the Python language. I can’t find any explanation for how each token is classified and organized into colors.

Specifically, why are some keywords the same color, but some are different?

Looking at this code:

Code example in VSCode

for...in shows the keywords as the same color, but if...in shows the keywords as different colors. Why does in change color? What determines if a keyword is purple or blue or something else?

Finally, is there an exhaustive list or chart for how keywords are organized?

Asked By: Trevin Avery

||

Answers:

They are not the same in. The first is used to iterate while the second is an operator.

The color of the code in VSCode is provided by the theme. (VSCode uses its own theme by default.) Reference: syntax-highlighting-optimizations and theme color.

Using different themes can make the code display different colors. You could also set the color of custom code. Reference: Customizing a Color Theme.

Answered By: Jill Cheng

From the first link in @JillCheng’s answer I found the Scope Inspector, which can be enabled by running Developer: Inspect Editor Tokens and Scopes in the command palette. This will then show how the currently selected text was tokenized.

for...in shows both keywords are tokenized as keyword.control.flow.python.

if...in shows if as keyword.control.flow.python and in as keyword.operator.logical.python.


Note: This answer was originally posted as part of the question.

Answered By: wjandrea