liveBook · Manning

Keyword

As explained in Get Programming: Learn to code with Python

A keyword is a reserved word in a programming language with a predefined syntactic meaning.

A keyword is a reserved word with a predefined role in a programming language. Because Python assigns keywords special meanings, you cannot use them as variable names. For example, if begins a conditional statement. section 13.2

Notice how different keywords begin different language structures:

  • if starts a conditional statement that checks a Boolean condition and may execute an indented code block. section 13.2
  • class starts the definition of a new object type, such as Circle. section 31.1
  • while starts a loop that repeats its code block while a condition remains true. section 18.1

A keyword directing program flow

num = int(input("Enter a number: ")) if num > 0: print("num is positive") print("finished comparing num to 0") Python recognizes if as syntax, not as a variable name. It checks the condition, runs the indented line when the result is true, and then continues with the final print statement. section 13.2

Listing 13.1. Example of a simple conditional statement
num = int(input("Enter a number: "))               #1
if num > 0:                                        #2
    print("num is positive")                       #3
print("finished comparing num to 0")               #4

Use the listing to trace how the if keyword separates the condition, the indented code block, and the statement after it. section 13.2

How Get Programming: Learn to code with Python covers it

How deep it goes
Defines reserved words and demonstrates three uses, but does not provide a complete keyword inventory or language-level implementation details.
Examples
Demonstrates if with a positive-number check, class with Circle, and while with a word-guessing game.
What it assumes
Assumes basic Python code, variables, input, conditions, and indentation while explaining keyword restrictions through concrete examples.
What you can do after
Evaluate Python code by recognizing reserved words and relating each keyword to the structure it begins.
How it connects
Connects keywords with variable names, conditional statements, classes, object types, Boolean conditions, and while loops.

Also explained in

Compare how 3 books explain keyword →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Get Programming: Learn to code with Python. The section links point to the parts of the book it draws on.