{"id":39,"date":"2024-01-24T13:58:38","date_gmt":"2024-01-24T13:58:38","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=39"},"modified":"2024-01-24T13:58:40","modified_gmt":"2024-01-24T13:58:40","slug":"python-for-loop-with-range","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/24\/python-for-loop-with-range\/","title":{"rendered":"Python for Loop with Range"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn about the Python&nbsp;<code>for<\/code>&nbsp;loop and how to use it to execute a code block a fixed number of times.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to Python for loop statement with the range() function<\/h2>\n\n\n\n<p>In programming, you often want to execute a block of code multiple times. To do so, you use a&nbsp;<code>for<\/code>&nbsp;loop.<\/p>\n\n\n\n<p>The following illustrates the syntax of a&nbsp;<code>for<\/code>&nbsp;loop:<code>for index in range(n): statement<\/code><small>Code language: Python (python)<\/small><\/p>\n\n\n\n<p>In this syntax, the&nbsp;<code>index<\/code>&nbsp;is called a&nbsp;<strong>loop counter<\/strong>. And&nbsp;<code>n<\/code>&nbsp;is the number of times that the loop will execute the&nbsp;<code>statement<\/code>.<\/p>\n\n\n\n<p>The name of the loop counter doesn\u2019t have to be&nbsp;<code>index<\/code>, you can use whatever name you want.<\/p>\n\n\n\n<p>The&nbsp;<code>range()<\/code>&nbsp;is a built-in function in Python. It\u2019s like the&nbsp;<code>print()<\/code>&nbsp;function in the sense that it\u2019s always available in the program.<\/p>\n\n\n\n<p>The&nbsp;<code>range(n)<\/code>&nbsp;generates a sequence of&nbsp;<code>n<\/code>&nbsp;integers starting at zero. It increases the value by one until it reaches&nbsp;<code>n<\/code>.<\/p>\n\n\n\n<p>So the&nbsp;<code>range(n)<\/code>&nbsp;generates a sequence of numbers:&nbsp;<code>0<\/code>,<code>1<\/code>,&nbsp;<code>2<\/code>, \u2026<code>n-1<\/code>. Note that it\u2019s always short of the final number (<code>n<\/code>).<\/p>\n\n\n\n<p>The following diagram illustrates the&nbsp;<code>for<\/code>&nbsp;loop statement:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/Python-for-loop.png\" alt=\"Python for loop\" class=\"wp-image-849\"\/><\/figure>\n\n\n\n<p>The following example shows how to use the&nbsp;<code>for<\/code>&nbsp;loop with the&nbsp;<code>range()<\/code>&nbsp;function to display 5 numbers from 0 to 4 to the screen:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>for index in range(5): print(index)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>0 1 2 3 4<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>In this example, the&nbsp;<code>for<\/code>&nbsp;loop executes the statement&nbsp;<code>print(index)<\/code>&nbsp;exactly five times.<\/p>\n\n\n\n<p>If you want to show 5 numbers from 1 to 5 on the screen, you can do something like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>for index in range(5): print(index + 1)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>1 2 3 4 5<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>In this example, we increase the&nbsp;<code>index<\/code>&nbsp;by one in each iteration and print it out. However, there\u2019s a better way to do it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Specifying the starting value for the sequence<\/h3>\n\n\n\n<p>By default, the&nbsp;<code>range()<\/code>&nbsp;function uses zero as the starting number for the sequence.<\/p>\n\n\n\n<p>In addition, the\u00a0<code>range()<\/code>\u00a0function allows you to specify the starting number like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>range(start, stop)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>In this syntax, the&nbsp;<code>range()<\/code>&nbsp;function increases the&nbsp;<code>start<\/code>&nbsp;value by one until it reaches the&nbsp;<code>stop<\/code>&nbsp;value.<\/p>\n\n\n\n<p>The following example uses a\u00a0<code>for<\/code>\u00a0loop to show five numbers, from 1 to 5 to the screen:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>for index in range(1, 6): print(index)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>1 2 3 4 5<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Specifying the increment for the sequence<\/h3>\n\n\n\n<p>By default, the&nbsp;<code>range(start, stop)<\/code>&nbsp;increases the&nbsp;<code>start<\/code>&nbsp;value by one in each loop iteration.<\/p>\n\n\n\n<p>To increase the\u00a0<code>start<\/code>\u00a0value by a different number, you use the following form of the\u00a0<code>range()<\/code>\u00a0function:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>range(start, stop, step)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>In this form, you can specify the value that the&nbsp;<code>range()<\/code>&nbsp;function should increase.<\/p>\n\n\n\n<p>The following example shows all the odd numbers from 0 to 10:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>for index in range(0, 11, 2): print(index)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>0 2 4 6 8 10<\/code><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Using Python for loop to calculate the sum of a sequence<\/h2>\n\n\n\n<p>The following example uses the for loop statement to calculate the sum of numbers from 1 to 100:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>sum = 0 for num in range(101): sum += num print(sum)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>5050<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, the sum is initialized to zero.<\/li>\n\n\n\n<li>Second, the sum is added with the number from 1 to 100 in each iteration.<\/li>\n\n\n\n<li>Finally, show the sum to the screen.<\/li>\n<\/ul>\n\n\n\n<p>By the way, if you\u2019re a mathematician, you can use the simple formula:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>n = 100 sum = n * (n+1)\/2 print(sum)<\/code><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll learn about the Python&nbsp;for&nbsp;loop and how to use it to execute a code block a fixed number of times. Introduction to Python for loop statement with the range() function In programming, you often want to execute a block of code multiple times. To do so, you use a&nbsp;for&nbsp;loop. The following [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-39","post","type-post","status-publish","format-standard","hentry","category-3-control-flow"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/39","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=39"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/39\/revisions"}],"predecessor-version":[{"id":40,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/39\/revisions\/40"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=39"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=39"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=39"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}