Close Menu
Soshace Digital Blog

    Subscribe to Updates

    Get The Latest News, Updates, And Amazing Offers

    What's Hot
    JavaScript

    Task Estimation

    Java

    Guide to Control Flow Statements in Java

    LinkedIn

    Transforming LinkedIn Connections into Effective Sales Leads

    Important Pages:
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Today's Picks:
    • Scaling Success: Monitoring Indexation of Programmatic SEO Content
    • Leveraging Influencers: Key Drivers in New Product Launches
    • How Privacy-First Marketing Will Transform the Industry Landscape
    • The Impact of Social Proof on Thought Leadership Marketing
    • Balancing Value-Driven Content and Promotional Messaging Strategies
    • Top Influencer Marketing Platforms to Explore in 2025
    • Emerging Trends in Marketing Automation and AI Tools for 2023
    • Strategies to Mitigate Duplicate Content in Programmatic SEO
    Wednesday, January 21
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Soshace Digital Blog
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Services
    • SaaS & Tech

      Maximizing Efficiency: How SaaS Lowers IT Infrastructure Costs

      August 27, 2025

      Navigating Tomorrow: Innovations Shaping the Future of SaaS

      August 27, 2025

      Maximizing Impact: Strategies for SaaS & Technology Marketing

      August 27, 2025
    • AI & Automation

      Enhancing Customer Feedback Analysis Through AI Innovations

      August 27, 2025

      Navigating the Impact of AI on SEO and Search Rankings

      August 27, 2025

      5 Automation Hacks Every Home Service Business Needs to Know

      May 3, 2025
    • Finance & Fintech

      Critical Missteps in Finance Marketing: What to Avoid

      August 27, 2025

      Analyzing Future Fintech Marketing Trends: Insights Ahead

      August 27, 2025

      Navigating the Complex Landscape of Finance and Fintech Marketing

      August 27, 2025
    • Legal & Compliance

      Exploring Thought Leadership’s Impact on Legal Marketing

      August 27, 2025

      Maximizing LinkedIn: Strategies for Legal and Compliance Marketing

      August 27, 2025

      Why Transparency Matters in Legal Advertising Practices

      August 27, 2025
    • Medical Marketing

      Enhancing Online Reputation Management in Hospitals: A Guide

      August 27, 2025

      Analyzing Emerging Trends in Health and Medical Marketing

      August 27, 2025

      Exploring Innovative Content Ideas for Wellness Blogs and Clinics

      August 27, 2025
    • E-commerce & Retail

      Strategic Seasonal Campaign Concepts for Online and Retail Markets

      August 27, 2025

      Emerging Trends in E-commerce and Retail Marketing Strategies

      August 27, 2025

      Maximizing Revenue: The Advantages of Affiliate Marketing for E-Commerce

      August 27, 2025
    • Influencer & Community

      Leveraging Influencers: Key Drivers in New Product Launches

      August 27, 2025

      Top Influencer Marketing Platforms to Explore in 2025

      August 27, 2025

      Key Strategies for Successful Influencer Partnership Negotiations

      August 27, 2025
    • Content & Leadership

      The Impact of Social Proof on Thought Leadership Marketing

      August 27, 2025

      Balancing Value-Driven Content and Promotional Messaging Strategies

      August 27, 2025

      Analyzing Storytelling’s Impact on Content Marketing Effectiveness

      August 27, 2025
    • SEO & Analytics

      Scaling Success: Monitoring Indexation of Programmatic SEO Content

      August 27, 2025

      Strategies to Mitigate Duplicate Content in Programmatic SEO

      August 27, 2025

      Effective Data Visualization Techniques for SEO Reporting

      August 27, 2025
    • Marketing Trends

      How Privacy-First Marketing Will Transform the Industry Landscape

      August 27, 2025

      Emerging Trends in Marketing Automation and AI Tools for 2023

      August 27, 2025

      Maximizing ROI: Key Trends in Paid Social Advertising

      August 27, 2025
    Soshace Digital Blog
    Blog / Python / Python range() Explained and Visualized
    Programming

    Python range() Explained and Visualized

    Denis KryukovBy Denis KryukovSeptember 20, 2019No Comments8 Mins Read
    Facebook Twitter Pinterest Telegram LinkedIn Tumblr Email Reddit
    Python range() Explained and Visualized
    Explaining and visualizing the range(0 function in Python with examples
    Share
    Facebook Twitter LinkedIn Pinterest Email Copy Link
    artwork depicting a stylized Python range() button
    Explaining and visualizing the range(0 function in Python with examples

    We’re continuing to delve into the inner workings of various Python functions, modules, and libraries. Having explored enumerate(), we now delve into another essential function — range().

    In this article, we’ll explore the functionality of range() and highlight its use cases — and you’ll know how to use range() in Python efficiently, appreciating both its strong sides and limitations. range()-related questions may often pop up during technical interviews — have you checked our Python interview questions yet?

    (As always, the code in this article is from Python 3)

    So What Does range() Actually Do in Python?

    range() is a built-in function, meaning that Python comes prepackaged with it. This function can create a sequence of numbers (this is called a range object) and return it. Naturally, you can utilize this set of numbers for various purposes: as demonstrated below, range() actually accompanies loops very well.

    Here’s a more technical explanation provided py the Python help() module:

    Return an object that produces a sequence of integers from start (inclusive) to stop (exclusive) by step. range(i, j) produces i, i+1, i+2, …, j-1. start defaults to 0, and stop is omitted! range(4) produces 0, 1, 2, 3. These are exactly the valid indices for a list of 4 elements. When a step is given, it specifies the increment (or decrement).

    Syntax of range() in Python

    Let’s visualize the most simple scenario: range(5)

    Visualization of how range() works in Python
    Ranging the sequence…

    The syntax of the range() function is trivial — we call the function and input the parameters:

    range([start], stop, [step])

    Let’s take a closer look at what each parameter does:

    1. start is an optional parameter that defines the starting point of our sequence. When not specified, it defaults to 0.
    2. stop is a required parameter that defines the endpoint of our sequence.
    3. step is an optional parameter that defines the step size (i.e. the number of integers that will be omitted between individual integers in the sequence). When not specified, it defaults to 0.

    Combining range() with List() in Python

    Visualization of how lists and the range function work in Python
    Ranging and listing

    We might want to showcase which numbers are actually used in the sequence when the parameter is, say, 5. To do this, we can use another built-in Python function — list — to create a list of numbers that a function call range(5) will produce:

    print(list(range(5)))

    This will output:

    [0, 1, 2, 3, 4]

    Combining Range() with for Loops in Python

    Visualization of how for loops and the range function work in Python
    Looping and ranging

    Alternatively, we can utilize a for loop — it will allow us to execute the given command a number of times. This process is called iteration and we can use various data structures (e.g. strings or lists) to specify the exact amount of ”repetition.” Naturally, we can also the range() function for this purpose — it will basically run the command N times.

    for i in range(5):
        print(i)
    

    Thanks to Python’s clear syntax, it’s easy to memorize how for loops work like this: ”For every element/part/item in something, do this.” However, the output will be vertical (each print call will end with a newline) — some developers may find it not convenient or readable. To print the output horizontally, we can add the end parameter to our print function:

    for i in range(5):
        print(i, end=', ')
    

    This way, the result would be easier to read:

    0, 1, 2, 3, 4,

    A Note About the ‘Stop’ Parameter of range() (a.k.a. Inclusive Ranges in Python)

    At this point, we need to reiterate how indices work in almost every programming language. Since we didn’t specify the start parameter, it defaulted to 0 and our sequence became 0 — 5. You’d think that if we put this sequence in a list and print it, the list would look like this:

    [0, 1, 2, 3, 4, 5]

    However, the last index (i.e. the stop argument) isn’t included in the operation, so it’s useful to memorize the formula as range(from number X up to — but not including! — number Z).

    Let’s examine a more complicated scenario when we want to use all three arguments:

    Read More:  How To Secure Python Web App Using Bandit
    Visualization of how range() parameters work in Python
    Ranging and stopping

    The code in the previous sections (range(5)) actually received 3 parameters even though we only inputted one.

    Creating a Reverse range() Object in Python

    So far we’ve only been using positive integers to construct range objects that follow the normal (i.e. ascending) order — but what if we want to use range() in reverse order (i.e. backwards)? To implement this idea, we’ll need to make our third range() parameter — stop — negative. Let’s imagine that it’s December 31st and we’re only 10 seconds away from New Year — a countdown timer will help us, as its name suggests, count these 10 seconds down:

    print("The New Year is upon us!")
    for i in range(10, 0, -1):
        print(str(i) + '...')
    print("Happy New Year!")
    

    This will output:

    The New Year is upon us!
    10...
    9...
    8...
    7...
    6...
    5...
    4...
    3...
    2...
    1...
    Happy New Year!
    

    Note: To concatenate (i.e. combine two pieces of data) integers and ellipses (the ‘…’ symbols) we had to convert integers to string, calling the str() function on them — str(i) in our example. Otherwise, we would run into a TypeError:

    TypeError: unsupported operand type(s) for +: 'int' and 'str'

    range() in Python: Applying List Operations

    Thanks to the fact that range() returns a list, we can apply various list operations to it, including slicing; xrange(), on the other hand, doesn’t offer this functionality.

    r = range(10)
    sliced_r = r[3:6]
    another_sliced_r = r[5:]
    yet_another_sliced_r = r[:5]
    reversed_r = r[::-1]
    
    print(f"Here's the original range object: {r}")
    print(f"Here's a sliced range object: {sliced_r}")
    print(f"Here's another sliced range object: {another_sliced_r}")
    print(f"Here's yet another sliced range object: {yet_another_sliced_r}")
    print(f"Here's a reversed range object: {reversed_r}")
    

    This will output:

    Here's the original range object: range(0, 10)
    Here's a sliced range object: range(3, 6)
    Here's another sliced range object: range(5, 10)
    Here's yet another sliced range object: range(0, 5)
    Here's a reversed range object: range(9, -1, -1)
    

    range() vs xrange() in Python: What’s the Difference?

    Visualization of how range() and xrange() work in Python
    Ranging and… x-ranging?

    Python 3 brought a number of improvements, but the reasoning behind some of these changes wasn’t so clear. A good example is transition from xrange() (Python 2) to range() (Python 3). New language features are typically better than their predecessors, but xrange() still has the upper hand in some areas. Let’s explore how they compare:

    Read More:  Node.js Lesson 3: Node Package Manager

    range() vs xrange() in Python: What These Functions Return

    Although they share the same functionality, what they return is the main difference.

    • range() returns a list.
    • xrange() returns an xrange object .

    Despite the fact that their output is the same, the difference in their return values is an important point to consider — it influences the way these functions perform and the ways they can be used.

    range() vs xrange() in Python: How Fast These Functions Perform

    Performance is arguably the most important factor when we compare Technology A and Technology B. Both range() and xrange(), of course, are blazingly fast when dealing with smaller sets of data; when it comes to larger numbers, however, the difference in speed becomes apparent. One of the possible explanations for the switch from xrange() to range() is performance — but has performance actually increased?

    The built-in Python module timeit is optimal for testing the performance of small code snippets. Let’s use it to compare range() with xrange() (notice that we’re running these commands from the command line, not from the Python interpreter):

    # testing range()
    python -m timeit 'for i in range(1000000):' ' pass'
    10 loops, best of 3: 90.5 msec per loop
    # testing xrange()
    python -m timeit 'for i in xrange(1000000):' ' pass'
    10 loops, best of 3: 51.1 msec per loop
    

    Although these metrics may vary from computer to computer, the average speed difference is around 100%! This is possible thanks to the fact xrange() returns a generator object — it allows to only process the range of numbers that the user demands, saving time and resources. (Fun fact: This process is called ”lazy evaluation”).

    range() vs xrange() in Python: How Much Memory These Functions Use

    Similar to timeit, another built-in Python module called sys can help us see how much memory these two functions use. To do this, we’ll use the getsizeof() function from the sys module:

    import sys
    
    xr = xrange(1, 10000)
    r = range(1, 10000)
    
    size_xr = sys.getsizeof(xr)
    size_r = sys.getsizeof(r)
    
    print(f"The xrange() function uses {size_xr} bytes of memory.")
    print(f"The range() function uses {size_r} bytes of memory.")
    

    This will output:

    The xrange() function uses 24 bytes of memory.
    The xrange() function uses 80064 of memory.
    

    Conclusion

    The number of useful modules, libraries, and functions in Python may seem overwhelming at times. Now, however, you’ve mastered another awesome function — and now range() will always be a loyal companion in your programming sessions. 🙂

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Denis Kryukov
    • Website

    Related Posts

    Mastering REST APIs: Essential Techniques for Programmers

    December 18, 2024

    Crafting Interactive User Interfaces Using JavaScript Techniques

    December 17, 2024

    Effective Strategies for Utilizing Frameworks in Web Development

    December 16, 2024
    Leave A Reply Cancel Reply

    You must be logged in to post a comment.

    Stay In Touch
    • Facebook
    • Twitter
    • Pinterest
    • Instagram
    • YouTube
    • Vimeo
    Don't Miss
    LinkedIn November 24, 2024

    Analyzing LinkedIn Automation’s Impact on Lead Generation

    LinkedIn automation tools have reshaped lead generation strategies, enabling businesses to streamline outreach and engagement. However, while efficiency increases, potential risks such as diminished personalization and authenticity may undermine long-term relationship building.

    The Impact of Integrated Development Environments on Programming

    November 28, 2024

    Handling HEIC Images in Angular: A Comprehensive Tutorial

    April 20, 2023

    How Facebook Ads Can Skyrocket Your Home Services Bookings

    May 3, 2025

    Categories

    • AI & Automation
    • Angular
    • ASP.NET
    • AWS
    • B2B Leads
    • Beginners
    • Blogs
    • Business Growth
    • Case Studies
    • Comics
    • Consultation
    • Content & Leadership
    • CSS
    • Development
    • Django
    • E-commerce & Retail
    • Entrepreneurs
    • Entrepreneurship
    • Events
    • Express.js
    • Facebook Ads
    • Finance & Fintech
    • Flask
    • Flutter
    • Franchising
    • Funnel Strategy
    • Git
    • GraphQL
    • Home Services Marketing
    • Influencer & Community
    • Interview
    • Java
    • Java Spring
    • JavaScript
    • Job
    • Laravel
    • Lead Generation
    • Legal & Compliance
    • LinkedIn
    • Machine Learning
    • Marketing Trends
    • Medical Marketing
    • MSP Lead Generation
    • MSP Marketing
    • NestJS
    • Next.js
    • Node.js
    • Node.js Lessons
    • Paid Advertising
    • PHP
    • Podcasts
    • POS Tutorial
    • Programming
    • Programming
    • Python
    • React
    • React Lessons
    • React Native
    • React Native Lessons
    • Recruitment
    • Remote Job
    • SaaS & Tech
    • SEO & Analytics
    • Soshace
    • Startups
    • Swarm Intelligence
    • Tips
    • Trends
    • Vue
    • Wiki
    • WordPress
    Top Posts

    Tim&Tom JavaScript Frameworks

    Comics August 24, 2016

    Автоматическое добавление в задачи ссылок на коммиты

    Wiki June 25, 2016

    Monthly Digest of the Most Popular JS Github Repositories

    JavaScript July 26, 2019

    How to Write a Winning Software Development Project Pitch

    Startups September 23, 2019

    Subscribe to Updates

    Get The Latest News, Updates, And Amazing Offers

    About Us
    About Us

    Soshace Digital delivers comprehensive web design and development solutions tailored to your business objectives. Your website will be meticulously designed and developed by our team of seasoned professionals, who combine creative expertise with technical excellence to transform your vision into a high-impact, user-centric digital experience that elevates your brand and drives measurable results.

    7901 4th St N, Suite 28690
    Saint Petersburg, FL 33702-4305
    Phone: 1(877)SOSHACE

    Facebook X (Twitter) Instagram Pinterest YouTube LinkedIn
    Our Picks
    LinkedIn

    Maximizing LinkedIn: Strategic Lead Generation for Real Estate

    Interview

    How to Hire a Freelance Web Developer: Things to Look For and Avoid

    Node.js

    Create simple POS with React.js, Node.js, and MongoDB #12 : Getting started with React Table

    Most Popular

    Node.js Lesson 8: Inheritance from Errors, Error

    JavaScript

    The Bootstrapped Guide To Set Up & Hire A Remote Workforce

    Entrepreneurship

    Mastering IoT Development: A Strategic Guide for Professionals

    Development
    © 2026 Soshace Digital.
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions

    Type above and press Enter to search. Press Esc to cancel.