Skip to content

Python function naming convention

  • by

The Python function naming convention follows several guidelines to promote the readability and maintainability of code. Here are the key points of the convention:

  1. Use lowercase letters and separate words with underscores: Function names should be in lowercase letters, and if the name consists of multiple words, they should be separated by underscores. This convention is known as “snake_case”. For example: calculate_average, get_user_name.
  2. Use descriptive and meaningful names: Choose function names that accurately describe the purpose or behavior of the function. This helps make your code more readable and understandable to other developers. Avoid using generic names like function1 or my_function.
  3. Use verbs or verb phrases: Start the function name with a verb or a verb phrase to indicate that it performs an action. This makes it clear that the function is doing something. For example: calculate, find_maximum, validate_input.
  4. Be consistent: Maintain consistency in your naming conventions throughout your codebase. If you choose to follow a specific naming convention, stick to it for all your functions.

The Python function naming convention syntax can be summarized as follows:

def function_name(arguments):
    Function documentation (optional)
    # Function code
    return result (if applicable)

Following these naming conventions will help make your code more readable, maintainable, and easier to understand for yourself and others who read your code.

Read more: Python PEP8 Style Guide

Python function naming convention example

Here’s an example that demonstrates the Python function naming convention:

def calculate_average(numbers_list):
    Calculate the average of a list of numbers.
        numbers_list (list): A list of numbers.
        float: The average of the numbers.
    total = sum(numbers_list)
    count = len(numbers_list)
    average = total / count
    return average

# Usage example
numbers = [2, 4, 6, 8, 10]
avg = calculate_average(numbers)
print("Average:", avg)


Python function naming convention

In this code, the calculate_average function is defined according to the naming convention and syntax we discussed earlier. It takes a list of numbers as an argument, calculates the average, and returns the result.

Do comment if you have any doubts or suggestions on this Basic Python topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

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