{"id":28,"date":"2025-10-19T02:54:23","date_gmt":"2025-10-19T02:54:23","guid":{"rendered":"https:\/\/codetypingpro.com\/?p=28"},"modified":"2025-12-17T11:50:02","modified_gmt":"2025-12-17T11:50:02","slug":"lesson-6-loops-in-python","status":"publish","type":"post","link":"https:\/\/codetypingpro.com\/?p=28","title":{"rendered":"Lesson 6: Loops in Python"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">\ud83c\udfaf <strong>Lesson Objective<\/strong><\/h3>\n\n\n\n<p>To understand how to repeat tasks using loops in Python efficiently.<br>You will learn about the <code>for<\/code> loop, <code>while<\/code> loop, nested loops, and control statements like <code>break<\/code> and <code>continue<\/code>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83e\udde0 <strong>1. What Are Loops?<\/strong><\/h3>\n\n\n\n<p>Loops allow you to <strong>execute a block of code repeatedly<\/strong> until a certain condition is met.<br>They are used when you want to perform the same task multiple times \u2014 for example, printing numbers, iterating through a list, or processing data.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd01 <strong>2. Types of Loops in Python<\/strong><\/h3>\n\n\n\n<p>Python supports two main types of loops:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>for loop<\/strong> \u2013 used for iterating over a sequence (list, tuple, string, etc.)<\/li>\n\n\n\n<li><strong>while loop<\/strong> \u2013 runs as long as a condition remains True<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u2699\ufe0f <strong>3. <code>for<\/code> Loop Syntax<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>for variable in sequence:\n    # code block\n<\/code><\/pre>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for i in range(5):\n    print(\"Hello Python\")\n<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Hello Python\nHello Python\nHello Python\nHello Python\nHello Python\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udccb <strong>4. Using <code>range()<\/code> Function<\/strong><\/h3>\n\n\n\n<p>The <code>range()<\/code> function generates a sequence of numbers.<br>It can take 1, 2, or 3 arguments: <code>range(start, stop, step)<\/code><\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for i in range(1, 10, 2):\n    print(i)\n<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1\n3\n5\n7\n9\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd04 <strong>5. Looping Through Lists and Strings<\/strong><\/h3>\n\n\n\n<p>You can loop through any iterable (like a list or string).<\/p>\n\n\n\n<p>Example 1: Loop through a list<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fruits = &#91;\"apple\", \"banana\", \"cherry\"]\nfor fruit in fruits:\n    print(fruit)\n<\/code><\/pre>\n\n\n\n<p>Example 2: Loop through a string<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for letter in \"PYTHON\":\n    print(letter)\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u23f3 <strong>6. The <code>while<\/code> Loop<\/strong><\/h3>\n\n\n\n<p>A <code>while<\/code> loop runs until its condition becomes <code>False<\/code>.<\/p>\n\n\n\n<p>Syntax:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>while condition:\n    # code block\n<\/code><\/pre>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>count = 1\nwhile count &lt;= 5:\n    print(\"Count:\", count)\n    count += 1\n<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Count: 1\nCount: 2\nCount: 3\nCount: 4\nCount: 5\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u26a0\ufe0f <strong>7. Infinite Loops<\/strong><\/h3>\n\n\n\n<p>If the condition in a <code>while<\/code> loop never becomes <code>False<\/code>, it runs forever.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>while True:\n    print(\"This will run forever!\")\n<\/code><\/pre>\n\n\n\n<p>\u2757 Use <code>Ctrl + C<\/code> to stop infinite loops manually.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83e\udde9 <strong>8. Loop Control Statements<\/strong><\/h3>\n\n\n\n<p>Python provides special keywords to control loop behavior.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><code>break<\/code> \u2013 Exit the loop early<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>for i in range(10):\n    if i == 5:\n        break\n    print(i)\n<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>0\n1\n2\n3\n4\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><code>continue<\/code> \u2013 Skip the current iteration<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>for i in range(5):\n    if i == 2:\n        continue\n    print(i)\n<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>0\n1\n3\n4\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><code>pass<\/code> \u2013 Placeholder for future code<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>for i in range(3):\n    pass  # no action taken now\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd01 <strong>9. Nested Loops<\/strong><\/h3>\n\n\n\n<p>You can place one loop inside another.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for i in range(1, 4):\n    for j in range(1, 4):\n        print(i, j)\n<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1 1\n1 2\n1 3\n2 1\n2 2\n2 3\n3 1\n3 2\n3 3\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83c\udf0d <strong>10. Real-Life Example: Multiplication Table<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>num = int(input(\"Enter a number: \"))\nfor i in range(1, 11):\n    print(f\"{num} x {i} = {num * i}\")\n<\/code><\/pre>\n\n\n\n<p>Output (for input 5):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>5 x 1 = 5\n5 x 2 = 10\n...\n5 x 10 = 50\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83e\udde0 <strong>11. Practice Ideas<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Print numbers from 1 to 20 using both <code>for<\/code> and <code>while<\/code> loops.<\/li>\n\n\n\n<li>Display all even numbers between 1 and 50.<\/li>\n\n\n\n<li>Create a program that reverses a string using a loop.<\/li>\n\n\n\n<li>Use a loop to count vowels in a given sentence.<\/li>\n\n\n\n<li>Generate a simple pattern like:<\/li>\n<\/ol>\n\n\n\n<p>*\n**\n***\n****\n*****<\/p>\n\n\n\n<div class=\"wp-block-file\"><a id=\"wp-block-file--media-a9d3d583-53ce-459d-a486-a8d7dc7e60dc\" href=\"https:\/\/codetypingpro.com\/wp-content\/uploads\/2025\/10\/Lesson_6_Loops_Practice_Sheet.docx\">Lesson_6_Loops_Practice_Sheet<\/a><a href=\"https:\/\/codetypingpro.com\/wp-content\/uploads\/2025\/10\/Lesson_6_Loops_Practice_Sheet.docx\" class=\"wp-block-file__button wp-element-button\" download aria-describedby=\"wp-block-file--media-a9d3d583-53ce-459d-a486-a8d7dc7e60dc\">Download<\/a><\/div>\n\n\n\n<p><a href=\"https:\/\/codetypingpro.com\/?p=350\" data-type=\"post\" data-id=\"350\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">more examples<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\ud83c\udfaf Lesson Objective To understand how to repeat tasks using loops in Python efficiently.You will learn about the for loop, while loop, nested loops, and control statements like break and continue. \ud83e\udde0 1. What Are Loops? Loops allow you to execute a block of code repeatedly until a certain condition is met.They are used when [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,1],"tags":[],"class_list":["post-28","post","type-post","status-publish","format-standard","hentry","category-python-easy-course-outline","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/28","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=28"}],"version-history":[{"count":2,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/28\/revisions"}],"predecessor-version":[{"id":354,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/28\/revisions\/354"}],"wp:attachment":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=28"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=28"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=28"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}