Close Menu
Soshace Digital Blog

    Subscribe to Updates

    Get The Latest News, Updates, And Amazing Offers

    What's Hot
    Startups

    Maximizing Efficiency: The Crucial Role of Lean Startup Methodology

    Programming

    14. Уроки Node.js. Отладка скриптов. Часть 2.

    B2B Leads

    Maximizing B2B Leads: A Guide to Account-Based Marketing

    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, September 10
    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:  19. Node.js Lessons. Safe Way to a FS File and Path
    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:  Design Patterns Overview: Helping You Write Better Software

    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
    Beginners September 21, 2020

    Understanding Data Structures in JavaScript (Linked Lists)

    Understanding Linked Lists can be a difficult task when you are a beginner JavaScript developer since JavaScript does not provide built-in Linked List support. In an advanced language like JavaScript, we need to implement this data structure by scratch and, if you are unfamiliar with how this data structure works, the implementation part becomes that much more difficult.

    React Lesson 6: CSS and Animation

    January 4, 2020

    Navigating the Impact of AI on SEO and Search Rankings

    August 27, 2025

    14. Node.js Lessons. Script Debugging pt. 2

    October 4, 2016

    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

    3. Уроки Node.js. Менеджер пакетов для Node.js

    Programming September 7, 2016

    10. Уроки Node.js. Node.JS как веб-сервер

    Programming September 20, 2016

    Ultimate Reading List for Developers | 40 Web Development Books

    JavaScript July 22, 2019

    React Lesson 3: Exploring the React Component Lifecycle

    JavaScript November 14, 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
    Programming

    23. Уроки Node.js. Домены, “асинхронный try..catch”. Часть 3.

    Programming

    Уроки React. Урок 1, Введение.

    Interview

    Interview with Alex

    Most Popular

    This Is How I Created a Simple App Using React Routing

    JavaScript

    Building React Components Using Children Props and Context API

    JavaScript

    17. Уроки Node.js. Таймеры, Отличия от Браузера, ref и unref

    Programming
    © 2025 Soshace Digital.
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions

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