What are Identifiers?
Identifiers are names used to label variables, functions, classes, modules, and other objects in 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: Words like def and if, which are reserved for specific language functions, cannot be used as identifiers. -
Case Sensitivity: Python identifiers are case-sensitive.mynameandmyNameare 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 like variable, function, and class names should be descriptive and reflect their purpose in the code.
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.
Lessons in this chapter · Master the Fundamentals of Python
- 1. What is Programming and What Benefits Can You Gain from Learning It?
- 2. Coding and Viewing Your Results in Real-Time
- 3. Are There Levels in Programming Languages?
- 4. Features of Python and Simple Code Example
- 5. Reasons Why Python is Popular
- 6. Why is Indentation Important in Python?
- 7. Printing Output in Python
- 8. Getting User Input in Python
- 9. Multiple-choice quiz
- 10. Statements and Expressions
- 11. Predefined Reserved Words in Python - Keywords
- 12. How to Name Variables and Functions Sensibly
- 13. Explaining Code with Comments
- 14. What are Identifiers?
- 15. A Container for Data, Variables
- 16. Important Points When Assigning Values to Variables
- 17. Symbols for Performing Operations, Operators
- 18. Literals Representing Data
- 19. Coding Quiz - Return 10 Times the Given Number
- 20. Multiple-choice quiz
- 21. Fill-in-the-blank quiz
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help