In Python range() is an inbuilt function, it represents an immutable sequence of numbers, you may mostly use it for looping a specific number of times in for loops.
Syntax:range(stop)
range(start,stop)
range(start,stop,step)
- start: It is a value of the start parameter, zero by default. Optional
- stop: It is an value defining where to stop: Mandatory
- step: It is an value for inclementation. Optional
- The arguments to range function i.e. stop, start and stop can only byte int values.
- Only stop argument is mandatory.
- If you use syntax: range(stop), the start value is 0.
- If you use syntax: range(start,stop), the step value for increment is 1.
- If you use syntax: range(start,stop,step), the step value for cannot be 0
Range with one constructor argument: range(stop)
Example 1:no = range(5)
for i in no:
print("Hello Python!")
Output:
Hello Python!
Hello Python!
Hello Python!
Hello Python!
Hello Python!
Example 2:
print(list(range(10)))
Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Range with two constructor argument: range(start, stop)
Example 3:for i in range(1, 3):
print("Hello Python!")
Output:
Hello Python!
Hello Python!
Example 4:
print(list(range(1, 11)))
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Range with three constructor argument: range(start, stop, step)
Example 5:for i in range(1, 11, 1):
print(i*2)
Output:
2
4
6
8
10
12
14
16
18
20
Example 6:
print(list(range(0, 40, 5)))
Output:
[0, 5, 10, 15, 20, 25, 30, 35]
Note: if you provide step as zero, you will get ValueError error,
Traceback (most recent call last):
File "/Users/code2care/PycharmProjects/pythonProject/main.py", line 1, in &t;module>
print(list(range(10,20,0)))
ValueError: range() arg 3 must not be zero
Python documentation for range: https://docs.python.org/3/library/stdtypes.html?highlight=range#range
Facing issues? Have Questions? Post them here! I am happy to answer!
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org
- How to convert int to ASCII in Python
- How to make use of SQLite Module in Python?
- Split a String into Sub-string and Parse in Python
- Python: Pandas Rename Columns with List Example
- How to run Python file from Mac Terminal
- How to Exit a Loop in Python Code
- Python: How to Plot a Histogram using Matplotlib and data as list
- MD5 Hashing in Python
- Jupyter: Safari Cant Connect to the Server localhost:8888/tree
- Fix: AttributeError: str object has no attribute decode. Did you mean: encode?[Python]
- How to Read a binary File with Python
- How to add two float numbers in Python
- Python: How to install YAML Package
- Python: How to Save Image from URL
- What is Markdown in Jupyter Notebook with Examples
- How to change the Python Default version
- 33: Python Program to send an email vid GMail
- How to comment code in Python
- How to Fix AttributeError in Python
- Fix: error: Jupyter command `jupyter-nbconvert` not found [VSCode]
- How to comment out a block of code in Python
- List of All 35 Reserved Keywords in Python Programming Language 3.11
- Import Other Python Files Examples
- Python: How to add Progress Bar in Console with Examples
- 3 Ways to convert bytes to String in Python
- Download a SSL Certificate from a URL in Terminal - Bash
- Command: How to scp a file to remote server location? - HowTos
- SharePoint installation - Appfabric installation failed because installer MSI returned with error code:1603 - SharePoint
- Trigger Flow on selected Listitem from SharePoint view - create button with JSON column formatting - SharePoint
- Restore deleted Office 365 SharePoint group site - SharePoint
- Convert Java String to JSON Object using Jackson - Java
- How to Run Terminal As Admin on Mac - MacOS
- Remove git config at Local, Global or System Levels? - Git