{"id":448,"date":"2025-05-20T04:53:25","date_gmt":"2025-05-20T04:53:25","guid":{"rendered":"https:\/\/buhave.com\/courses\/?p=448"},"modified":"2026-06-20T18:29:26","modified_gmt":"2026-06-20T18:29:26","slug":"loops-and-iteration-2","status":"publish","type":"post","link":"https:\/\/buhave.com\/courses\/python\/loops-and-iteration-2\/","title":{"rendered":"Loops and Iteration"},"content":{"rendered":"<h2>For Loops and While Loops<\/h2>\n<p>In this lesson, you&#8217;ll explore core loop constructs in Python, including for and while loops, and common control-flow patterns like break, continue, and pass. By the end, you&#8217;ll be able to iterate over sequences, build simple menus, and validate user input. If you&#8217;re new to Python, consider starting with <a href=\"https:\/\/buhave.com\/courses\/python\/introduction-to-python\/\" target=\"_blank\" rel=\"noopener\">Introduction to Python<\/a> to learn the basics, then return here to practice looping concepts. For broader control-flow discussions, see <a href=\"https:\/\/buhave.com\/courses\/python\/control-flow-2\/\" target=\"_blank\" rel=\"noopener\">Control Flow<\/a>. For data-centric topics, explore <a href=\"https:\/\/buhave.com\/courses\/python\/data-structures\/\" target=\"_blank\" rel=\"noopener\">Data Structures<\/a> and <a href=\"https:\/\/buhave.com\/courses\/python\/working-with-strings\/\" target=\"_blank\" rel=\"noopener\">Working with Strings<\/a> as you implement examples.<\/p>\n<p><strong>Note:<\/strong> If you\u2019re brushing up on variables and types, see <a href=\"https:\/\/buhave.com\/courses\/python\/variables-data-types-and-operators\/\" target=\"_blank\" rel=\"noopener\">Variables<\/a> for quick references on naming conventions and data types.<\/p>\n<h3>1. For Loops<\/h3>\n<p><strong>Purpose:<\/strong><\/p>\n<p>Used to iterate over a sequence (like a list, string, tuple, range, etc.).<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<p style=\"text-align: center\"><em>for variable in iterable:<\/em><br \/>\n<em># code block<\/em><\/p>\n<p><strong>Example 1:<\/strong> Looping through a list<\/p>\n<p style=\"text-align: center\"><em>fruits = [&#8220;apple&#8221;, &#8220;banana&#8221;, &#8220;cherry&#8221;]<\/em><\/p>\n<p style=\"text-align: center\"><em>for fruit in fruits:<\/em><br \/>\n<em>print(fruit)<\/em><\/p>\n<p><strong>Output:<\/strong><\/p>\n<p style=\"text-align: center\"><em>apple<\/em><br \/>\n<em>banana<\/em><br \/>\n<em>cherry<\/em><\/p>\n<p><strong>Example 2:<\/strong> Using range()<\/p>\n<p style=\"text-align: center\"><em>for i in range(5):<\/em><br \/>\n<em>print(i)<\/em><\/p>\n<p><strong>Output:<\/strong><\/p>\n<p style=\"text-align: center\"><em>0<\/em><br \/>\n<em>1<\/em><br \/>\n<em>2<\/em><br \/>\n<em>3<\/em><br \/>\n<em>4<\/em><\/p>\n<p>range(n) gives numbers from 0 to n-1<\/p>\n<p><strong>You can also use:<\/strong><\/p>\n<p style=\"text-align: center\"><em>range(start, stop, step)<\/em><\/p>\n<p><strong> Example 3:<\/strong> Loop with index (using enumerate())<\/p>\n<p style=\"text-align: center\"><em>colors = [&#8220;red&#8221;, &#8220;green&#8221;, &#8220;blue&#8221;]<\/em><\/p>\n<p style=\"text-align: center\"><em>for index, color in enumerate(colors):<\/em><br \/>\n<em>print(f&#8221;{index}: {color}&#8221;)<\/em><\/p>\n<p><strong>When to use for:<\/strong><\/p>\n<ul>\n<li>You know the number of iterations or are iterating over a concrete collection (list, string, tuple, range)<\/li>\n<li>You want concise, readable iteration over an iterable<\/li>\n<li>You need to access elements with their indices (e.g., using enumerate())<\/li>\n<\/ul>\n<p><strong>Tip:<\/strong> If you&#8217;re new to Python, start with <a href=\"https:\/\/buhave.com\/courses\/python\/introduction-to-python\/\" target=\"_blank\" rel=\"noopener\">Introduction to Python<\/a> to understand the basics of variables and control flow, and then come back to practice loops. For broader control-flow concepts, see <a href=\"https:\/\/buhave.com\/courses\/python\/control-flow-2\/\" target=\"_blank\" rel=\"noopener\">Control Flow<\/a>.<\/p>\n<h3>2. While Loops<\/h3>\n<p><strong>Purpose:<\/strong><\/p>\n<p>Repeats a block of code as long as a condition is True.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<p style=\"text-align: center\"><em>while condition:<\/em><br \/>\n<em># code block<\/em><\/p>\n<p><strong>Example 1:<\/strong> Basic while loop<\/p>\n<p style=\"text-align: center\"><em>count = 0<\/em><\/p>\n<p style=\"text-align: center\"><em>while count &lt; 5:<\/em><br \/>\n<em>print(&#8220;Count is:&#8221;, count)<\/em><br \/>\n<em>count += 1<\/em><\/p>\n<p><strong>Example 2:<\/strong> User input loop<\/p>\n<p style=\"text-align: center\"><em>password = &#8220;&#8221;<\/em><\/p>\n<p style=\"text-align: center\"><em>while password != &#8220;secret&#8221;:<\/em><br \/>\n<em>password = input(&#8220;Enter the password: &#8220;)<\/em><\/p>\n<p style=\"text-align: center\"><em>print(&#8220;Access granted.&#8221;)<\/em><\/p>\n<p><strong>When to use while:<\/strong><\/p>\n<ul>\n<li>You don\u2019t know in advance how many times to loop<\/li>\n<li>You&#8217;re waiting for a condition to be met<\/li>\n<li>Great for input validation, menus, waiting for user action, etc.<\/li>\n<\/ul>\n<p><strong>Control Flow Keywords (work in both loops)<\/strong><\/p>\n<table style=\"width: 100%;border-collapse: collapse;font-family: Arial, sans-serif;margin: 20px 0\">\n<thead>\n<tr>\n<th style=\"padding: 12px;text-align: left;border-bottom: 2px solid #000;font-weight: bold\"><strong>Keyword<\/strong><\/th>\n<th style=\"padding: 12px;text-align: left;border-bottom: 2px solid #000;font-weight: bold\"><strong>Use<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\"><code>break<\/code><\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">Exits the loop immediately<\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\"><code>continue<\/code><\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">Skips remaining code in current iteration and moves to next iteration<\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\"><code>else<\/code><\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">Optional block that executes only if loop completes normally (without <code>break<\/code>)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>break Example:<\/strong><\/p>\n<p style=\"text-align: center\"><em>for num in range(10):<\/em><br \/>\n<em>if num == 5:<\/em><br \/>\n<em>break<\/em><br \/>\n<em>print(num)<\/em><\/p>\n<p><strong>continue Example:<\/strong><\/p>\n<p style=\"text-align: center\"><em>for num in range(5):<\/em><br \/>\n<em>if num == 2:<\/em><br \/>\n<em>continue<\/em><br \/>\n<em>print(num)<\/em><\/p>\n<p><strong>Summary:<\/strong> for vs while<\/p>\n<table style=\"width: 100%;border-collapse: collapse;font-family: Arial, sans-serif;margin: 20px 0\">\n<thead>\n<tr>\n<th style=\"padding: 12px;text-align: left;border-bottom: 2px solid #000;font-weight: bold\"><strong>Feature<\/strong><\/th>\n<th style=\"padding: 12px;text-align: left;border-bottom: 2px solid #000;font-weight: bold\"><strong>for Loop<\/strong><\/th>\n<th style=\"padding: 12px;text-align: left;border-bottom: 2px solid #000;font-weight: bold\"><strong>while Loop<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">Iterates over<\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">Sequence (<code>range<\/code>, <code>list<\/code>, <code>string<\/code>)<\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">Until a condition becomes <code>False<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">Use when<\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">You know how many iterations needed<\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">You don&#8217;t know how many iterations needed<\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">Example Use<\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">Printing list items, counting<\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">Waiting for correct user input<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Want More?<\/strong><\/p>\n<p>I can add:<\/p>\n<ul>\n<li>Loop exercises (countdown timer, sum calculator, etc.)<\/li>\n<li>A quiz or worksheet<\/li>\n<li>A visual loop flowchart<\/li>\n<li>Let me know what format you\u2019d like next!<\/li>\n<\/ul>\n<h2>Break, Continue, and Pass<\/h2>\n<h3>1. break \u2013 Exit the loop early<\/h3>\n<p><strong>What it does:<\/strong><\/p>\n<p>Immediately terminates the nearest enclosing loop \u2014 either for or while.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<p style=\"text-align: center\"><em>for item in iterable:<\/em><br \/>\n<em>if condition:<\/em><br \/>\n<em>break<\/em><\/p>\n<p><strong>Example:<\/strong><\/p>\n<p style=\"text-align: center\"><em>for num in range(10):<\/em><br \/>\n<em>if num == 5:<\/em><br \/>\n<em>break<\/em><br \/>\n<em>print(num)<\/em><\/p>\n<p><strong>Output:<\/strong><\/p>\n<p style=\"text-align: center\"><em>0<\/em><br \/>\n<em>1<\/em><br \/>\n<em>2<\/em><br \/>\n<em>3<\/em><br \/>\n<em>4<\/em><\/p>\n<p>The loop stops before 5, because break exits the loop.<\/p>\n<p><strong>Real Use Case:<\/strong><\/p>\n<ul>\n<li>Exiting on a condition (e.g. stop searching when found)<\/li>\n<li>Validating passwords or attempts<\/li>\n<\/ul>\n<h3>2. Continue \u2013 Skip this iteration<\/h3>\n<p><strong>What it does:<\/strong><\/p>\n<p>Skips the rest of the code in the current loop iteration, and moves to the next one.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<p style=\"text-align: center\"><em>for item in iterable:<\/em><br \/>\n<em>if condition:<\/em><br \/>\n<em>continue<\/em><br \/>\n<em># code here will be skipped if condition is True<\/em><\/p>\n<p><strong>Example:<\/strong><\/p>\n<p style=\"text-align: center\"><em>for num in range(5):<\/em><br \/>\n<em>if num == 2:<\/em><br \/>\n<em>continue<\/em><br \/>\n<em>print(num)<\/em><\/p>\n<p><strong>Output:<\/strong><\/p>\n<p style=\"text-align: center\"><em>0<\/em><br \/>\n<em>1<\/em><br \/>\n<em>3<\/em><br \/>\n<em>4<\/em><\/p>\n<p>When num == 2, it skips the print() and goes to the next loop.<\/p>\n<p><strong>Real Use Case:<\/strong><\/p>\n<ul>\n<li>Skipping invalid data<\/li>\n<li>Ignoring certain values (e.g., skip vowels or zeroes)<\/li>\n<\/ul>\n<h3>3. pass \u2013 Do nothing (placeholder)<\/h3>\n<p><strong>What it does:<\/strong><\/p>\n<p>Literally does nothing \u2014 used when a statement is syntactically required but you don\u2019t want any code to run.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<p style=\"text-align: center\"><em>if condition:<\/em><br \/>\n<em>pass # Do nothing (for now)<\/em><\/p>\n<p><strong>Example:<\/strong><\/p>\n<p style=\"text-align: center\"><em>for num in range(3):<\/em><br \/>\n<em>if num == 1:<\/em><br \/>\n<em>pass<\/em><br \/>\n<em>print(f&#8221;Number is {num}&#8221;)<\/em><\/p>\n<p><strong>Output:<\/strong><\/p>\n<p style=\"text-align: center\"><em>Number is 0<\/em><br \/>\n<em>Number is 1<\/em><br \/>\n<em>Number is 2<\/em><\/p>\n<p>The pass does nothing \u2014 but it lets the if statement exist without error.<\/p>\n<p><strong>Real Use Case:<\/strong><\/p>\n<ul>\n<li>Placeholder for code you&#8217;ll write later<\/li>\n<li>Empty function or class definitions<\/li>\n<li>Keeping structure while under development<\/li>\n<\/ul>\n<p><strong>Summary Table:<\/strong><\/p>\n<table style=\"width: 100%;border-collapse: collapse;font-family: Arial, sans-serif;margin: 20px 0\">\n<thead>\n<tr>\n<th style=\"padding: 12px;text-align: left;border-bottom: 2px solid #000;font-weight: bold\">Keyword<\/th>\n<th style=\"padding: 12px;text-align: left;border-bottom: 2px solid #000;font-weight: bold\">Purpose<\/th>\n<th style=\"padding: 12px;text-align: center;border-bottom: 2px solid #000;font-weight: bold\">Ends Loop?<\/th>\n<th style=\"padding: 12px;text-align: center;border-bottom: 2px solid #000;font-weight: bold\">Skips Iteration?<\/th>\n<th style=\"padding: 12px;text-align: left;border-bottom: 2px solid #000;font-weight: bold\">Use Case<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\"><code>break<\/code><\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">Exits loop immediately<\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd;text-align: center;color: green\"><b><\/b><\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd;text-align: center;color: red\">X<\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">Stop a loop early (e.g., on match)<\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\"><code>continue<\/code><\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">Skips rest of current iteration<\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd;text-align: center;color: red\">X<\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd;text-align: center;color: green\"><b><\/b><\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">Skip unwanted data<\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\"><code>pass<\/code><\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">Does nothing; placeholder<\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd;text-align: center;color: red\">X<\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd;text-align: center;color: red\">X<\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">Keep syntax valid without logic<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Bonus Practice Ideas:<\/strong><\/p>\n<ul>\n<li>Loop through numbers and break when divisible by 7<\/li>\n<li>Skip even numbers using continue<\/li>\n<li>Create an empty function using pass<\/li>\n<\/ul>\n<h2>Looping through strings, lists, and ranges<\/h2>\n<p>You can use for loops to iterate over sequences in Python \u2014 like strings, lists, and ranges \u2014 because they\u2019re all iterables.<\/p>\n<h3>1. Looping Through a String<\/h3>\n<p><strong>Purpose:<\/strong><\/p>\n<p>Each character in a string can be accessed one by one.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<p style=\"text-align: center\"><em>for char in string:<\/em><br \/>\n<em># do something with char<\/em><\/p>\n<p><strong>Example:<\/strong><\/p>\n<p style=\"text-align: center\"><em>word = &#8220;Python&#8221;<\/em><\/p>\n<p style=\"text-align: center\"><em>for letter in word:<\/em><br \/>\n<em>print(letter)<\/em><\/p>\n<p><strong>Output:<\/strong><\/p>\n<p style=\"text-align: center\"><em>P<\/em><br \/>\n<em>y<\/em><br \/>\n<em>t<\/em><br \/>\n<em>h<\/em><br \/>\n<em>o<\/em><br \/>\n<em>n<\/em><\/p>\n<p><strong>Use Cases:<\/strong><\/p>\n<ul>\n<li>Counting vowels<\/li>\n<li>Reversing or analyzing text<\/li>\n<li>String transformations<\/li>\n<\/ul>\n<p>For more string-related operations, see <a href=\"https:\/\/buhave.com\/courses\/python\/working-with-strings\/\" target=\"_blank\" rel=\"noopener\">Working with Strings<\/a>.<\/p>\n<h3>2. Looping Through a List<\/h3>\n<p><strong>Purpose:<\/strong><\/p>\n<p>Iterate through each item (element) in a list.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<p style=\"text-align: center\"><em>for item in list_name:<\/em><br \/>\n<em># do something with item<\/em><\/p>\n<p><strong>Example:<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>For Loops and While Loops In this lesson, you&#8217;ll explore core loop constructs in Python, including for and while loops, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":449,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[9],"tags":[],"class_list":["post-448","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Loops and Iteration - Python Course<\/title>\n<meta name=\"description\" content=\"Loops and iteration allow repeated execution of code, either over sequences, ranges, or while conditions are met.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/buhave.com\/courses\/python\/loops-and-iteration-2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Loops and Iteration - Python Course\" \/>\n<meta property=\"og:description\" content=\"Loops and iteration allow repeated execution of code, either over sequences, ranges, or while conditions are met.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/buhave.com\/courses\/python\/loops-and-iteration-2\/\" \/>\n<meta property=\"og:site_name\" content=\"BUHAVE\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/BeYouHave\/\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/naveedsafdarawan\/\" \/>\n<meta property=\"article:published_time\" content=\"2025-05-20T04:53:25+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-06-20T18:29:26+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/Loops-and-Iteration.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"Naveed Safdar\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Naveed Safdar\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/loops-and-iteration-2\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/loops-and-iteration-2\\\/\"},\"author\":{\"name\":\"Naveed Safdar\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#\\\/schema\\\/person\\\/04fe0254e118521c9fbb3da39de5acca\"},\"headline\":\"Loops and Iteration\",\"datePublished\":\"2025-05-20T04:53:25+00:00\",\"dateModified\":\"2026-06-20T18:29:26+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/loops-and-iteration-2\\\/\"},\"wordCount\":896,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/loops-and-iteration-2\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/04\\\/Loops-and-Iteration.webp\",\"articleSection\":[\"Python Course\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/loops-and-iteration-2\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/loops-and-iteration-2\\\/\",\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/loops-and-iteration-2\\\/\",\"name\":\"Loops and Iteration - Python Course\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/loops-and-iteration-2\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/loops-and-iteration-2\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/04\\\/Loops-and-Iteration.webp\",\"datePublished\":\"2025-05-20T04:53:25+00:00\",\"dateModified\":\"2026-06-20T18:29:26+00:00\",\"description\":\"Loops and iteration allow repeated execution of code, either over sequences, ranges, or while conditions are met.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/loops-and-iteration-2\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/loops-and-iteration-2\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/loops-and-iteration-2\\\/#primaryimage\",\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/04\\\/Loops-and-Iteration.webp\",\"contentUrl\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/04\\\/Loops-and-Iteration.webp\",\"width\":1200,\"height\":628,\"caption\":\"Loops and Iteration\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/loops-and-iteration-2\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Courses\",\"item\":\"https:\\\/\\\/buhave.com\\\/courses\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Course\",\"item\":\"https:\\\/\\\/buhave.com\\\/courses\\\/learn\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Loops and Iteration\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#website\",\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/\",\"name\":\"BUHAVE\",\"description\":\"Courses - Learn Online for Free\",\"publisher\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/buhave.com\\\/courses\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#organization\",\"name\":\"BUHAVE\",\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/03\\\/buhave-course.webp\",\"contentUrl\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/03\\\/buhave-course.webp\",\"width\":375,\"height\":75,\"caption\":\"BUHAVE\"},\"image\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/BeYouHave\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/company\\\/buhave\",\"https:\\\/\\\/www.youtube.com\\\/@buhave\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#\\\/schema\\\/person\\\/04fe0254e118521c9fbb3da39de5acca\",\"name\":\"Naveed Safdar\",\"description\":\"I\u2019m Naveed Safdar - SEO Manager with over 10 years of experience in SEO and Digital Marketing. I\u2019ve had the privilege of working with leading national and international companies including Grafdom, PakWheels, Systems Limited, Confiz, Educative, and Dubizzle Labs. My expertise spans technical SEO, content strategy, organic growth, and performance analytics - helping businesses improve visibility, traffic, and ROI.\",\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/naveedsafdar\\\/\",\"https:\\\/\\\/www.facebook.com\\\/naveedsafdarawan\\\/\",\"https:\\\/\\\/www.youtube.com\\\/@naveedsafdar\"],\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/author\\\/naveed-safdar\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Loops and Iteration - Python Course","description":"Loops and iteration allow repeated execution of code, either over sequences, ranges, or while conditions are met.","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:\/\/buhave.com\/courses\/python\/loops-and-iteration-2\/","og_locale":"en_US","og_type":"article","og_title":"Loops and Iteration - Python Course","og_description":"Loops and iteration allow repeated execution of code, either over sequences, ranges, or while conditions are met.","og_url":"https:\/\/buhave.com\/courses\/python\/loops-and-iteration-2\/","og_site_name":"BUHAVE","article_publisher":"https:\/\/www.facebook.com\/BeYouHave\/","article_author":"https:\/\/www.facebook.com\/naveedsafdarawan\/","article_published_time":"2025-05-20T04:53:25+00:00","article_modified_time":"2026-06-20T18:29:26+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/Loops-and-Iteration.webp","type":"image\/webp"}],"author":"Naveed Safdar","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Naveed Safdar","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/buhave.com\/courses\/python\/loops-and-iteration-2\/#article","isPartOf":{"@id":"https:\/\/buhave.com\/courses\/python\/loops-and-iteration-2\/"},"author":{"name":"Naveed Safdar","@id":"https:\/\/buhave.com\/courses\/#\/schema\/person\/04fe0254e118521c9fbb3da39de5acca"},"headline":"Loops and Iteration","datePublished":"2025-05-20T04:53:25+00:00","dateModified":"2026-06-20T18:29:26+00:00","mainEntityOfPage":{"@id":"https:\/\/buhave.com\/courses\/python\/loops-and-iteration-2\/"},"wordCount":896,"commentCount":0,"publisher":{"@id":"https:\/\/buhave.com\/courses\/#organization"},"image":{"@id":"https:\/\/buhave.com\/courses\/python\/loops-and-iteration-2\/#primaryimage"},"thumbnailUrl":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/Loops-and-Iteration.webp","articleSection":["Python Course"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/buhave.com\/courses\/python\/loops-and-iteration-2\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/buhave.com\/courses\/python\/loops-and-iteration-2\/","url":"https:\/\/buhave.com\/courses\/python\/loops-and-iteration-2\/","name":"Loops and Iteration - Python Course","isPartOf":{"@id":"https:\/\/buhave.com\/courses\/#website"},"primaryImageOfPage":{"@id":"https:\/\/buhave.com\/courses\/python\/loops-and-iteration-2\/#primaryimage"},"image":{"@id":"https:\/\/buhave.com\/courses\/python\/loops-and-iteration-2\/#primaryimage"},"thumbnailUrl":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/Loops-and-Iteration.webp","datePublished":"2025-05-20T04:53:25+00:00","dateModified":"2026-06-20T18:29:26+00:00","description":"Loops and iteration allow repeated execution of code, either over sequences, ranges, or while conditions are met.","breadcrumb":{"@id":"https:\/\/buhave.com\/courses\/python\/loops-and-iteration-2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/buhave.com\/courses\/python\/loops-and-iteration-2\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/buhave.com\/courses\/python\/loops-and-iteration-2\/#primaryimage","url":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/Loops-and-Iteration.webp","contentUrl":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/Loops-and-Iteration.webp","width":1200,"height":628,"caption":"Loops and Iteration"},{"@type":"BreadcrumbList","@id":"https:\/\/buhave.com\/courses\/python\/loops-and-iteration-2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Courses","item":"https:\/\/buhave.com\/courses\/"},{"@type":"ListItem","position":2,"name":"Python Course","item":"https:\/\/buhave.com\/courses\/learn\/python\/"},{"@type":"ListItem","position":3,"name":"Loops and Iteration"}]},{"@type":"WebSite","@id":"https:\/\/buhave.com\/courses\/#website","url":"https:\/\/buhave.com\/courses\/","name":"BUHAVE","description":"Courses - Learn Online for Free","publisher":{"@id":"https:\/\/buhave.com\/courses\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/buhave.com\/courses\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/buhave.com\/courses\/#organization","name":"BUHAVE","url":"https:\/\/buhave.com\/courses\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/buhave.com\/courses\/#\/schema\/logo\/image\/","url":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/03\/buhave-course.webp","contentUrl":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/03\/buhave-course.webp","width":375,"height":75,"caption":"BUHAVE"},"image":{"@id":"https:\/\/buhave.com\/courses\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/BeYouHave\/","https:\/\/www.linkedin.com\/company\/buhave","https:\/\/www.youtube.com\/@buhave"]},{"@type":"Person","@id":"https:\/\/buhave.com\/courses\/#\/schema\/person\/04fe0254e118521c9fbb3da39de5acca","name":"Naveed Safdar","description":"I\u2019m Naveed Safdar - SEO Manager with over 10 years of experience in SEO and Digital Marketing. I\u2019ve had the privilege of working with leading national and international companies including Grafdom, PakWheels, Systems Limited, Confiz, Educative, and Dubizzle Labs. My expertise spans technical SEO, content strategy, organic growth, and performance analytics - helping businesses improve visibility, traffic, and ROI.","sameAs":["https:\/\/www.linkedin.com\/in\/naveedsafdar\/","https:\/\/www.facebook.com\/naveedsafdarawan\/","https:\/\/www.youtube.com\/@naveedsafdar"],"url":"https:\/\/buhave.com\/courses\/author\/naveed-safdar\/"}]}},"_links":{"self":[{"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/posts\/448","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/comments?post=448"}],"version-history":[{"count":5,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/posts\/448\/revisions"}],"predecessor-version":[{"id":1281,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/posts\/448\/revisions\/1281"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/media\/449"}],"wp:attachment":[{"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/media?parent=448"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/categories?post=448"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/tags?post=448"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}