0% found this document useful (0 votes)
2 views7 pages

Range Function Reference

its all about python range function

Uploaded by

Zia Ullah
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

Range Function Reference

its all about python range function

Uploaded by

Zia Ullah
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python range() Function

Complete Syntax & Usage Reference

1. Syntax Overview
The range() function generates an immutable sequence of integers. It supports three calling signatures:

Form Signature Description

1-arg range(stop) 0 up to (not including) stop, step = 1

2-arg range(start, stop) start up to (not including) stop, step = 1

3-arg range(start, stop, start up to stop, incrementing by step


step)

✓ All three arguments must be integers. Floats raise a TypeError.

2. Parameters
Parameter Default Require Notes
d

start 0 No First value in the sequence

stop — Yes Exclusive upper bound; sequence never includes this


value

step 1 No Increment (positive or negative, never 0)

3. Basic Examples
Single argument — range(stop)

range(5) # → 0, 1, 2, 3, 4
list(range(5)) # → [0, 1, 2, 3, 4]

range(1) # → [0]

range(0) # → [] (empty)

Two arguments — range(start, stop)

list(range(2, 7)) # → [2, 3, 4, 5, 6]

list(range(0, 10)) # → [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

list(range(5, 5)) # → [] (start == stop → empty)

list(range(7, 3)) # → [] (start > stop, step=1 → empty)

Three arguments — range(start, stop, step)

list(range(0, 10, 2)) # → [0, 2, 4, 6, 8] (even numbers)

list(range(1, 10, 2)) # → [1, 3, 5, 7, 9] (odd numbers)

list(range(0, 30, 5)) # → [0, 5, 10, 15, 20, 25] (multiples of 5)

list(range(10, 0, -1)) # → [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

list(range(10, 0, -2)) # → [10, 8, 6, 4, 2]

list(range(-5, 5)) # → [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4]

4. Common Looping Patterns


Classic for-loop

for i in range(5):

print(i) # prints 0 1 2 3 4

Countdown
for i in range(10, 0, -1):

print(i) # 10 9 8 7 ... 1

Repeat N times (index unused)

for _ in range(3):

print("hello") # prints "hello" three times

Index-based list access

fruits = ['apple', 'banana', 'cherry']

for i in range(len(fruits)):

print(i, fruits[i]) # 0 apple 1 banana 2 cherry

Prefer enumerate() over range(len(...))

# Pythonic alternative to range(len(...))

for i, fruit in enumerate(fruits):

print(i, fruit)

# enumerate also accepts a start value

for i, fruit in enumerate(fruits, start=1):

print(i, fruit) # 1 apple 2 banana 3 cherry

5. Combining range() with Built-ins


# sum

sum(range(101)) # → 5050 (Gauss's trick)


# list / tuple / set

list(range(5)) # → [0, 1, 2, 3, 4]

tuple(range(5)) # → (0, 1, 2, 3, 4)

set(range(5)) # → {0, 1, 2, 3, 4}

# reversed()

list(reversed(range(5))) # → [4, 3, 2, 1, 0]

# zip with another iterable

list(zip(range(3), 'abc')) # → [(0,'a'), (1,'b'), (2,'c')]

# in operator (O(1) — no list conversion needed)

7 in range(10) # → True

15 in range(0, 20, 2) # → False (15 is odd)

# len()

len(range(0, 100, 3)) # → 34

# slicing a range returns another range

range(10)[2:8:2] # → range(2, 8, 2)

6. range() in List / Dict / Set Comprehensions


# Squares

squares = [x**2 for x in range(1, 6)]


# → [1, 4, 9, 16, 25]

# Filtered comprehension

evens = [x for x in range(20) if x % 2 == 0]

# → [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

# Dict comprehension

sq_map = {x: x**2 for x in range(6)}

# → {0:0, 1:1, 2:4, 3:9, 4:16, 5:25}

# Set comprehension

mod3 = {x % 3 for x in range(10)}

# → {0, 1, 2}

# Nested ranges (multiplication table)

table = [[r*c for c in range(1,6)] for r in range(1,6)]

7. Negative Steps & Edge Cases


Expression Result Why

range(5, 0, -1) [5, 4, 3, 2, 1] Counts down; stop is exclusive

range(0, 10, 0) ValueError step cannot be 0

range(5, 0) [] start > stop with positive step → empty

range(0, 5, -1) [] start < stop with negative step → empty

range(10**18) Valid range object range is lazy — no memory allocated


8. range Object Properties & Methods
r = range(2, 20, 3) # → 2, 5, 8, 11, 14, 17

[Link] # 2

[Link] # 20

[Link] # 3

len(r) # 6

r[0] # 2 (indexing)

r[-1] # 17 (negative indexing)

r[1:4] # range(5, 14, 3) (slicing returns a range)

[Link](8) # 2 (position of value 8)

[Link](5) # 1 (how many times 5 appears)

# Equality: two ranges are equal if they produce the same sequence

range(0, 6, 2) == range(0, 5, 2) # True (both give 0,2,4)


9. Quick-Reference Cheat Sheet
Goal Expression

0 to n-1 range(n)

1 to n range(1, n+1)

a to b inclusive range(a, b+1)

Even numbers 0–n range(0, n+1, 2)

Odd numbers 1–n range(1, n+1, 2)

Multiples of k up to n range(0, n+1, k)

Countdown n to 1 range(n, 0, -1)

Countdown n to 0 range(n, -1, -1)

Reverse of range(n) range(n-1, -1, -1)

Every k-th element range(start, stop, k)

Check membership (fast) x in range(start, stop, step)

Length of sequence len(range(start, stop, step))

Python range() Function Reference • Applies to Python 3.x • range() returns a lazy sequence object

You might also like