range() In Python

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 39:- range() In Python

In Python, the range() function is used to generate a sequence of numbers. It is commonly used with for loops to iterate over a specific range of numbers. The range() function can take up to three arguments:

  1. start: The starting value of the sequence (inclusive). If not specified, the default value is 0.
  2. stop: The ending value of the sequence (exclusive). The sequence will generate numbers up to, but not including, this value.
  3. step: The increment between each number in the sequence. If not specified, the default value is 1.

The syntax for the range() function is as follows:

pythonCopy code

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

Here are some examples of using the range() function:

Example 1: Generating a sequence of numbers from 0 to 4 (exclusive):

pythonCopy code

for num in range(5):    print(num)

Output:

Copy code

0 1 2 3 4

Example 2: Generating a sequence of even numbers from 0 to 10 (exclusive):

pythonCopy code

for num in range(0, 10, 2):    print(num)

Output:

Copy code

0 2 4 6 8

Example 3: Generating a sequence of numbers in reverse order from 10 to 1 (exclusive):

pythonCopy code

for num in range(10, 0, -1):    print(num)

Output:

Copy code

10 9 8 7 6 5 4 3 2 1

In each example, the range() function is used to generate a sequence of numbers based on the specified arguments, and then the for loop iterates over the generated sequence to print each number. The range() function is a useful tool for creating number sequences and controlling the iteration of loops in Python.

6. Loops

0 Comments

Start the conversation!

Be the first to share your thoughts

Frequently Asked Questions About Sciaku Courses & Services

Quick answers to common questions about our courses, quizzes, and learning platform

Didn't find what you're looking for?

help_center Contact Support