In this lesson, we will discuss Keywords and Identifiers in Python. These are fundamental concepts that form the building blocks of Python programming.
Keywords are reserved words in Python that have a specific meaning and function. These words are part of the language syntax and cannot be used as identifiers (e.g., variable or function names).
True
is a keyword, but true
is not).Here are some of the most commonly used keywords:
False
, None
, True
, and
, as
, assert
, async
, await
, break
, class
, continue
, def
, del
, elif
, else
, except
, finally
, for
, from
, global
, if
, import
, in
, is
, lambda
, nonlocal
, not
, or
, pass
, raise
, return
, try
, while
, with
, yield
# Using some keywords in a program
if True: # True is a keyword
print("This is a true statement")
def my_function(): # def is a keyword
pass # pass is a keyword used as a placeholder
An identifier is the name used to identify variables, functions, classes, modules, or other objects in a program.
Allowed Characters:
a-z
, A-Z
), digits (0-9
), and underscores (_
).Case Sensitivity:
variable
, Variable
, and VARIABLE
are all different.Reserved Words:
Special Characters:
@
, $
, %
, etc.Length:
# Examples of valid identifiers
my_variable = 10 # Valid
result_2 = 20 # Valid
MyClass = "Python Class" # Valid
calculate_total = 100.50 # Valid
# Examples of invalid identifiers
2variable = 10 # Invalid: Cannot start with a digit
my-variable = 20 # Invalid: Hyphen is not allowed
class = "Test" # Invalid: 'class' is a keyword
Use Descriptive Names:
Example:
score = 95 # Describes the purpose of the variable
Follow Naming Conventions:
snake_case
for variable and function names.PascalCase
for class names.Example:
my_variable = 10 # snake_case for variables
class MyClass: # PascalCase for classes
pass
Avoid Single-Letter Names:
x
, y
, or z
unless in loops or mathematical contexts.Avoid Confusion with Keywords:
Example:
# Avoid using built-in names like 'list', 'str', 'int' as identifiers
my_list = [1, 2, 3] # Preferred
list = [1, 2, 3] # Avoid this
When naming variables, functions, classes, or other identifiers, choosing a consistent naming style improves the readability and maintainability of your code. Below are common naming conventions used in programming, including Python:
snake_case:
All lowercase letters with words separated by underscores.
Example: my_variable_name
camelCase:
The first word is lowercase, and subsequent words are capitalized.
Example: myVariableName
PascalCase (or UpperCamelCase):
All words are capitalized with no separators.
Example: MyVariableName
SCREAMING_SNAKE_CASE:
All uppercase letters with words separated by underscores, typically used for constants.
Example: MAX_LIMIT
UPPERFLATCASE:
All uppercase letters with no separators.
Example: MYVARIABLENAME
kebab-case (not valid in Python):
Words are separated by hyphens. This style is commonly used in URLs or CSS but not in Python.
Example: my-variable-name
flatcase:
All lowercase letters with no separators.
Example: myvariablename
Pascal_Snake_Case:
A mix of PascalCase and snake_case. Words are capitalized and separated by underscores.
Example: My_Variable_Name
camel_Snake_Case:
A mix of camelCase and snake_case.
Example: my_Variable_Name
Train-Case (not valid in Python):
Similar to PascalCase but with hyphens instead of spaces.
Example: My-Variable-Name
COBOL-CASE (not valid in Python):
Similar to SCREAMING_SNAKE_CASE but with hyphens instead of underscores.
Example: MY-VARIABLE-NAME
Adhering to these conventions ensures your code is clean, standardized, and easy for others to understand.
Objective: Write a Python script to explore and practice keywords and identifiers.
Create a Python Script:
keywords_and_identifiers.py
.Experiment with Keywords:
Example:
# Example using Python keywords
for i in range(3):
if i % 2 == 0:
print("Even number")
else:
print("Odd number")
Create Valid Identifiers:
Example:
my_variable = 10
def calculate_sum(a, b):
return a + b
class MyClass:
pass
Demonstrate Invalid Identifiers:
Example:
# Invalid identifiers
123name = "Alice" # SyntaxError
my-variable = 50 # SyntaxError
Understanding keywords and identifiers is crucial for writing clean and error-free Python code. With this knowledge, you can now create meaningful and valid names for your Python objects while avoiding reserved words!