StudyLover
  • Home
  • Study Zone
  • Profiles
  • Typing Tutor
  • Contact us
  • Sign in
StudyLover Range
Download
  1. Python
  2. Pyhton MCA (Machine Learning using Python)
  3. Unit 3: Getting Started with Python: A Guide to Syntax, Data Structures, and OOP
Tuple : dict (Dictionary)
Unit 3: Getting Started with Python: A Guide to Syntax, Data Structures, and OOP

The range type in Python represents an immutable sequence of numbers. It is most commonly used for looping a specific number of times in for loops.

The main advantage of using range instead of a list or tuple of numbers is that it is very memory-efficient. A range object only stores the start, stop, and step values and calculates the individual numbers on the fly as you iterate over them. It doesn't generate and store the entire sequence of numbers in memory at once.


Creating a range Object

You can create a range object in three ways:

1.   range(stop): Creates a sequence of numbers starting from 0 up to (but not including) the stop number.

2.   range(start, stop): Creates a sequence of numbers from the start number up to (but not including) the stop number.

3.   range(start, stop, step): Creates a sequence of numbers from start to stop, incrementing by the step value.


Operations and Functions

Even though it's not a list, range behaves like an immutable sequence, so you can perform many of the same operations:

  • Indexing and Slicing: You can access individual numbers or a sub-sequence of numbers using their index.

  • Membership Testing (in, not in): You can efficiently check if a number is present in the sequence.

  • Built-in Functions: You can use functions like len(), sum(), min(), and max() on a range object.


range Attributes

A range object has three main attributes that reflect the values it was created with:

  • .start: The starting number of the sequence.

  • .stop: The number where the sequence stops (this value is not included).

  • .step: The difference between each number in the sequence.

 

# --- 1. Creating range Objects ---

# A range is an immutable sequence of numbers.

 

# range(stop): From 0 up to (but not including) 5.

range1 = range(5)

print("--- Creating range Objects ---")

print(f"range(5): {list(range1)}") # Convert to list to see all numbers

 

# range(start, stop): From 2 up to 10.

range2 = range(2, 10)

print(f"range(2, 10): {list(range2)}")

 

# range(start, stop, step): From 10 to 50, incrementing by 5.

range3 = range(10, 51, 5)

print(f"range(10, 51, 5): {list(range3)}")

 

# --- 2. Using range in a for loop ---

# This is the most common use case for range.

print("\n--- Using range in a for loop ---")

for i in range(5):

    print(f"Loop iteration: {i}")

 

# --- 3. Indexing and Slicing ---

# You can access elements in a range just like a list or tuple.

r = range(10, 21) # Numbers from 10 to 20

print("\n--- Indexing and Slicing ---")

print(f"Original range: {list(r)}")

print(f"First element (index 0): {r[0]}")   # Output: 10

print(f"Last element (index -1): {r[-1]}")  # Output: 20

print(f"Slice from index 2 to 5: {list(r[2:5])}") # Output: [12, 13, 14]

 

# --- 4. Membership Testing ---

# Checking if a number is in the sequence is very fast.

print("\n--- Membership Testing ---")

print(f"Is 15 in range(10, 21)? {15 in r}")

print(f"Is 25 in range(10, 21)? {25 in r}")

 

# --- 5. Built-in Functions with range ---

print("\n--- Built-in Functions with range ---")

print(f"Length of range(10, 21): {len(r)}")

print(f"Sum of range(10, 21): {sum(r)}")

print(f"Max of range(10, 21): {max(r)}")

print(f"Min of range(10, 21): {min(r)}")

 

# --- 6. range Attributes ---

# You can access the start, stop, and step values.

r_complex = range(5, 50, 10) # 5, 15, 25, 35, 45

print("\n--- range Attributes ---")

print(f"For range(5, 50, 10):")

print(f"Start: {r_complex.start}")

print(f"Stop: {r_complex.stop}")

print(f"Step: {r_complex.step}")

 

 

 

 

Tuple dict (Dictionary)
Our Products & Services
  • Home
Connect with us
  • Contact us
  • +91 82955 87844
  • Rk6yadav@gmail.com

StudyLover - About us

The Best knowledge for Best people.

Copyright © StudyLover
Powered by Odoo - Create a free website