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

Comments: 0

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?