Range Function in Python

Step-by-step Python programming tutorial

Interested in building your Python skills? Understand the nuances of the built-in Python range function, its usage, and its wide applications in creating a sequence of integers in this detailed video tutorial.

Key Insights

  • The Python range function creates a sequence of integers or a range of numbers.
  • Three parameters are used in the range function: start, stop, and step. However, only the stop parameter is mandatory.
  • The default start point of the range function is zero when not explicitly specified.
  • The stop point in the range function is exclusive, generating numbers up to one less than the stop value.
  • The range function is often used in conjunction with a 'for' loop. If you want to include the stop point in the range, you need to increment the stop point by one.
  • The 'step' argument in the range function can be manipulated to skip numbers in the sequence or to generate a sequence in descending order.

Python Essentials

  • Data Types

    int, float, str, list, dict, set, tuple — the basic vocabulary.

  • Control Flow

    if/elif/else, for, while — the logic of any program.

  • Functions

    def, return, *args, **kwargs — reusable units of behavior.

  • Modules & Imports

    import statements, packages, virtual environments.

Play video

Video Transcription

Hi, my name is Art, and I teach Python at Noble Desktop. In this video, I'm going to show you how to use the built-in Python range function. You can always use help to see the definition of range. Range creates or produces a sequence of integers; it returns a range of numbers.

For example, if you want to get a range of numbers from 1 to 10, it will generate a range of numbers, all of which are numeric data types (integers). Range takes three arguments: start, stop, and step.

Do not confuse these with Python's built-in slicing notation. You can provide just one argument (stop) and the default value for the start point will be zero.

Python for Data Science Bootcamp: Live & Hands-on, In NYC or Online, Learn From Experts, Free Retake, Small Class Sizes,  1-on-1 Bonus Training. Named a Top Bootcamp by Forbes, Fortune, & Time Out. Noble Desktop. Learn More.

Remember that the stop point is exclusive, so if you pass 5, it will generate a range of numbers from 0 to 4. Most of the time, people use range with a for loop, like "for i in range". If you want to include 5, you need to increase the stop point to 6.

You can also provide a step argument. A step of one means you will get each number in the sequence, while a step of two will skip every other number. To go in descending order, you need to swap the start and stop values and use a negative step.

Try playing around with range, using different numbers and variations. In my next video, I'm going to show you how to use range to generate an index. See you then!

Topics

Learn more in these courses

How to Learn Python

Master Python with hands-on training. Python is a popular object-oriented programming language used for data science, machine learning, and web development. 

Yelp Facebook LinkedIn YouTube Twitter Instagram