Python range function example - onlinetutorialspoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
The range() is a built-in function in Python that returns the sequence of numbers between the specified range. This function is preferred over a list when we want a series of integers that needs to be generated at runtime.
The signature for the range() function is as shown below. However, there are two variants in the range function.
range(stop)
range(start, stop, step)

There are two different ways in which this function can be used. The first one shows when we are only passing one parameter stop. The second one shows when we use starting, ending, and step. However, step is an optional parameter.
However, it returns a sequence of numbers between the given range.
Example 1: In this example, let us demonstrate the working of the range function with a single parameter.
#Using range
r=range(10)
# Printing the range
print (r)
#Printing range of values as tuple
print(tuple(r))

Output
range(0, 10)
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

Example 2: In this case, we will generate a sequence of numbers with specific start and end values and step. Here, we are printing it as a list.
#Using range
r=range(111,120,2)
#Printing range and values as a list
print (r)
print(list(r))

Output
range(111, 120,2)
[111, 113, 115, 117, 119]

Example 3: For this example, we will take a string and the end value will be the length of the string.
#Initializing
s="python"
#Using range
r=range(0,len(s))
#Printing
print (r)
print(list(r))

Output
range(0, 6)
[0, 1, 2, 3, 4, 5]

The range() function returns the sequence of numbers between the given range of numbers.
Python Built-in Functions
Happy Learning 🙂

Python any function example
Python Callable function Example
Python compile function example
Python exec function example
How to use fromkeys for a Dictionary in Python
What are Python default function parameters ?
What are different Python Data Types
How to get elements from Dict in Python
Python – How to filter a list in python ?
What is Python NumPy Library
How to perform Intersection Operation on a Set in Python
How to perform Union operation on a Set in Python
How to set default values to a Dictionary in Python
How to use update method in a Set in Python
How to find Symmetric Difference of two Sets in Python

Python any function example
Python Callable function Example
Python compile function example
Python exec function example
How to use fromkeys for a Dictionary in Python
What are Python default function parameters ?
What are different Python Data Types
How to get elements from Dict in Python
Python – How to filter a list in python ?
What is Python NumPy Library
How to perform Intersection Operation on a Set in Python
How to perform Union operation on a Set in Python
How to set default values to a Dictionary in Python
How to use update method in a Set in Python
How to find Symmetric Difference of two Sets in Python
 


Δ
 Python – Introduction
 Python – Features
 Python – Install on Windows
 Python – Modes of Program
 Python – Number System
 Python – Identifiers
 Python – Operators
 Python – Ternary Operator
 Python – Command Line Arguments
 Python – Keywords
 Python – Data Types
 Python – Upgrade Python PIP
 Python – Virtual Environment
 Pyhton – Type Casting
 Python – String to Int
 Python – Conditional Statements
 Python – if statement
 Python – *args and **kwargs
 Python – Date Formatting
 Python – Read input from keyboard
 Python – raw_input
 Python – List In Depth
 Python – List Comprehension
 Python – Set in Depth
 Python – Dictionary in Depth
 Python – Tuple in Depth
 Python – Stack Datastructure
 Python – Classes and Objects
 Python – Constructors
 Python – Object Introspection
 Python – Inheritance
 Python – Decorators
 Python – Serialization with Pickle
 Python – Exceptions Handling
 Python – User defined Exceptions
 Python – Multiprocessing
 Python – Default function parameters
 Python – Lambdas Functions
 Python – NumPy Library
 Python – MySQL Connector
 Python – MySQL Create Database
 Python – MySQL Read Data
 Python – MySQL Insert Data
 Python – MySQL Update Records
 Python – MySQL Delete Records
 Python – String Case Conversion
 Howto – Find biggest of 2 numbers
 Howto – Remove duplicates from List
 Howto – Convert any Number to Binary
 Howto  – Merge two Lists
 Howto – Merge two dicts
 Howto – Get Characters Count in a File
 Howto – Get Words Count in a File
 Howto – Remove Spaces from String
 Howto –  Read Env variables
 Howto – Read a text File
 Howto – Read a JSON File
 Howto –  Read Config.ini files
 Howto – Iterate Dictionary
 Howto – Convert List Of Objects to CSV
 Howto – Merge two dict in Python
 Howto – create Zip File
 Howto – Get OS info
 Howto – Get size of Directory
 Howto – Check whether a file exists
 Howto –  Remove key from dictionary
 Howto – Sort Objects
 Howto – Create or Delete Directories
 Howto – Read CSV File
 Howto – Create Python Iterable class
 Howto – Access for loop index
 Howto – Clear all elements from List
 Howto – Remove empty lists from a List
 Howto – Remove special characters from String
 Howto – Sort dictionary by key
 Howto – Filter a list