An Introduction to the Python Range Function.

An Introduction to the Python Range Function.

Let’s learn about the python range function in detail. The range type represents an immutable sequence of numbers and is commonly used for looping a specific number of times in for loops.

Range:

The_ range__ type represents an immutable sequence of numbers and is commonly used for looping a specific number of times in **_for**_ loops._

range(stop)
range(start,stop,step)

**start**

The value of the start parameter (or 0 if the parameter was not supplied)

**stop**

The value of the stop parameter

**step**

The value of the step parameter (or 1 if the parameter was not supplied).

If the step is 0, it will raise ValueError.

The arguments to the range function should be integers. (either built-in int or any object that implements the __index__ special method)

Example 1:Only the stop parameter is given.

range(10)

  • start by default will be 0 and step *by default will be *1
  • stop is given as 10.
  • stop value is excluded. It generates value until 9 only.
  • It will return a range object containing numbers starting from 0 to 9.
  • We can convert the range object to list using list() constructor.
  • We can also iterate using for loop
r=range(10)
print (r)#Output:range(0, 10)
print (type(r))#Output:<class 'range'>
print (list(r))
#Output:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Example 2:Only the start and stop parameter is given.

**range(1,10)**

  • step by default will be 1
  • It will generate a sequence of numbers starting from 1 to 9.
r=range(1,10)
print (r)#Output:range(1, 10)
#Converting range object to list
print (list(r))
#Output:[1, 2, 3, 4, 5, 6, 7, 8, 9]

Example 3:start, stop and step parameter is given

range(1,10,2)

  • It will generate a sequence from** 1, increment by **2, and will stop at 9.
r=range(1,10,2)
print (r)#Output:range(1, 10, 2)
#Converting range object to list
print (list(r))
#Output:[1, 3, 5, 7, 9]

Example 4:We can also decrement step by mentioning a negative number.

range(10,1,-2)

  • It will generate a sequence of numbers from 10, decrement by 2, and stop at 1.
  • Iterating through range object using for loop.

python-range python-programming python python3 software-development function

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Offshore Software Development - Best Practices

To make the most out of the benefits of offshore software development, you should understand the crucial factors that affect offshore development.

Hire Python Developers

Are you looking for experienced, reliable, and qualified Python developers? If yes, you have reached the right place. At **[HourlyDeveloper.io](https://hourlydeveloper.io/ "HourlyDeveloper.io")**, our full-stack Python development services...

Hire Python Developers India

Looking to build robust, scalable, and dynamic responsive websites and applications in Python? At **[HourlyDeveloper.io](https://hourlydeveloper.io/ "HourlyDeveloper.io")**, we constantly endeavor to give you exactly what you need. If you need to...

Python Programming: A Beginner’s Guide

Python is an interpreted, high-level, powerful general-purpose programming language. You may ask, Python’s a snake right? and Why is this programming language named after it?

Functional Programming

Functional Programming: Functional Programming is a Declarative style of Programming Paradigm for writing computer programs.