Python Keywords and Identifiers

This is fundamental. Don't mess it up.

What are Keywords?

Keywords are the reserved words in Python. They have special meanings. You can't use them for anything else, like variable names. The interpreter needs them to understand your code's structure.

Think of if, for, while, def. They control logic and define parts of your program.

Most are lowercase, except for True, False, and None.

To see all keywords in your Python version, run this:

import keyword
print(keyword.kwlist)

Don't memorize the whole list. You'll learn them by using them. Just know they exist and what they are for.

Example:

This is how you use a keyword. def tells Python you're defining a function.

# Correct usage of 'def' keyword
def calculate_sum(a, b):
    return a + b

This is what you don't do. You can't name a variable def.

# Incorrect: Using a keyword as a variable name
def = 10  # This will raise a SyntaxError

What are Identifiers?

An identifier is the name you give to things like variables, functions, classes, etc. It's how you label your stuff so you can refer to it later.

Rules for Naming Identifiers:

There are hard rules. Break them and your code won't run.

  • Allowed Characters: You can use letters (a-z, A-Z), numbers (0-9), and underscores (_).
  • Starting Character: Must start with a letter or an underscore. It cannot start with a number.
  • No Keywords: An identifier cannot be a keyword.
  • No Special Symbols: You can't use characters like !, @, #, $, %.
  • Case-Sensitive: my_variable is different from My_Variable.

Examples:

# --- VALID IDENTIFIERS ---

# Standard variable name
user_score = 100

# Starts with an underscore
_internal_value = 50

# Contains numbers
player1_name = "Alice"

# All caps (usually for constants)
MAX_CONNECTIONS = 5

# --- INVALID IDENTIFIERS ---

# Starts with a number
# 1st_place = "Gold"  # SyntaxError

# Contains a special character
# user@name = "Bob"   # SyntaxError

# Is a keyword
# for = 10            # SyntaxError

Don't name your variables after built-in functions like list or print. While Python might let you, it's a terrible idea because you'll lose access to the original function. Your code editor will usually highlight these names to warn you. Keep names descriptive but short.

We will learn in the next lesson:

Scroll to Top