banner



How To Set Range In Python For Loop

Summary: in this tutorial, you lot'll learn virtually the Python for loop and how to employ information technology to execute a code cake a fixed number of times.

Introduction to Python for loop statement with the range() function

In programming, you frequently desire to execute a block of code multiple times. To do and so, you use a for loop.

The following illustrates the syntax of a for loop:

            

for index in range(n): statement

Code language: Python ( python )

In this syntax, the alphabetize is called a loop counter. And n is the number of times that the loop volition execute the argument.

The proper name of the loop counter doesn't have to exist index, you tin can use whatever proper noun yous desire.

The range() is a built-in function in Python. It's like the print() role in the sense that information technology'southward always bachelor in the program.

The range(n) generates a sequence of northward integers starting at zero. It increases the value by one until it reaches north.

So the range(northward) generates a sequence of numbers: 0,1, 2, …n-1. Note that it's ever short of the final number (northward).

The following diagram illustrates the for loop argument:

Python for loop

The following example shows how to utilise the for loop with the range() function to display five numbers from 0 to four to the screen:

            

for alphabetize in range(5): print(index)

Code language: Python ( python )

Output:

            

0 1 2 3 4

Lawmaking language: Python ( python )

In this example, the for loop executes the argument impress(index) exactly five times.

If y'all want to show 5 numbers from 1 to 5 to the screen, you tin exercise something similar this:

            

for index in range(5): print(alphabetize + 1)

Code language: Python ( python )

Output:

            

1 ii 3 4 5

Code linguistic communication: Python ( python )

In this example, we increase the index by one in each iteration and print it out. Nevertheless, at that place'southward a improve way to exercise it.

Specifying the starting value for the sequence

By default, the range() role uses zero as the starting number for the sequence.

In addition, the range() function allows you to specify the starting number like this:

            

range(start, terminate)

Code language: Python ( python )

In this syntax, the range() function increases the start value by i until it reaches the stop value.

The following example uses a for loop to testify 5 numbers, from 1 to 5 to the screen:

            

for index in range(1, half dozen): print(index)

Code language: Python ( python )

Output:

            

ane 2 iii four 5

Code linguistic communication: Python ( python )

Specifying the increment for the sequence

By default, the range(get-go, stop) increases the showtime value past one in each loop iteration.

To increase the beginning value by a different number, you lot utilize the following class of the range() role:

            

range(beginning, stop, footstep)

Lawmaking language: Python ( python )

In this form, you can specify the value that the range() function should increase.

The following case shows all the odd numbers from 0 to 10:

            

for alphabetize in range(0, xi, two): impress(index)

Lawmaking linguistic communication: Python ( python )

Output:

            

0 ii 4 6 eight ten

Code linguistic communication: Python ( python )

Using Python for loop to calculate the sum of a sequence

The following instance uses the for loop statement to calculate the sum of numbers from ane to 100:

            

sum = 0 for num in range(101): sum += num print(sum)

Code language: Python ( python )

Output:

            

5050

Code language: Python ( python )

How it works.

  • First, the sum is initialized to zero.
  • Second, the sum is added with the number from i to 100 in each iteration.
  • Finally, show the sum to the screen.

By the way, if you're a mathematician, you tin can use the simple formula:

            

northward = 100 sum = n * (n+one)/2 print(sum)

Code language: Python ( python )

Summary

  • Use the for loop statement to run a code cake a fixed number of times.
  • Use the range(start, finish, step) to customize the loop.

Did yous notice this tutorial helpful ?

How To Set Range In Python For Loop,

Source: https://www.pythontutorial.net/python-basics/python-for-range/

Posted by: deciccobeirl1940.blogspot.com

0 Response to "How To Set Range In Python For Loop"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel