Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is loc in Python

Understanding LOC in Python

When you start learning to program, you may come across the term "LOC" or "Lines of Code." It's a simple yet important concept used to measure the size of a software program by counting the number of lines in its source code. In Python, LOC gives you an idea of how large a script or application is, which can be helpful in various scenarios, such as estimating the effort required to maintain the code or understanding the complexity of a project.

The Basics of LOC

LOC is literally the count of lines written in the source code of a program. However, not all lines are considered equal. In Python, a line of code typically includes any line that has a statement that the Python interpreter can execute. This means that blank lines, comments, or purely formatting lines (like those containing only braces or parentheses) are not usually counted as LOC.

Counting Lines of Code

To illustrate how LOC is counted, let's look at a simple Python program:

# This is a comment and not counted in LOC

def greet(name):  # This line is a part of LOC
    # Another comment that is not part of LOC
    print(f"Hello, {name}!")  # This line is counted

greet("Alice")  # This line is also part of LOC

In the above example, there are three lines of code that the Python interpreter will execute: the function definition line, the print statement inside the function, and the function call at the end. The comments and the blank line are not counted in the LOC.

Why LOC Matters

LOC can be a helpful metric for several reasons:

  • Estimating Work: It can give a rough estimate of how much work went into writing the program or how much might be needed to review or maintain it.
  • Measuring Productivity: Some organizations use LOC to measure programmer productivity, although this is a controversial and often criticized practice.
  • Understanding Codebase Size: LOC can help developers get a quick sense of the size of a codebase. A larger LOC might indicate a more complex or feature-rich application.

LOC and Pythonic Code

Python is known for being a language that allows you to do more with less code, thanks to its readable syntax and powerful standard library. Writing "Pythonic" code often means writing code that is clear, concise, and efficient. As a result, Python programs might have fewer lines of code compared to the same functionality implemented in other languages.

Practical Example: Counting LOC in a Python File

Let's write a Python script that counts the LOC in another Python file. This script will read a file, go through each line, and count it if it's not a comment or a blank line:

def count_loc(file_path):
    loc_count = 0
    with open(file_path, 'r') as file:
        for line in file:
            stripped_line = line.strip()
            if stripped_line and not stripped_line.startswith("#"):
                loc_count += 1
    return loc_count

# Example usage
file_path = ''
print(f"The file {file_path} has {count_loc(file_path)} lines of code.")

In this example, we define a function count_loc that takes a file path as an argument. It opens the file, goes through each line, strips whitespace from the beginning and end of the line, and checks if the line is neither empty nor a comment. If it's a line of code, it increments the loc_count.

LOC Limitations

While LOC can be useful, it's important to understand its limitations:

  • Quality Over Quantity: LOC does not measure the quality of code. A smaller, well-written codebase can be far superior to a larger, poorly written one.
  • Not a Measure of Complexity: LOC does not necessarily reflect the complexity or difficulty of the code. Some complex algorithms can be written in just a few lines.
  • Varying Coding Styles: Different programmers have different coding styles. Some may write more verbose code, while others may prefer concise expressions.

Intuition and Analogies

Think of LOC as the word count in an essay. Just as the word count gives you a quick idea of the essay's length, the LOC gives you an idea of a program's size. However, just like a longer essay isn't automatically better or more informative, a program with more LOC isn't automatically more complex or useful.


In the journey of learning programming, understanding metrics like LOC is part of understanding the broader landscape of software development. LOC provides a simple way to gauge the size of a Python program, but remember that it's only one piece of the puzzle. The art of programming is not just in the lines you write but in the problems you solve and the elegance with which you solve them. As you grow as a programmer, you'll learn to appreciate the beauty of a well-crafted piece of code, not just its length. Keep coding, keep learning, and remember that every line of code is a step towards mastery, regardless of how many lines it takes to get there.