AlgoMaster Logo

len & range

Last Updated: January 3, 2026

6 min read

Understanding how to effectively use the len and range functions in Python can significantly enhance your coding skills.

These two functions are foundational, and knowing when and how to use them will help you work with collections and loops more efficiently.

Let’s dive into these essential built-in functions.

The len Function

At its core, the len function provides a simple way to determine the length of an object. It works with various data types, including strings, lists, tuples, and dictionaries.

Basic Usage

To get the length of a collection, you simply pass it as an argument to len:

In these examples, len returns the number of characters in the string and the number of items in the list. It’s that straightforward!

Working with Different Data Types

len can be used with various data types, and understanding how it operates with each can prevent errors in your code. Here are some examples to illustrate:

Strings

When you use len on a string, it counts every character, including spaces and punctuation.

Lists and Tuples

For lists and tuples, len counts the number of elements they contain.

Dictionaries

With dictionaries, len counts the number of key-value pairs.

Edge Cases

While len is quite straightforward, there are some nuances to be aware of. For example:

  • If you pass None to len, it will raise a TypeError since None does not have a length.
  • An empty collection will return a length of 0, which is useful in conditional checks.

Practical Applications

The len function is often used in conditions, especially when you want to check if a collection is empty before proceeding. Here’s a common pattern:

This approach helps you avoid errors that could occur if you attempt to access elements of an empty list.

The range Function

The range function is a powerful tool for generating sequences of numbers, commonly used in loops. It returns an immutable sequence of numbers, which you can iterate over.

Basic Usage

The most basic way to use range is with a single argument, which defines the upper limit, starting from 0 by default:

This will output:

Specifying Start and Step

You can also specify a starting point and a step size. This flexibility makes range incredibly versatile.

In this example, we start at 1, stop before 10, and increase by 2 each time.

Negative Ranges

The range function also supports negative steps, allowing you to generate sequences in reverse.

This can be particularly useful for countdowns or iterating backward through a list.

Using range with Lists

A common use case for range is iterating over the indices of a list. This can be beneficial when you need the index for accessing elements, or for modifying them.

In this example, we double each element in the list by using their indices.

Edge Cases and Nuances

While range is quite robust, there are a few key points to remember:

  • If you provide a range where the start is greater than the stop with a positive step, you’ll get an empty sequence.
  • range generates numbers lazily, meaning it doesn’t create a list in memory. This efficiency is crucial when dealing with large ranges.

Real-World Applications

Iterating with Conditions

You might often see range used in conjunction with condition checks. For instance, you may want to process only even indices in a list:

Generating Number Sequences

You can use range to generate number sequences for various applications, such as simulating data or creating test scenarios.

This list comprehension combined with range creates a list of squares from 0 to 9.

Combining len and range

Often, you will find yourself combining both len and range in your code. This is especially common when working with lists or strings where you need the index to access elements.

This snippet prints each character in the string along with its index.

In the next chapter, we will look at how to work with types in Python, which will deepen your understanding of dynamic typing and object-oriented programming.