Python - Range (sequence of integer)

Card Puncher Data Processing

About

A range represents a sequence of integer, it is not a list or a set.

To get a list of a set from a range, you have to use the constructor: list() or set().

>>> list(range(2))
[0, 1]

Snippet

For any integer n, range(n) represents the sequence of integers from 0 through n - 1.

for x in range(5):
    print x
0
1
2
3
4

Syntax

A range can take 1, 2 or 3 arguments.

You can form a range with one, two, or three arguments.

  • The expression range(a,b) represents the sequence of integers a, a + 1, a + 2, … , b - 1.
  • The expression range(a,b,c) represents a, a + c, a + 2c, … (stopping just before b).

1 argument

Range starts the range at zero and increments by 1 until the size reaches 1 less than the range.

For instance:

print range(1) 
[0]

print range(2) 
[0,1]

2 arguments

Range starts the range at the first argument and increments by 1 until the size reaches 1 less than the second argument:

range(1,3)
[1,2]

print range(2,5)
[2, 3, 4]

3 arguments

range(Start,End,Increment)

where:

  • Start is the number the list starts at,
  • End is the number minus 1 where the list ends,
  • Increment is how much you should increment by (default increment of 1)

For instance:

print range(4,13,2)
[4, 6, 8, 10, 12]





Discover More
Card Puncher Data Processing
Python - Collection

collection in Python: Data Type: Type Sequence (Ordered) Mutable Duplicate Yes Yes Yes Yes No Yes No Yes No No Yes No
Card Puncher Data Processing
Python - Comprehension (Iteration)

Comprehension are a way to loop over elements of: a , , , tuple, , or . Comprehensions permit to build collections out of other collections. Comprehensions allow one to express computations...
Card Puncher Data Processing
Python - List

Lists are used to store a collection of different pieces of information in a sequence order of under a single variable name. A list can be used to implement a . A list is mutable whereas a tuple is not....
Card Puncher Data Processing
Python - Set

Implementation of a set data structure in Python. Sets are mutable. There is a non-mutable version of set called frozenset. The elements of a set arenot mutable. A set then cannot contain a list since...
Card Puncher Data Processing
Python - Tuple

Tuples are: an ordered sequence of elements, just like lists. immutable so they can be elements of sets but they can contain mutable objects. A tuple consists of a number of values separated...



Share this page:
Follow us:
Task Runner