{"id":19816,"date":"2022-05-18T11:15:48","date_gmt":"2022-05-18T05:45:48","guid":{"rendered":"https:\/\/java2blog.com\/?p=19816"},"modified":"2023-10-17T18:28:23","modified_gmt":"2023-10-17T12:58:23","slug":"count-python-loop","status":"publish","type":"post","link":"https:\/\/java2blog.com\/count-python-loop\/","title":{"rendered":"How to Count in Python Loop"},"content":{"rendered":"<div id=\"toc_container\" class=\"toc_light_blue no_bullets\"><p class=\"toc_title\">Table of Contents<\/p><ul class=\"toc_list\"><li><a href=\"#1_Using_the_enumerate_Function\">1. Using the enumerate() Function<\/a><\/li><li><a href=\"#2_Using_the_enumerate_Function_with_different_start_number\">2. Using the enumerate() Function with different start number<\/a><\/li><li><a href=\"#2_Using_a_Counter_Variable\">2. Using a Counter Variable<\/a><\/li><li><a href=\"#3_Using_the_zip_Function_with_range_function\">3. Using the zip() Function with range() function<\/a><\/li><li><a href=\"#4_Using_range_function\">4. Using range() function<\/a><\/li><li><a href=\"#5_Count_in_while_loop\">5. Count in while loop<\/a><\/li><li><a href=\"#6_Skip_iteration_while_counting_in_the_loop\">6. Skip iteration while counting in the loop<\/a><\/li><\/ul><\/div>\n<p>This tutorial discusses about how to count in Python loop.<\/p>\n<p>In Python, we have many objects that can be treated as iterable. An iterable is an object like a list, tuple, or any other sequence of elements that can be iterated using a simple <code>for<\/code> loop.<\/p>\n<p>Counting in a loop means making a note of every iteration to find the total number of iterations after the loop has stopped running.<\/p>\n<blockquote>\n<p>If you want to count in while loop, skip to <a href=\"#while\" title=\"count in while loop section\">count in while loop section<\/a>.<\/p>\n<\/blockquote>\n<h2><span id=\"1_Using_the_enumerate_Function\">1. Using the <code>enumerate()<\/code> Function<\/span><\/h2>\n<p>The <code>enumerate()<\/code> function takes an iterable and assigns a counter variable to each element. This way, every element gets assigned a value that can act as the index of the element.<\/p>\n<p>We can use this index variable to act as a counter variable for the loop.<\/p>\n<p>See the code below.<\/p>\n<pre code=\"python\" title=\"Using enumerate() function\">\nlst = ['USA', 'Canada', 'France']\nfor i,v in enumerate(lst):\n    print(i,v)\n<\/pre>\n<p>Output:<\/p>\n<div class=\"content-box-green\">\n0 USA<br \/>\n1 Canada<br \/>\n2 France\n<\/div>\n<p>In the above example, we display the index of the element after using the <code>enumerate()<\/code> function and iterating it. Note that the index starts from 0.<\/p>\n<p><code>enumerate()<\/code> function takes an iterables such as list as input and returns enumerate object containing a tuple where the first element is index and second element is iterable element.<\/p>\n<p><code>enumerate()<\/code> function also takes optional argument <code>start<\/code>. We will learn about it in next section.<\/p>\n<h2><span id=\"2_Using_the_enumerate_Function_with_different_start_number\">2. Using the <code>enumerate()<\/code> Function with different start number<\/span><\/h2>\n<p>In case, you don&#8217;t want to start count with 0 and start it with different number such as 1, pass optional argument start argument with different value such as 1.<\/p>\n<p>Let&#8217;s see with the help of example:<\/p>\n<pre code=\"python\" title=\"Using enumerate() function\">\nlst = ['USA', 'Canada', 'France']\nfor i,v in enumerate(lst,start=1):\n    print(i,v)\n<\/pre>\n<p>Output:<\/p>\n<div class=\"content-box-green\">\n1 USA<br \/>\n2 Canada<br \/>\n3 France\n<\/div>\n<h2><span id=\"2_Using_a_Counter_Variable\">2. Using a Counter Variable<\/span><\/h2>\n<p>This method is very straightforward, but manual counting method. We will declare a variable outside the loop and assign it a value of zero. In every iteration, we will increment its value and print it.<\/p>\n<p>See the code below.<\/p>\n<pre code=\"python\" title=\"Using count variable\">\nlst = ['USA', 'Canada', 'France']\nc = 0\nfor i in lst:\n    c = c + 1\n    print(c)\n<\/pre>\n<p>Output:<\/p>\n<div class=\"content-box-green\">\n1<br \/>\n2<br \/>\n3\n<\/div>\n<p>In the above example, we create a list <code>lst<\/code> that contains three elements. The variable <code>c<\/code> is initialized with zero. We use the <code>for<\/code> loop to iterate over this list. In every iteration, we increment the variable <code>c<\/code> and print its value.<\/p>\n<p>You can start counter with different number if you want.<\/p>\n<pre code=\"python\" title=\"Using count variable\">\nlst = ['USA', 'Canada', 'France']\nc = 10\nfor i in lst:\n    c = c + 1\n    print(c)\n<\/pre>\n<p>Output:<\/p>\n<div class=\"content-box-green\">\n11<br \/>\n12<br \/>\n13\n<\/div>\n<p>Here we start counter variable <code>c<\/code> with 10.<\/p>\n<section class=\"read-more-posts\">\n<div class=\"rm-header\">\n<h2>Further reading:<\/h2>\n<\/div>\n<div class=\"rm-wrap\">\n<div class=\"rm-item\">\n<h5><a href=\"https:\/\/java2blog.com\/python-for-loop-decrement\/\" target=\"_blank\" rel=\"noopener\">How to decrement for loop in python<\/a><\/h5>\n<div class=\"ex\">\n            <a href=\"https:\/\/java2blog.com\/python-for-loop-decrement\/\" target=\"_blank\" rel=\"noopener\">Read more \u2192<\/a>\n        <\/div>\n<\/div>\n<div class=\"rm-item\">\n<h5><a href=\"https:\/\/java2blog.com\/for-loop-increment-by-2-in-python\/\" target=\"_blank\" rel=\"noopener\">For Loop Increment By 2 in Python<\/a><\/h5>\n<div class=\"ex\">\n            <a href=\"https:\/\/java2blog.com\/for-loop-increment-by-2-in-python\/\" target=\"_blank\" rel=\"noopener\">Read more \u2192<\/a>\n        <\/div>\n<\/p><\/div>\n<\/div>\n<\/section>\n<h2><span id=\"3_Using_the_zip_Function_with_range_function\">3. Using the <code>zip()<\/code> Function with range() function<\/span><\/h2>\n<p>The <code>zip()<\/code> function can be used to combine two iterables into one by combining the individual elements at corresponding positions together.<\/p>\n<p><a href=\"https:\/\/java2blog.com\/create-list-from-1-to-100-python\/\" title=\"We can create a list\">We can create a list<\/a> of the length iterable with the <code>range()<\/code> function. The two can be then combined to create a new iterable. This method is just another way to emulate the <code>enumerate()<\/code> function discussed previously.<\/p>\n<p>The code is demonstrated below.<\/p>\n<pre code=\"python\" title=\"Using zip() function\">\nlst = ['USA', 'Canada', 'France']\nfor i,v in zip(range(0,len(lst)),lst):\n    print(i,v)\n<\/pre>\n<p>Output:<\/p>\n<div class=\"content-box-green\">\n0 USA<br \/>\n1 Canada<br \/>\n2 France\n<\/div>\n<p>The <code>range()<\/code> function returns an object containing a sequence to the length of the list. This is combined with the list and the counter value is displayed.<\/p>\n<h2><span id=\"4_Using_range_function\">4. Using range() function<\/span><\/h2>\n<p>This is similar to previous method, but here we will directly use <code>range()<\/code> function and will use index to print elements of the list.<\/p>\n<pre code=\"python\" title=\"Using zip() function\">\nlst = ['USA', 'Canada', 'France']\nfor i in range(0,len(lst)):\n    print(i,lst[i]) \n<\/pre>\n<p>Output:<\/p>\n<div class=\"content-box-green\">\n0 USA<br \/>\n1 Canada<br \/>\n2 France\n<\/div>\n<h2 id=\"while\"><span id=\"5_Count_in_while_loop\">5. Count in while loop<\/span><\/h2>\n<p>To count in while loop<\/p>\n<ul>\n<li>Define a variable named <code>c<\/code> with 0. You can initialize it 1 as well based on your requirements.<\/li>\n<li>Iterate over while loop upto N.<\/li>\n<li>In each iteration, increase the value by 1.\n<pre code=\"python\">\nlst = ['USA', 'Canada', 'France']\nc=0\nwhile c &lt; len(lst):\nc+=1\nprint(c)\n<\/pre>\n<div class=\"content-box-green\">\n1<br \/>\n2<br \/>\n3\n<\/div>\n<\/li>\n<\/ul>\n<h2><span id=\"6_Skip_iteration_while_counting_in_the_loop\">6. Skip iteration while counting in the loop<\/span><\/h2>\n<p><a href=\"https:\/\/java2blog.com\/skip-iterations-python-loop\/\" title=\"To skip iteration while counting in the loop\">To skip iteration while counting in the loop<\/a>, you can use if clause with continue while iterating.<br \/>\nHere is simple example:<\/p>\n<pre code=\"python\">\nlst = ['USA', 'Canada', 'France']\nc = 0\nfor i in lst:\n    c = c + 1\n    if c==2:\n        continue\n    print(c)\n<\/pre>\n<div class=\"content-box-green\">\n1<br \/>\n3\n<\/div>\n<p>As you can see, when value of <code>c<\/code> was <code>1<\/code>, we skipped the iteration by continuing the loop and that&#8217;s the reason we got <code>1 3<\/code> as output.<\/p>\n<p>That&#8217;s all about how to count in Python loop.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Table of Contents1. Using the enumerate() Function2. Using the enumerate() Function with different start number2. Using a Counter Variable3. Using the zip() Function with range() function4. Using range() function5. Count in while loop6. Skip iteration while counting in the loop This tutorial discusses about how to count in Python loop. In Python, we have many [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_mi_skip_tracking":false},"categories":[145],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/19816"}],"collection":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=19816"}],"version-history":[{"count":6,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/19816\/revisions"}],"predecessor-version":[{"id":25298,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/19816\/revisions\/25298"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=19816"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=19816"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=19816"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}