Python Basics — Keywords & Identifiers
Keywords & Identifiers
Every programming language has reserved words that have special meaning. Python calls these keywords. Meanwhile, the names you give to your variables, functions, and classes are called identifiers.
Python Keywords
Keywords are reserved — you cannot use them as variable names, function names, or any other identifiers.
Full List of Python Keywords (3.12)
import keyword
print(keyword.kwlist)
| Category | Keywords |
|---|---|
| Logic | True, False, None |
| Boolean Ops | and, or, not |
| Control Flow | if, elif, else |
| Loops | for, while, break, continue, pass |
| Functions | def, return, lambda, yield |
| Classes | class |
| Exceptions | try, except, finally, raise |
| Imports | import, from, as |
| Variable Scope | global, nonlocal |
| Membership/Identity | in, is |
| Context Manager | with |
| Assertion | assert |
| Deletion | del |
| Async | async, await |
| Pattern Matching | match, case (3.10+, soft keywords) |
What Happens If You Use a Keyword as a Name?
class = "Math" # SyntaxError!
Error:
SyntaxError: invalid syntax
Python will refuse to run your code if you try to use a keyword as an identifier.
Checking If a Word Is a Keyword
import keyword
print(keyword.iskeyword("for")) # True
print(keyword.iskeyword("name")) # False
print(keyword.iskeyword("class")) # True
print(len(keyword.kwlist)) # 35
Output:
True
False
True
35
Identifiers
An identifier is any name you create in Python — variables, functions, classes, modules. Think of them as labels you stick on things.
Naming Rules
| Rule | Valid | Invalid |
|---|---|---|
| Must start with a letter or underscore | name, _count, _ |
1name, @value |
| Can contain letters, digits, underscores | total_1, my_var2 |
my-var, my var |
| Cannot be a keyword | score, result |
class, for |
| Case-sensitive | name ≠ Name ≠ NAME |
— |
| No spaces allowed | first_name |
first name |
| No special characters | hello_world |
hello@world |
Naming Conventions (snake_case)
Python uses snake_case for most names — lowercase words separated by underscores:
# Good — snake_case for variables and functions
user_name = "Alice"
total_score = 95
max_retry_count = 3
def calculate_area(length, width):
return length * width
# Bad — these work but violate convention
userName = "Alice" # camelCase (used in Java/JavaScript)
TotalScore = 95 # PascalCase (reserved for classes)
MAXRETRYCOUNT = 3 # Hard to read without underscores
Convention Summary
| Type | Convention | Example |
|---|---|---|
| Variables | snake_case |
user_age, total_price |
| Functions | snake_case |
get_name(), calculate_tax() |
| Constants | UPPER_SNAKE_CASE |
MAX_SIZE, PI, BASE_URL |
| Classes | PascalCase |
MyClass, UserProfile |
| Modules | snake_case |
my_module.py, utils.py |
| Packages | lowercase |
mypackage, requests |
| Private | _leading_underscore |
_internal_value |
| Name Mangling | __double_leading |
__secret |
| Magic/Dunder | __double_both__ |
__init__, __str__ |
Good vs Bad Identifier Names
# Good — descriptive and clear
student_count = 42
temperature_celsius = 36.6
is_logged_in = True
# Bad — vague and unclear
x = 42
t = 36.6
flag = True
Always choose names that describe what the variable holds. Your future self (and your teammates) will thank you.
Case Sensitivity
Python treats uppercase and lowercase letters as different:
name = "Alice"
Name = "Bob"
NAME = "Charlie"
print(name) # Alice
print(Name) # Bob
print(NAME) # Charlie
Output:
Alice
Bob
Charlie
These are three completely separate variables. Be careful with capitalization.
Summary
- Python has 35 keywords that cannot be used as identifiers
- Use
keyword.iskeyword()to check if a word is reserved - Identifiers must start with a letter or underscore
- Python is case-sensitive —
nameandNameare different - Use snake_case for variables and functions
- Use PascalCase for classes
- Use UPPER_SNAKE_CASE for constants
- Choose descriptive names — clarity over brevity
Next up: Comments & Docstrings →