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


