{"id":59,"date":"2026-03-01T15:38:35","date_gmt":"2026-03-01T10:38:35","guid":{"rendered":"https:\/\/gigz.pk\/python\/?post_type=lesson&#038;p=59"},"modified":"2026-03-13T09:43:44","modified_gmt":"2026-03-13T04:43:44","slug":"for-loop","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/python\/lesson\/for-loop\/","title":{"rendered":"For Loop"},"content":{"rendered":"\n<p>A <strong>for loop<\/strong> is used to repeat a block of code multiple times.<br>It is commonly used to iterate over sequences like lists, tuples, strings, dictionaries, and ranges.<\/p>\n\n\n\n<p>For loops make programs shorter, cleaner, and more efficient.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>SYNTAX OF FOR LOOP<\/strong><\/h1>\n\n\n\n<pre class=\"wp-block-preformatted\">for variable in sequence:<br>    # code block<\/pre>\n\n\n\n<p>The loop runs once for each item in the sequence.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>EXAMPLE 1: LOOP WITH A LIST<\/strong><\/h1>\n\n\n\n<pre class=\"wp-block-preformatted\">numbers = [1, 2, 3, 4, 5]for num in numbers:<br>    print(num)<\/pre>\n\n\n\n<p>The loop prints each number from the list one by one.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>EXAMPLE 2: LOOP WITH A STRING<\/strong><\/h1>\n\n\n\n<pre class=\"wp-block-preformatted\">text = \"Python\"for letter in text:<br>    print(letter)<\/pre>\n\n\n\n<p>The loop prints each character separately.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>USING range() FUNCTION<\/strong><\/h1>\n\n\n\n<p>The <code>range()<\/code> function generates a sequence of numbers.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 1: Basic range<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">for i in range(5):<br>    print(i)<\/pre>\n\n\n\n<p>Output:<br>0<br>1<br>2<br>3<br>4<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 2: Start and Stop<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">for i in range(1, 6):<br>    print(i)<\/pre>\n\n\n\n<p>Output:<br>1 to 5<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 3: Step Value<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">for i in range(1, 10, 2):<br>    print(i)<\/pre>\n\n\n\n<p>This prints numbers with a step of 2.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>FOR LOOP WITH IF CONDITION<\/strong><\/h1>\n\n\n\n<pre class=\"wp-block-preformatted\">for num in range(1, 11):<br>    if num % 2 == 0:<br>        print(num)<\/pre>\n\n\n\n<p>This prints only even numbers between 1 and 10.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>NESTED FOR LOOP<\/strong><\/h1>\n\n\n\n<p>You can use a loop inside another loop.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">for i in range(3):<br>    for j in range(2):<br>        print(\"i =\", i, \"j =\", j)<\/pre>\n\n\n\n<p>Nested loops are useful for patterns, tables, and matrix operations.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>BREAK AND CONTINUE<\/strong><\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">Break Statement<\/h3>\n\n\n\n<p>Stops the loop completely.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">for i in range(5):<br>    if i == 3:<br>        break<br>    print(i)<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Continue Statement<\/h3>\n\n\n\n<p>Skips the current iteration.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">for i in range(5):<br>    if i == 3:<br>        continue<br>    print(i)<\/pre>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>WHY FOR LOOPS ARE IMPORTANT<\/strong><\/h1>\n\n\n\n<p>\u2022 Automate repetitive tasks<br>\u2022 Work with collections of data<br>\u2022 Build logical programs efficiently<br>\u2022 Create patterns and tables<\/p>\n\n\n\n<p>Understanding the <strong>for loop<\/strong> is essential for writing powerful and dynamic Python programs.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/python\/\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">PYTHON FUNDAMENTALS (PYF) > Control Flow > For Loop<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1773376894247\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n","protected":false},"menu_order":18,"template":"","class_list":["post-59","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>For Loop - One Language. Endless Possibilities<\/title>\n<meta name=\"description\" content=\"Learn Python for loops to iterate lists, strings, and ranges. Master break, continue, and nested loops for efficient programs.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/python\/lesson\/for-loop\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"For Loop - One Language. Endless Possibilities\" \/>\n<meta property=\"og:description\" content=\"Learn Python for loops to iterate lists, strings, and ranges. Master break, continue, and nested loops for efficient programs.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/python\/lesson\/for-loop\/\" \/>\n<meta property=\"og:site_name\" content=\"One Language. Endless Possibilities\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-13T04:43:44+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/for-loop\\\/\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/for-loop\\\/\",\"name\":\"For Loop - One Language. Endless Possibilities\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\"},\"datePublished\":\"2026-03-01T10:38:35+00:00\",\"dateModified\":\"2026-03-13T04:43:44+00:00\",\"description\":\"Learn Python for loops to iterate lists, strings, and ranges. Master break, continue, and nested loops for efficient programs.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/for-loop\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/for-loop\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/for-loop\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PYTHON FUNDAMENTALS (PYF) > Control Flow > For Loop\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\",\"name\":\"One Language. Endless Possibilities\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/python\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"For Loop - One Language. Endless Possibilities","description":"Learn Python for loops to iterate lists, strings, and ranges. Master break, continue, and nested loops for efficient programs.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/gigz.pk\/python\/lesson\/for-loop\/","og_locale":"en_US","og_type":"article","og_title":"For Loop - One Language. Endless Possibilities","og_description":"Learn Python for loops to iterate lists, strings, and ranges. Master break, continue, and nested loops for efficient programs.","og_url":"https:\/\/gigz.pk\/python\/lesson\/for-loop\/","og_site_name":"One Language. Endless Possibilities","article_modified_time":"2026-03-13T04:43:44+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/python\/lesson\/for-loop\/","url":"https:\/\/gigz.pk\/python\/lesson\/for-loop\/","name":"For Loop - One Language. Endless Possibilities","isPartOf":{"@id":"https:\/\/gigz.pk\/python\/#website"},"datePublished":"2026-03-01T10:38:35+00:00","dateModified":"2026-03-13T04:43:44+00:00","description":"Learn Python for loops to iterate lists, strings, and ranges. Master break, continue, and nested loops for efficient programs.","breadcrumb":{"@id":"https:\/\/gigz.pk\/python\/lesson\/for-loop\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/python\/lesson\/for-loop\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/python\/lesson\/for-loop\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/python\/"},{"@type":"ListItem","position":2,"name":"PYTHON FUNDAMENTALS (PYF) > Control Flow > For Loop"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/python\/#website","url":"https:\/\/gigz.pk\/python\/","name":"One Language. Endless Possibilities","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/python\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson\/59","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/media?parent=59"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}