Python Basics
Python Keywords and Identifiers
Keywords are reserved words with special meaning in Python, while identifiers are the names you choose for variables, functions and more.
What are Keywords?
Keywords are words that Python has reserved for its own syntax, such as if, else, for, while, def, class, and True. Because they already have a fixed meaning, you cannot use them as variable names.
import keyword
print(keyword.kwlist)Common Python Keywords
| Keyword | Purpose |
|---|---|
| if / elif / else | Conditional statements |
| for / while | Loops |
| def | Defining a function |
| class | Defining a class |
| return | Returning a value from a function |
| True / False / None | Special built-in values |
| import | Bringing in a module |
| and / or / not | Logical operators |
What are Identifiers?
An identifier is the name you give to a variable, function, class, or module. Choosing clear identifiers makes your code much easier to read and maintain.
Rules for Valid Identifiers
- Must start with a letter (a-z, A-Z) or an underscore _
- Can contain letters, digits and underscores after the first character
- Cannot start with a digit
- Cannot contain spaces or symbols like -, @, or %
- Cannot be the same as a reserved keyword
- Are case-sensitive, so total and Total are different names
# Valid identifiers
student_name = "Meera"
_score = 90
total2 = 100
# Invalid identifiers (would cause errors)
# 2total = 100 -> starts with a digit
# student-name = "" -> contains a hyphen
# class = "A" -> class is a reserved keywordPick descriptive identifier names like total_marks instead of short, unclear ones like tm — your future self will thank you.
