Python Keywords and Identifiers

To write any Python code, you must understand the difference between two fundamental types of "words": Keywords and Identifiers.

This guide will make you an expert at both.

What Are Python Keywords?

A keyword is a reserved word in Python that has a predefined meaning and purpose. It helps the interpreter understand the structure of your program.

This means you cannot use keywords as names for your variables, functions, or any other custom identifiers. For example, you cannot create a variable named if = 10 because Python reserves that word for conditional statements. If you try, Python will raise a SyntaxError.

Exercise 1: See All The Keywords

Python's list of keywords is small and manageable. You can see it at any time! In the editor below, import the built-in keyword module and print the keyword.kwlist.

# 1. Import the 'keyword' module # 2. Print the keyword list using keyword.kwlist
# This is the example solution
import keyword
print("All Python Keywords:")
print(keyword.kwlist)
Your output will appear here...
Analysis: You'll see a list of all the reserved words. You'll recognize some, like True, False, if, else, for, and while. You can never use any of these words as a variable name.

Want to master Python fundamentals? This lesson is part of our Free Python course covering variables, data types, functions, and more.

Explore this free Python course →

What Are Identifiers?

An identifier is a custom name that you, the programmer, create to identify a variable, function, class, or other object.

Identifies are the labels you create to make your code readable and organized. They are the names you choose for your data and logic. For example, user_age, calculate_total, and Car are all valid identifiers.

The Rules for Naming Identifiers

Python has specific rules for what constitutes a valid identifier. Your code will not run if you break them.

  • Use only letters, numbers, and underscores: An identifier can only contain letters (a-z, A-Z), numbers (0-9), and the underscore character (_).
  • Start with a letter or an underscore: An identifier cannot begin with a number.
  • Avoid using reserved keywords: An identifier cannot be the same as any word on the keyword list (like for or class).
  • Treat names as case-sensitive: Python considers my_variable and My_Variable to be two different identifiers.

Exercise 2: Testing the Rules

Let's put these rules to the test. We have set up three distinct tests below. Some are broken on purpose to show you the errors. Run them to see what happens!

Test 1: The Number Rule

Rule: Identifiers cannot start with a number.
Run the code below. You will see a SyntaxError immediately.

# INVALID: Starts with a number 2nd_variable = 20 print(f"The value is: {2nd_variable}")
# Explanation:
# This fails because identifiers cannot start with a digit.
# FIX: Move the number to the end.
variable_2nd = 20
print(variable_2nd)
Your output will appear here...

Test 2: The Keyword Rule

Rule: You cannot use a reserved keyword (like try, if, class) as a name.
Run this code to see the error.

# INVALID: Uses a keyword try = 30 print(f"The value is: {try}")
# Explanation:
# 'try' is a command in Python. You cannot use it as a name.
# FIX: Change the name.
my_attempt = 30
print(my_attempt)
Your output will appear here...

Test 3: The Case-Sensitivity Rule

Rule: Age and age are different things.
Run this code. It is valid! It proves Python sees uppercase and lowercase differently.

# VALID: Case-sensitive Age = 40 age = 50 print(f"Upper Case 'Age' is: {Age}") print(f"Lower Case 'age' is: {age}")
# Explanation:
# This works perfectly. 
# 'Age' and 'age' are stored in different places in memory.
Your output will appear here...
Analysis: By testing these rules separately, you can see exactly which errors appear. Remember: Numbers at the start? No. Keywords? No. Uppercase/Lowercase mixed? Yes, but be careful!

You're making great progress! Ready to tackle more Python challenges with real-world Exercise?

Python Exercise

Beyond the Rules: Naming Well (Best Practices)

Following the rules makes your code work. Following best practices (known as PEP 8, Python's official style guide) makes your code readable and professional.

  • Use snake_case for variables and functions: This means all lowercase letters, with words separated by underscores.
    • Good: user_name, first_name, calculate_tax()
    • Bad: UserName, firstname, CalculateTax()
  • Use PascalCase for classes: This means capitalizing the first letter of each word (also called CapWords).
    • Good: class UserProfile:, class ShoppingCart:
    • Bad: class user_profile:, class shopping_cart:
  • Use descriptive names: Your code should be easy to read.
    • Good: customer_email = "test@example.com"
    • Bad: c_eml = "test@example.com" (What is `c_eml`?)

Keywords vs. Identifiers: A Quick Summary

Here is the key difference. A table is a great way to see it side-by-side.

Feature Keywords Identifiers
Purpose Python's built-in syntax rules Your custom labels for data
Who creates it? Python language You (the programmer)
Can you change it? No, they are fixed Yes, you invent them
Example if, def, while, class age, user_name, Calculator
📚

Learn Python the Right Way

  • 50+ hands-on coding exercises
  • Real-world projects and applications
  • Learn best practices from industry professionals

Knowledge Check: Fix the Broken Code!

You've learned the rules and the best practices. Now, let's put it all together. The code in the editor below is completely broken! It violates keyword rules, identifier rules, and best practices.

Your mission: Fix the code so it runs successfully and follows professional naming conventions.

# Fix the names in this code! # 'class' is a keyword, 'my-user' has a hyphen class my-user: def __init__(self, name, 1st_age): self.name = name # '1st_age' starts with a number self.1st_age = 1st_age # 'pass' is a keyword def pass(user): print(f"User: {user.name}, Age: {user.1st_age}") # Create a user # 'for' is a keyword for = my-user("Alice", 30) pass(for)

Here is how a professional developer would fix this:

# --- Here's the corrected code ---

# Rule: Classes use PascalCase (e.g., MyUser)
class MyUser:
  # 'age' is a valid identifier. '1st_age' is not.
  def __init__(self, name, age):
    self.name = name
    self.age = age

# Rule: Functions use snake_case.
# 'pass' is a keyword, so we rename it to something descriptive.
def print_user_details(user):
  # We also fix the attribute we're printing
  print(f"User: {user.name}, Age: {user.age}")

# Create a user
# 'new_user' is a good snake_case identifier. 'for' is a keyword.
new_user = MyUser("Alice", 30)
print_user_details(new_user)
Your output will appear here...
🏆

Congratulations! You've Completed This Lesson

You now understand Python keywords, identifiers, and the professional conventions for naming. Ready to continue your Python mastery?

📘

Full Python Course

Master Python with 11+ hours of content, 50+ exercises, real-world projects and get a Certificate.

Enroll Now
📝

Next Lesson

Continue with the next lesson on Python Variables.

Next Lesson →

Scroll to Top