Table of Contents
In this article, you’ll learn about Python Keywords and Identifiers and more.
Python, a versatile and widely-used programming language, relies on specific keywords and identifiers to construct its code.
Python Keywords
- Keywords are the reserved words in Python.
- We cannot use a keyword as a variable name, function name or any other identifier. They are used to define the syntax and structure of the Python language.
- In Python, keywords are case sensitive.
It’s difficult to neatly categorize all 35 Python keywords into a table because they serve vastly different purposes. However, Here we grouped them by general functionality to give you a better overview:
Category | Keywords | Description |
---|---|---|
Conditional Statements | if , elif , else | Control the flow of execution based on conditions. |
Loops | for , while , break , continue , pass | Iterate over sequences or execute blocks of code repeatedly. break exits a loop, continue skips to the next iteration, and pass is a placeholder. |
Functions | def , return , lambda | Define and return values from functions. lambda creates anonymous functions. |
Classes and Objects | class , isinstance , issubclass | Define classes and check object relationships. |
Exception Handling | try , except , finally , raise | Handle errors and exceptions during program execution. |
Boolean Values and Operators | True , False , and , or , not , in , is | Represent truth values and perform logical/membership/identity comparisons. |
Imports and Modules | import , from , as | Import modules and specific objects from modules. as is used for aliasing. |
Context Management | with | Manage resources (like files) efficiently. |
Generators | yield | Create iterators that produce values on demand. |
Variable Scope | global , nonlocal | Control the scope of variables within functions. |
Deletion | del | Delete objects or references. |
Null Value | None | Represents a null or empty value. |
All the keywords except True
, False
and None
are in lowercase.
Example
if x > 10: # 'if' is a keyword
print("x is greater than 10")
else: # 'else' is a keyword
print("x is not greater than 10")
for i in range(5): # 'for' and 'in' are keywords
print(i)
def greet(name): # 'def' is a keyword
return "Hello, " + name # 'return' is a keyword
is_active = True # 'True' is a keyword
class MyClass: # 'class' is a keyword
pass # 'pass' is a keyword
Python Identifiers
An identifier is a name given to entities like class, functions, variables, etc. It helps to differentiate one entity from another.
Rules for Identifiers
- Character Set: Identifiers can contain letters (a-z, A-Z), digits (0-9), and underscores (_).
- First Character: The first character of an identifier cannot be a digit. It must be a letter or an underscore.
- Case Sensitivity: Python is case-sensitive.
my_variable
andMy_Variable
are considered different identifiers. - No Keywords: Identifiers cannot be the same as any of Python’s keywords.
- No Special Characters: Identifiers cannot contain special characters like !, @, #, $, %, etc.
- Length: While there’s no official limit on the length of an identifier, it’s best to keep them reasonably short and descriptive.
Example
name = "Alice" # 'name' is an identifier
age = 30 # 'age' is an identifier
def calculate_sum(x, y): # 'calculate_sum', 'x', and 'y' are identifiers
total = x + y # 'total' is an identifier
return total
result = calculate_sum(5, 10) # 'result' is an identifier
print(result)
Things to Remember
- Python is a case-sensitive language. This means,
Variable
andvariable
are not the same. - Always give the identifiers a name that makes sense. While
c = 10
is a valid name, writingcount = 10
would make more sense, and it would be easier to figure out what it represents when you look at your code after a long gap. - Multiple words can be separated using an underscore, like
this_is_a_long_variable
.
Best Practices for Identifiers
- Be Descriptive: Choose names that clearly indicate the purpose of the variable or function.
- Follow Conventions: Adhere to common naming conventions (e.g., snake_case for variables and functions, CamelCase for classes).
- Be Consistent: Use the same naming style throughout your code.
- Avoid Single-Character Names (except for simple loop counters):
i
,j
,k
are acceptable for short loops, but more descriptive names are better in most cases. - Use Meaningful Abbreviations: If you need to shorten a name, use abbreviations that are easily understood.