Skip to main content
Home
  • Tutorials
    • Quality Assurance
    • Software Development
    • Machine Learning
    • Data Science
  • About Us
  • Contact
programsbuzz facebook programsbuzz twitter programsbuzz linkedin
  • Log in

Main navigation

  • Tutorials
    • Quality Assurance
    • Software Development
    • Machine Learning
    • Data Science
  • About Us
  • Contact

Python range() Function

Profile picture for user devanshi.srivastava
Written by devanshi.srivastava on 06/07/2021 - 17:17

It returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and stops before a specified number.range() is commonly used in for looping hence, knowledge of same is key aspect when dealing with any kind of Python code. Most common use of range() function in Python is to iterate sequence type (List, string etc.. ) with for and while loop.

range() function only works with the integers i.e. whole numbers.

Syntax:

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

range() Parameters:

start
Optional. An integer number specifying at which position to start
stop
 Required . Endpoint of the sequence.
step Optional. Integer value which determines the increment between each integer in the sequence

All argument must be integers. User can not pass a string or float number or any other type in a start, stop and step argument of a range().

All three arguments can be positive or negative.

Example:

x = range(1, 30, 5)
for n in x:
    print(n, end = ' ')

Output: 1 6 11 16 21 26

Incrementing with the help of range() by positive step:

If we want to increment, then we need step to be a positive number.

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

Output: 2 4 6 8 10 12 14 16 18 20 22 24 26

Decrementing with the help of range() by negative step :

If we want to decrement, then we  need step to be a negative number.

for i in range(25, -6, -3):
    print(i, end =" ")
Output: 25 22 19 16 13 10 7 4 1 -2 -5

Concatenation of two range() functions:

The chain() method is used to print all the values in iterable targets one after another mentioned in its arguments.

from itertools import chain
  
print("Result")
res = chain(range(5), range(10, 20, 2))
  
for i in res:
    print(i, end=" ")

Output:  0 1 2 3 4 10 12 14 16 18

range() indexing and slicing:

Indexing

It supports both positive and negative indices.

r = range(0,10)
print(r[9])

Output: 9

Slicing

It implies accessing a portion from range().

for i in range(10)[3:8]:
    print(i, end=' ')

Output: 3 4 5 6 7

Related Content
Python Tutorial
Python for loop with range() function
Python Function
Tags
Python
  • Log in or register to post comments

Choose Your Technology

  1. Agile
  2. Apache Groovy
  3. Apache Hadoop
  4. Apache HBase
  5. Apache Spark
  6. Appium
  7. AutoIt
  8. AWS
  9. Behat
  10. Cucumber Java
  11. Cypress
  12. DBMS
  13. Drupal
  14. GitHub
  15. GitLab
  16. GoLang
  17. Gradle
  18. HTML
  19. ISTQB Foundation
  20. Java
  21. JavaScript
  22. JMeter
  23. JUnit
  24. Karate
  25. Kotlin
  26. LoadRunner
  27. matplotlib
  28. MongoDB
  29. MS SQL Server
  30. MySQL
  31. Nightwatch JS
  32. PactumJS
  33. PHP
  34. Playwright
  35. Playwright Java
  36. Playwright Python
  37. Postman
  38. Project Management
  39. Protractor
  40. PyDev
  41. Python
  42. Python NumPy
  43. Python Pandas
  44. Python Seaborn
  45. R Language
  46. REST Assured
  47. Ruby
  48. Selenide
© Copyright By iVagus Services Pvt. Ltd. 2023. All Rights Reserved.

Footer

  • Cookie Policy
  • Privacy Policy
  • Terms of Use