Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • Python - Get Started
  • What is Python?
  • Where to use Python?
  • Python Version History
  • Install Python
  • Python - Shell/REPL
  • Python IDLE
  • Python Editors
  • Python Syntax
  • Python Keywords
  • Python Variables
  • Python Data Types
  • Number
  • String
  • List
  • Tuple
  • Set
  • Dictionary
  • Python Operators
  • Python Conditions - if, elif
  • Python While Loop
  • Python For Loop
  • User Defined Functions
  • Lambda Functions
  • Variable Scope
  • Python Modules
  • Module Attributes
  • Python Packages
  • Python PIP
  • __main__, __name__
  • Python Built-in Modules
  • OS Module
  • Sys Module
  • Math Module
  • Statistics Module
  • Collections Module
  • Random Module
  • Python Generator Function
  • Python List Comprehension
  • Python Recursion
  • Python Built-in Error Types
  • Python Exception Handling
  • Python Assert Statement
  • Define Class in Python
  • Inheritance in Python
  • Python Access Modifiers
  • Python Decorators
  • @property Decorator
  • @classmethod Decorator
  • @staticmethod Decorator
  • Python Dunder Methods
  • CRUD Operations in Python
  • Python Read, Write Files
  • Regex in Python
  • Create GUI using Tkinter
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

Python - range Function

In Python, the range() method returns an immutable sequence of numbers. It can be used to control the repetition of a block in the for loop.

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

All three parameters should be integers. The sequence starts with 0, by default, unless the [start] parameter is provided. The only mandatory parameter for the above function is stop. The last number in the sequence is the number before stop. The numbers in between are incremented with the [step] value, which is 1 by default.

range(5) will generate 0,1,2,3,4

range(1,10) will generate 1,2,3,4,5,6,7,8,9

range(10,21,2) will generate 10,12,14,16,18,20

We can run the for loop over a range to print each number in a given range.

Example: for loop with range()
x=range(5)
for num in x:
    print(num)
Output
0
1
2
3
4

The range() function can also be used directly with the for loop.

Example: for loop with range()
for num in range(5):
    print(num)

The following code computes the factorial value of a number taken as input. The factorial value of a certain number is the product of all numbers between 1 and the number itself. We use the range() function to obtain the sequence. The body of the for loop performs cumulative multiplication of all numbers in the range.

Example:
num=int(input("Enter a number: "))
fact=1
for x in range(1, num+1):
    fact=fact*x
print ("Factorial of {} is {}".format(num,fact))
Output
Enter a number: 4
Factorial of 4 is 24
TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.