{"id":350,"date":"2025-12-17T11:39:11","date_gmt":"2025-12-17T11:39:11","guid":{"rendered":"https:\/\/codetypingpro.com\/?p=350"},"modified":"2025-12-17T12:44:46","modified_gmt":"2025-12-17T12:44:46","slug":"lesson-6-loops-in-python-examples","status":"publish","type":"post","link":"https:\/\/codetypingpro.com\/?p=350","title":{"rendered":"\ud83d\udcd8 Lesson 6 examples: Loops in Python"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">\ud83d\udd39 1. What is a Loop?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A <strong>loop<\/strong> is used to <strong>repeat a block of code multiple times<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udc49 Without loops \u2192 code becomes long<br>\ud83d\udc49 With loops \u2192 code becomes short &amp; clean<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Python has mainly <strong>two loops<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>for<\/code> loop<\/li>\n\n\n\n<li><code>while<\/code> loop<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd39 2. <code>for<\/code> Loop<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A <code>for<\/code> loop is used when <strong>number of repetitions is known<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">Example 1: Printing Numbers<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>for i in range(5):\n    print(i)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>0\n1\n2\n3\n4\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udccc Explanation<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>range(5)<\/code> generates numbers from <code>0<\/code> to <code>4<\/code><\/li>\n\n\n\n<li>Loop runs <strong>5 times<\/strong><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">Example 2: Printing Numbers from 1 to 5<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>for i in range(1, 6):\n    print(i)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1\n2\n3\n4\n5\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">Example 3: Printing Names<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>names = &#91;\"Sameer\", \"Ayesha\", \"Saddam\", \"Chandini\"]\n\nfor name in names:\n    print(name)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Sameer\nAyesha\nSaddam\nChandini\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udccc Explanation<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Loop runs once for each item in the list<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd39 3. <code>range()<\/code> Function<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Syntax<\/th><th>Meaning<\/th><\/tr><\/thead><tbody><tr><td><code>range(stop)<\/code><\/td><td>0 to stop-1<\/td><\/tr><tr><td><code>range(start, stop)<\/code><\/td><td>start to stop-1<\/td><\/tr><tr><td><code>range(start, stop, step)<\/code><\/td><td>step increment<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">Example 4: Step Value<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>for i in range(1, 11, 2):\n    print(i)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output<\/strong><\/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\udd39 4. <code>while<\/code> Loop<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A <code>while<\/code> loop runs <strong>as long as a condition is true<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">Example 5: Print Numbers Using <code>while<\/code><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>i = 1\n\nwhile i &lt;= 5:\n    print(i)\n    i += 1\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1\n2\n3\n4\n5\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udccc Explanation<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Condition checked before every iteration<\/li>\n\n\n\n<li>If condition becomes <code>False<\/code>, loop stops<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd39 5. Infinite Loop (IMPORTANT)<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>while True:\n    print(\"Hello\")\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u26a0 This loop <strong>never stops<\/strong> unless manually interrupted.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd39 6. <code>break<\/code> Statement<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Used to <strong>stop the loop immediately<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">Example 6: Stop Loop When Condition Met<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>for i in range(1, 10):\n    if i == 5:\n        break\n    print(i)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1\n2\n3\n4\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 7. <code>continue<\/code> Statement<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Used to <strong>skip the current iteration<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">Example 7: Skip a Number<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>for i in range(1, 6):\n    if i == 3:\n        continue\n    print(i)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1\n2\n4\n5\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\udd39 8. <code>else<\/code> with Loops<\/h3>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">Example 8: Loop with <code>else<\/code><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>for i in range(3):\n    print(i)\nelse:\n    print(\"Loop completed\")\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>0\n1\n2\nLoop completed\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udccc Explanation<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>else<\/code> runs when loop ends <strong>normally<\/strong><\/li>\n\n\n\n<li>It does NOT run if <code>break<\/code> is used<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd39 9. Nested Loops (Loop inside Loop)<\/h3>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">Example 9: Number Pattern<\/h4>\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 class=\"wp-block-paragraph\"><strong>Output<\/strong><\/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\">\ud83d\udd39 10. Real-Life Example: Multiplication Table<\/h3>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">Example 10<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>number = int(input(\"Enter a number: \"))\n\nfor i in range(1, 11):\n    print(number, \"x\", i, \"=\", number * i)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Sample Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter a number: 5\n5 x 1 = 5\n5 x 2 = 10\n5 x 3 = 15\n5 x 4 = 20\n5 x 5 = 25\n5 x 6 = 30\n5 x 7 = 35\n5 x 8 = 40\n5 x 9 = 45\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\">\ud83d\udd39 11. Common Mistakes (IMPORTANT)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u274c Forgetting to update condition in <code>while<\/code> loop<br>\u274c Wrong indentation<br>\u274c Infinite loops accidentally<br>\u274c Using <code>break<\/code> incorrectly<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\udde0 Final Summary<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u2714 <code>for<\/code> loop \u2192 fixed number of times<br>\u2714 <code>while<\/code> loop \u2192 condition-based<br>\u2714 <code>range()<\/code> controls repetition<br>\u2714 <code>break<\/code> stops loop<br>\u2714 <code>continue<\/code> skips iteration<br>\u2714 Nested loops handle complex logic<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\ud83d\udd39 1. What is a Loop? A loop is used to repeat a block of code multiple times. \ud83d\udc49 Without loops \u2192 code becomes long\ud83d\udc49 With loops \u2192 code becomes short &amp; clean Python has mainly two loops: \ud83d\udd39 2. for Loop A for loop is used when number of repetitions is known. Example 1: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,1],"tags":[],"class_list":["post-350","post","type-post","status-publish","format-standard","hentry","category-python-easy-course-examples","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/350","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=350"}],"version-history":[{"count":4,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/350\/revisions"}],"predecessor-version":[{"id":367,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/350\/revisions\/367"}],"wp:attachment":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=350"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=350"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=350"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}