Python Tutorial

Python Variable

Python Operators

Python Sequence

Python String

Python Flow Control

Python Functions

Python Class and Object

Python Class Members (properties and methods)

Python Exception Handling

Python Modules

Python File Operations (I/O)

Python Identifier Naming Convention

In Python, an identifier is a name used to identify variables, functions, classes, modules, or other objects. Following a consistent naming convention makes your code more readable and maintainable. Python's recommended naming conventions are defined in PEP 8, the Python style guide.

Here are the general rules for creating identifiers:

  1. Identifiers must start with a letter (A-Z or a-z) or an underscore (_). They can be followed by any number of letters, digits (0-9), or underscores.
  2. Identifiers are case-sensitive, so my_variable and My_Variable are considered different identifiers.
  3. Keywords (reserved words) cannot be used as identifiers.

Here are the naming conventions for different types of identifiers:

  1. Variables and functions: Use lowercase letters and separate words with underscores (snake_case). Example: my_variable, calculate_sum

  2. Constants: Use uppercase letters and separate words with underscores. Example: PI, MAX_SIZE

  3. Classes: Use PascalCase, capitalizing the first letter of each word. Example: MyClass, DataProcessor

  4. Modules: Use short, lowercase names, and separate words with underscores if necessary. Example: my_module, file_reader

  5. Instance attributes and methods: Use lowercase letters and separate words with underscores (snake_case). Example: self.my_attribute, self.my_method()

  6. Private attributes and methods: Prefix the identifier with a single underscore. Example: self._private_attribute, self._private_method()

  7. "Strongly private" attributes and methods: Prefix the identifier with two underscores. This triggers Python's name mangling, which makes it more difficult to accidentally access or modify the attribute or method. Example: self.__strongly_private_attribute, self.__strongly_private_method()

  8. Magic methods and attributes: Use double underscores as a prefix and suffix. Example: __init__, __str__, __call__

Remember, these are conventions, not strict rules. However, following them will make your code more consistent and easier to read for others who are familiar with Python's naming conventions. When working with existing projects, follow the project's existing conventions for consistency.

  1. Python Variable Naming Conventions:

    • Variables should be descriptive and lowercase.
    • Use underscores to separate words in variable names.
    • Avoid using reserved words.
    # Example:
    my_variable = 42
    
  2. Naming Conventions for Python Functions:

    • Function names should be lowercase and descriptive.
    • Use underscores to separate words.
    • Follow the same rules as variable naming.
    # Example:
    def calculate_sum(a, b):
        return a + b
    
  3. CamelCase vs snake_case in Python:

    • Python conventionally uses snake_case for variable and function names.
    • CamelCase is often reserved for class names.
    # Snake case
    my_variable_name = 10
    
    # Camel case (commonly used for class names)
    class MyClass:
        pass
    
  4. Python Class Naming Conventions:

    • Class names should follow the CapWords convention (CamelCase).
    • Be descriptive and use nouns.
    # Example:
    class Car:
        def __init__(self, make, model):
            self.make = make
            self.model = model
    
  5. How to Name Variables in Python:

    • Use descriptive names.
    • Follow the conventions mentioned earlier.
    # Example:
    user_age = 25
    
  6. Python Module Naming Conventions:

    • Module names should be short, lowercase, and descriptive.
    • Avoid underscores in module names.
    # Example:
    import mymodule
    
  7. Consistent Naming in Python Code:

    • Maintain consistency throughout your codebase.
    • Stick to the chosen conventions for variables, functions, classes, and modules.
    # Consistent naming example:
    def process_data(data_list):
        for item in data_list:
            print(item)