Today we shall discuss “Python Identifiers and Naming Conventions”.
Python Identifiers
A Python identifier is the name of a variable, function, class, module, or other object. We can say that Python Identifier is the name we use to identify a variable, function, class, module or other object in a Python program / script. We use identifiers to refer to the above mentioned entities in our program.
Rules for Naming Identifiers
- An identifier starts with a letter A to Z, or a to z, or an underscore (_).
- We may use zero or more letters, underscores, and digits (0 to 9).
- Python identifiers cannot contain special characters such as @, $, and %.
- Since Python is a case-sensitive language, so “Marks” and “marks” are different identifiers.
Python Naming Conventions
Furthermore, Python has the following naming conventions:
Class Names
Class names should normally use the CapWords convention. Examples include:
- MyClass
- Circle
- Book
Function and Variable Names
Function names should be lowercase, with words separated by underscores to improve readability.
Variable names follow the same convention as function names. Therefore we may give the following examples of variable names and function names:
Function name examples
- my_function()
- factorial()
- calculate_total()
Variable name examples
- first_name
- num1
- num2
- average
- sum
- total
Constants
Constants are written in all capital letters with underscores to separate words. Examples of constants include:
- MAX_SPEED
- PI
Function and Method Arguments
Always use self for the first argument to instance methods.
Always use cls for the first argument to class methods.
Python Identifiers and Naming Conventions
-
You may also like:
- Python Ternary Operator or Conditional Expressions
- Looping Statements in Python
- Python for Loop Syntax Plus Examples
- Python User Defined and Built-in Functions – Defining and Using Functions
- Python Variable Names and Naming Rules
- Python Variables
- Python Math Operators
- Python if elif else Statement with Example Programs
- Explain Use of Python Comments
- Python Comparison Operators
- Python if else statement
- Python if statement Syntax Example
- Write and Run Sample Python Script
- How To Run Python on Windows
- How To Install Python on Windows