InfoHeap
Tech tutorials, tips, tools and more
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Python

    Python strings

    • Python string quick start tutorial
    • Python string split examples
    • Python string lowercase and uppercase
    • Python string contains check
    • int to string
    • string to int

    Python List (Array)

    • list basics
    • list append, prepend
    • list remove an item
    • merge lists
    • slice of list
    • array clone
    • initialize list with same value
    • value in list check

    Python Dictionary

    • dictionary basics
    • Python iterate dictionary
    • dictionary get key with default
    • sort dictionary by values

    Python control flow

    • if-elif-else
    • for loop

    Python Regex

    • re search() vs match()
    • re.match()
    • re.search()
    • re.sub()

    Python Cookbook

    • Command line - run python webserver
    • How to find python package's file location
    • Python etl petl - read table from csv file
    • Python file read write examples

    Python built-in functions

    • filter
    • filter vs ifilter
    • itertools ifilter
    • itertools imap
    • len
    • map
    • print
    • range
    • rstrip
    • type
    • xrange
     
    • Home
    • > Tutorials
    • > Python

    Python xrange examples

    on Feb 11, 2016

    Python xrange can be used to generate a sequence of numbers with desired start number and gap. It is often used in for loops. It is more efficient than range for large data.

    Usage:

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

    Here are some examples.

    xrange to generate 0 to N-1

    Note that xrange does not return a list.

    a = xrange(10)
    print "type=%s" % (type(a))
    print a
    print "==outcome of for loop==="
    for i in xrange(10):
      print i
    
    type=<type 'xrange'>
    xrange(10)
    ==outcome of for loop===
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    Env: Python 2.7.16

    xrange with start, stop

    This generates sequence from start to stop-1

    for i in xrange(5,10):
      print i
    5
    6
    7
    8
    9
    
    Env: Python 2.7.16

    xrange with start, stop, step

    This generates sequence start, start+step,… Ntoe that outcome will not include stop.

    for i in xrange(5,10, 3):
      print i
    5
    8
    
    Env: Python 2.7.16

    Additional notes

    Note that if you are working on large data then it would be more memory efficient to use xrange than range.

    Suggested posts:

    1. Python filter vs ifilter
    2. Python map examples
    3. Python itertools ifilter examples
    4. Python itertools imap examples
    5. Python range examples
    6. Python filter list/iterable examples
    7. Python for loop examples
    8. python print examples
    Share this article: share on facebook share on linkedin tweet this submit to reddit
    Posted in Tutorials | Tagged Python, Python array, Python built-in functions, Tutorials

    Follow InfoHeap

    facebook
    twitter
    googleplus
    • Browse site
    • Article Topics
    • Article archives
    • Recent Articles
    • Contact Us
    • Omoney
    Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries

    Copyright © 2023 InfoHeap.

    Powered by WordPress