Python Identifiers Naming Conventions

By | November 25, 2022

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.

Python Identifiers and Naming Conventions

Rules for Naming Identifiers

  1. An identifier starts with a letter A to Z, or a to z, or an underscore (_).
  2. We may use zero or more letters, underscores, and digits (0 to 9).
  3. Python identifiers cannot contain special characters such as @, $, and %.
  4. 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 With Naming Conventions

Python Identifiers and Naming Conventions

 

 36 total views,  1 views today

Leave a Reply

Your email address will not be published. Required fields are marked *