What are Identifiers?
Identifiers
are unique names that distinguish variables, functions, classes, modules, etc., identifying data and objects within a program.
Note: An object refers to anything that includes data and the behavior (methods) related to this data. In Python, everything is an object.
Python Identifier Rules
-
Starting Character Rule
: An identifier must start with a letter (A-Z, a-z) or an underscore (_). It cannot start with a digit. -
Use of Characters, Digits, Underscore
: After the first character, it can be followed by letters, digits (0-9), or underscores (_). -
Reserved Keywords Cannot Be Used
: Keywords reserved for certain functionalities, like def, if, cannot be used as identifiers. -
Case Sensitivity
: Python identifiers are case-sensitive.myname
andmyName
are considered different identifiers.
# Valid identifier examples my_variable = 10 # Variable name starts with a letter or underscore def my_function(): # Function name starts with a letter or underscore print("Hello") class MyClass: # Class name starts with a letter or underscore pass # Invalid identifier examples 2my_variable = 10 # Variable name cannot start with a digit def if(): # 'if' keyword cannot be used as a function name
Importance of Identifiers
Identifiers such as variable names, function names, and class names should be named to clearly reflect their role and purpose.
radius = 25 # Variable name representing the radius def calculate_circle_area(radius): # Function name to calculate circle area return 3.14 * radius * radius
Coding Practice
Try entering the highlighted 1_my_number = 7
on the practice screen.
You'll see an error occurs when a variable name starts with a digit.
Guidelines
AI Tutor
Publish
Design
Upload
Notes
Favorites
Help
Code Editor
Execution Result