• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Python Basics
    • Comments
    • Functions
    • Lists
    • Loops
    • Strings
    • Syntax Basics
  • Code Examples
  • Categories
    • Basics
    • Code Snippets
    • Python On The Web
    • Scripts
    • System & OS
    • Dictionary
    • Modules In Python
    • Lists
    • Modules
    • OS


You are here: Home / Modules In Python / Python Range Function

Python Range Function

Last Updated: August 27, 2020

The Range function

The built-in range function in Python is very useful to generate sequences of
numbers in the form of a list.

The given end point is never part of the generated list;

range(10) generates a list of 10 values, the legal indices for items of a
sequence of length 10.

It is possible to let the range start at another number, or to specify a
different increment (even negative;

Sometimes this is called the ‘step’):

Range Examples

>>> range(1,10)
[1, 2, 3, 4, 5, 6, 7, 8, 9]

# You can use range() wherever you would use a list. 

a = range(1, 10) 
for i in a: 
    print i 

for a in range(21,-1,-2):
   print a,

#output>> 21 19 17 15 13 11 9 7 5 3 1


# We can use any size of step (here 2)
>>> range(0,20,2)
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

>>> range(20,0,-2)
[20, 18, 16, 14, 12, 10, 8, 6, 4, 2]

# The sequence will start at 0 by default. 
#If we only give one number for a range this replaces the end of range value.
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

# If we give floats these will first be reduced to integers. 
>>> range(-3.5,9.8)
[-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8]
More Reading

http://www.python.org

Recommended Python Training

For Python training, our top recommendation is DataCamp.

Free Trial

Filed Under: Modules In Python Date Originally Published: October 15, 2012

More Python Topics

API Argv Basics Beautiful Soup bitly Cheatsheet Code Code Snippets Command Line Comments Control Flow crawler Data Types Development Dictionary Dictionary Data Structure In Python Errorhandling Error Handling Exceptions Fabric Files ftplib Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pil pip Python Python On The Web Python Strings Requests Scraping Scripts sh Strings System & OS urllib2 Web

Primary Sidebar

Get Our Free Guide To Learning Python

Menu

  • Python Basics
  • Code Examples
  • Loops
  • Functions
  • Strings
  • Python on the Web
  • Lists
  • Dictionaries
  • Python Modules
  • Python Glossary
  • Learn Python

Most Popular Content

  • Reading and Writing Files in Python
  • String Concatenation and Formatting
  • List Comprehensions in Python
  • How to use sys.argv in Python
  • How to use Split in Python
  • How to use comments in Python
  • Python Syntax Basics

Recent Posts

  • Iterating over dictionary in Python
  • Difference between comments and docstrings in Python
  • Shortcut to comment out multiple lines in Python
  • Convert a list containing float numbers to string in Python
  • Single Line and Multi Line Comments in Python

Python Courses

  • Datacamp: Intro To Python
  • 2021 Complete Python Bootcamp
  • Python Mega Course: Build 10 Real World Apps
  • Python Data Science Bootcamp
  • Complete Python Developer: Zero to Mastery

Copyright © 2012–2021 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us