Last Updated: January 3, 2026
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.
len FunctionAt 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.
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!
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:
When you use len on a string, it counts every character, including spaces and punctuation.
For lists and tuples, len counts the number of elements they contain.
With dictionaries, len counts the number of key-value pairs.
While len is quite straightforward, there are some nuances to be aware of. For example:
None to len, it will raise a TypeError since None does not have a length.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.
range FunctionThe 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.
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:
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.
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.
range with ListsA 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.
While range is quite robust, there are a few key points to remember:
range generates numbers lazily, meaning it doesn’t create a list in memory. This efficiency is crucial when dealing with large ranges.You might often see range used in conjunction with condition checks. For instance, you may want to process only even indices in a list:
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.
len and rangeOften, 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.