{"id":901,"date":"2024-09-15T11:20:11","date_gmt":"2024-09-15T11:20:11","guid":{"rendered":"https:\/\/www.zframez.com\/articles\/?p=901"},"modified":"2024-10-16T15:52:29","modified_gmt":"2024-10-16T15:52:29","slug":"chapter-6-python-while-and-for-loops-examples-and-explanations","status":"publish","type":"post","link":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-6-python-while-and-for-loops-examples-and-explanations","title":{"rendered":"Chapter 6: Python While and For Loops: Examples and Explanations"},"content":{"rendered":"<body>\n<div class=\"table-of-contents\" style=\"background-color: #066095; color: white; padding: 10px; border-radius: 10px; margin-top: 20px; display: flex; justify-content: space-between; align-items: center; font-weight: 600;\">\n  <span style=\"flex: 1; text-align: left;\">\n    <a href=\"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-5-python-conditional-statements-if-else-and-elif\" style=\"color: white; text-decoration: underline;\">\u25c0 Previous<\/a>\n  <\/span>\n  \n  <span style=\"flex: 1; text-align: center;\">\n    <a href=\"https:\/\/www.zframez.com\/articles\/python-tutorials\" style=\"color: white; text-decoration: underline;\">Python Tutorials<\/a>\n  <\/span>\n  \n  <span style=\"flex: 1; text-align: right;\">\n    <a href=\"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-7-working-with-strings-in-python\" style=\"color: white; text-decoration: underline;\">Next \u25b6<\/a>\n  <\/span>\n<\/div>\n\n\n\n\n<h1 class=\"wp-block-heading has-x-large-font-size\"><strong>Python While and For Loops<\/strong><\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">In this chapter of our Python tutorial series, we will explore two essential looping structures in Python: while loops and for loops. Loops are a fundamental part of programming, allowing you to execute a block of code repeatedly under specific conditions. In this chapter, you\u2019ll learn how to use while loops with the <code>else<\/code> clause, how to control output formatting using Python\u2019s <code>print()<\/code> function, and how to iterate over a range of numbers or lists with for loops. By the end of this chapter, you\u2019ll be able to write efficient loops in Python and apply them to various real-world scenarios.<\/p>\n\n\n\n<h2 class=\"wp-block-heading has-large-font-size\"><strong>While Loop Examples in Python<\/strong> :<\/h2>\n\n\n\n<pre class=\"wp-block-code has-background-color has-foreground-background-color has-text-color has-background has-link-color wp-elements-634e088e5760525447cfffd997af65cc\"><code>a = 5\nb = 1\n\nwhile b &lt;= 5:\n    print (f\" {a} * {b} = {a * b})\n    b += 1<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output: <\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-background-color has-foreground-background-color has-text-color has-background has-link-color wp-elements-d80a9f82a5c538c9afa9bae488b30642\">5 * 1 = 5<br>5 * 2 = 10<br>5 * 3 = 15<br>5 * 4 = 20<br>5 * 5 = 25<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Explanation<\/strong>:<br>In this example, the while loop in Python continues to run as long as the condition <code>b &lt;= 5<\/code> is true. It prints the multiplication table for <code>a = 5<\/code> and increments <code>b<\/code> after each iteration until <code>b<\/code> exceeds 5.<\/p>\n\n\n\n<h2 class=\"wp-block-heading has-large-font-size\"><strong>Using <code>else<\/code> with While Loops in Python<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code has-background-color has-foreground-background-color has-text-color has-background has-link-color has-medium-font-size wp-elements-0f801dde620137ca3fa6764d8e28b901\"><code>a = 1\n\nwhile a &lt;= 3:\n    b = int(input(\"Enter a number: \"))\n    \n    if b == 0:\n        print(\"Exiting loop with break command, 'else' is not executed\")\n        break\n    a += 1\n    \nelse:\n    print(\"Loop exited without executing break command\")\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Explanation<\/strong>:<br>In this Python example, the <code>else<\/code> block is executed only if the while loop completes normally without encountering a <code>break<\/code> statement. If the user enters <code>0<\/code>, the loop breaks, and the <code>else<\/code> block is skipped.<\/p>\n\n\n\n<h2 class=\"wp-block-heading has-large-font-size\"><strong>Using <code>print<\/code> Without a Newline in Python<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code has-background-color has-foreground-background-color has-text-color has-background has-link-color wp-elements-ee68fc5673f08de72ef5d4be45938a69\"><code>a = 1\n\nwhile a &lt; 10:\n    print(a, end=',')\n    a += 1<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output: <\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-background-color has-foreground-background-color has-text-color has-background has-link-color wp-elements-505f55beb5c94af297c7291737f7c009\">1,2,3,4,5,6,7,8,9,<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Explanation<\/strong>:<br>Here, the <code>end=','<\/code> argument in the <code>print()<\/code> function prevents the automatic newline after each printed value. Instead, the numbers are printed on the same line, separated by commas.<\/p>\n\n\n\n<h2 class=\"wp-block-heading has-large-font-size\"><strong>Using the <code>range()<\/code> Function in Python For Loops<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code has-background-color has-foreground-background-color has-text-color has-background has-link-color wp-elements-5df93c54e23410056068be2d3dfce8ab\"><code>for a in range(10):\n    print(a, end=\" \")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-background-color has-foreground-background-color has-text-color has-background has-link-color wp-elements-c7fa330b6e7b86e357702e9aee917c68\">0 1 2 3 4 5 6 7 8 9<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Explanation<\/strong>:<br>The <code>range()<\/code> function in Python generates numbers from 0 to 9, and the for loop iterates through each number, printing them on the same line with spaces in between.<\/p>\n\n\n\n<h2 class=\"wp-block-heading has-large-font-size\"><strong>Multiplication Table with For Loops<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code has-background-color has-foreground-background-color has-text-color has-background has-link-color wp-elements-b4022d8b319b3eedcec1b6026763f260\"><code>a = 5\n\nfor b in range(1, 5):\n    print(f\" {a} * {b} = {a * b})\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-background-color has-foreground-background-color has-text-color has-background has-link-color wp-elements-8472fe230a8d1e7677c9df527c717a72\">5 * 1 = 5<br>5 * 2 = 10<br>5 * 3 = 15<br>5 * 4 = 20<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Explanation<\/strong>:<br>This for loop prints the multiplication table for <code>a = 5<\/code> for values of <code>b<\/code> from 1 to 4, using Python\u2019s <code>range()<\/code> function.<\/p>\n\n\n\n<h2 class=\"wp-block-heading has-large-font-size\"><strong>For Loops with Lists in Python<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code has-background-color has-foreground-background-color has-text-color has-background has-link-color wp-elements-6f5d387af70c4550d4a76eea4a0c14da\"><code>a = [10, 20, 30, 40, 50]\n\nfor b in a:\n    print(b + 5, end=\" \")\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output: <\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-background-color has-foreground-background-color has-text-color has-background has-link-color wp-elements-da78a03aa54667196199d5501ff8ab17\">15 25 35 45 55<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Explanation<\/strong>:<br>In this Python example, a for loop iterates over a list of integers, adds 5 to each element, and prints the result.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\">\n\n\n\n<h2 class=\"wp-block-heading has-large-font-size\" id=\"Programming-Exercises\"><strong>Programming Exercises: <\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Write a Python program to print the square of all numbers from 0 to 10.<\/li>\n\n\n\n<li>Write a Python program to find the sum of all even numbers from 0 to 10.<\/li>\n\n\n\n<li>Write a Python program to read three numbers (a, b, c) and check how many numbers between \u2018a\u2019 and \u2018b\u2019 are divisible by \u2018c\u2019.<\/li>\n\n\n\n<li>Write a Python program to print two columns of numbers, where the first column starts from 1 and increments to 99, while the second column starts from 99 and decrements to 1. Each line should display a pair of numbers in the following format: \n<ul class=\"wp-block-list\">\n<li>1\u2014\u201399<\/li>\n\n\n\n<li>2\u2014\u201398<\/li>\n\n\n\n<li>3\u2014\u201397<\/li>\n\n\n\n<li>\u2026<\/li>\n\n\n\n<li>98\u2014\u20132<\/li>\n\n\n\n<li>99\u2014\u20131<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Write a Python program to read a number and print its binary representation.<\/li>\n\n\n\n<li>Write a Python program to read four numbers (representing the four octets of an IP) and print the next five IP addresses.<\/li>\n\n\n\n<li>Write a Python program to print the factorial of a given number.<\/li>\n\n\n\n<li>Write a Python program to print the first 10 numbers of the Fibonacci series.<\/li>\n\n\n\n<li>Write a Python program to read a number and print a right triangle using \u201c*\u201d.<\/li>\n\n\n\n<li>Write a Python program to check whether a given number is prime or not.<\/li>\n\n\n\n<li>Write a Python program to print all prime numbers between 0 and 100, and also print how many prime numbers are found.<\/li>\n\n\n\n<li>Given <code>a, b, c = 0, 0, 0<\/code>, write a Python program to print all permutations of these three variables.\n<ul class=\"wp-block-list\">\n<li><strong>Output<\/strong>: 000, 001, 002, 003, 004, 005, 006, 007, 008, 009, 010, 011 \u2026 999<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Write a Python program to print all numbers from 1 to 100 that are divisible by both 3 and 5.<\/li>\n\n\n\n<li>Write a Python program to find the sum of the digits of a given number. For example, if the input is 1234, the output should be 10 (since 1 + 2 + 3 + 4 = 10).<\/li>\n\n\n\n<li>Write a Python program to read a number and print its reverse. For example, if the input is 1234, the output should be 4321.<\/li>\n\n\n\n<li>Write a Python program to print all numbers from 1 to 100 that are divisible by both 3 and 5.<\/li>\n\n\n\n<li>Write a Python program to find the sum of the digits of a given number. For example, if the input is 1234, the output should be 10 (since 1 + 2 + 3 + 4 = 10).<\/li>\n\n\n\n<li>Write a Python program to read a number and print its reverse. For example, if the input is 1234, the output should be 4321.<\/li>\n\n\n\n<li>Write a Python program to print all perfect numbers between 1 and 100. (A perfect number is a number whose divisors sum up to the number itself, e.g., 6 = 1 + 2 + 3.)<\/li>\n\n\n\n<li>Write a Python program to find the greatest common divisor (GCD) of two numbers using a loop.<\/li>\n\n\n\n<li>Write a Python program to read a number and print a diamond pattern using \u201c*\u201d, where the height of the diamond is twice the number minus one. For example, if the input is 3, the pattern should look like:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-preformatted has-background-color has-foreground-background-color has-text-color has-background has-link-color wp-elements-666b424dc7213c400f98153c89d0b3eb\">  *<br> ***<br>*****<br> ***<br>  *<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\">\n\n\n\n<div class=\"table-of-contents\" style=\"background-color: #066095; color: white; padding: 10px; border-radius: 10px; margin-top: 20px; display: flex; justify-content: space-between; align-items: center; font-weight: 600;\">\n  <span style=\"flex: 1; text-align: left;\">\n    <a href=\"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-5-python-conditional-statements-if-else-and-elif\" style=\"color: white; text-decoration: underline;\">\u25c0 Previous<\/a>\n  <\/span>\n  \n  <span style=\"flex: 1; text-align: center;\">\n    <a href=\"https:\/\/www.zframez.com\/articles\/python-tutorials\" style=\"color: white; text-decoration: underline;\">Python Tutorials<\/a>\n  <\/span>\n  \n  <span style=\"flex: 1; text-align: right;\">\n    <a href=\"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-7-working-with-strings-in-python\" style=\"color: white; text-decoration: underline;\">Next \u25b6<\/a>\n  <\/span>\n<\/div>\n\n<\/body>","protected":false},"excerpt":{"rendered":"<p>\u25c0 Previous Python Tutorials Next \u25b6 Python While and For Loops In this chapter of our Python tutorial series, we will explore two essential looping structures in Python: while loops and for loops. Loops are a fundamental part of programming, allowing you to execute a block of code repeatedly under specific conditions. In this chapter, [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"wp-custom-template-post-with-sidebar2","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[46],"tags":[279,283,278,282,284,277,50,281,280,276],"class_list":["post-901","post","type-post","status-publish","format-standard","hentry","category-python-tutorials","tag-for-loop-in-python","tag-python-coding-basics","tag-python-else-in-while-loop","tag-python-examples","tag-python-loop-tutorial","tag-python-loops","tag-python-programming","tag-python-range-function","tag-python-tutorials","tag-while-loop-in-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Chapter 6: Python While and For Loops: Examples and Explanations - Tutorials<\/title>\n<meta name=\"description\" content=\"Learn how to use while and for loops in Python with practical examples. This tutorial covers while loops, else with while loops, for loops, and the range function.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-6-python-while-and-for-loops-examples-and-explanations\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Master Python Loops: While and For Loops Explained\" \/>\n<meta property=\"og:description\" content=\"This Python tutorial covers the basics of while loops, for loops, and the range function with easy-to-follow examples. Start learning Python loops now!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-6-python-while-and-for-loops-examples-and-explanations\" \/>\n<meta property=\"og:site_name\" content=\"Tutorials\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/zframez\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-09-15T11:20:11+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-10-16T15:52:29+00:00\" \/>\n<meta name=\"author\" content=\"sajith achipra\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@zframez\" \/>\n<meta name=\"twitter:site\" content=\"@zframez\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"sajith achipra\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/python-tutorials\\\/chapter-6-python-while-and-for-loops-examples-and-explanations#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/python-tutorials\\\/chapter-6-python-while-and-for-loops-examples-and-explanations\"},\"author\":{\"name\":\"sajith achipra\",\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/#\\\/schema\\\/person\\\/8b3b88007644501771d2452d3cc80f41\"},\"headline\":\"Chapter 6: Python While and For Loops: Examples and Explanations\",\"datePublished\":\"2024-09-15T11:20:11+00:00\",\"dateModified\":\"2024-10-16T15:52:29+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/python-tutorials\\\/chapter-6-python-while-and-for-loops-examples-and-explanations\"},\"wordCount\":750,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/#organization\"},\"keywords\":[\"for loop in Python\",\"Python coding basics\",\"Python else in while loop\",\"Python examples\",\"Python loop tutorial\",\"Python loops\",\"python programming\",\"Python range function\",\"Python tutorials\",\"while loop in Python\"],\"articleSection\":[\"python tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.zframez.com\\\/articles\\\/python-tutorials\\\/chapter-6-python-while-and-for-loops-examples-and-explanations#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/python-tutorials\\\/chapter-6-python-while-and-for-loops-examples-and-explanations\",\"url\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/python-tutorials\\\/chapter-6-python-while-and-for-loops-examples-and-explanations\",\"name\":\"Chapter 6: Python While and For Loops: Examples and Explanations - Tutorials\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/#website\"},\"datePublished\":\"2024-09-15T11:20:11+00:00\",\"dateModified\":\"2024-10-16T15:52:29+00:00\",\"description\":\"Learn how to use while and for loops in Python with practical examples. This tutorial covers while loops, else with while loops, for loops, and the range function.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/python-tutorials\\\/chapter-6-python-while-and-for-loops-examples-and-explanations#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.zframez.com\\\/articles\\\/python-tutorials\\\/chapter-6-python-while-and-for-loops-examples-and-explanations\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/python-tutorials\\\/chapter-6-python-while-and-for-loops-examples-and-explanations#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Tutorials\",\"item\":\"https:\\\/\\\/www.zframez.com\\\/articles\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Chapter 6: Python While and For Loops: Examples and Explanations\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/#website\",\"url\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/\",\"name\":\"zframez tutorials\",\"description\":\"Learn networking bit by bit\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/#organization\",\"name\":\"zframez technologies\",\"url\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/www.zframez.com\\\/articles\\\/wp-content\\\/uploads\\\/2024\\\/07\\\/zframez-logo.jpg?fit=864%2C864&ssl=1\",\"contentUrl\":\"https:\\\/\\\/i0.wp.com\\\/www.zframez.com\\\/articles\\\/wp-content\\\/uploads\\\/2024\\\/07\\\/zframez-logo.jpg?fit=864%2C864&ssl=1\",\"width\":864,\"height\":864,\"caption\":\"zframez technologies\"},\"image\":{\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/zframez\\\/\",\"https:\\\/\\\/x.com\\\/zframez\",\"https:\\\/\\\/www.instagram.com\\\/zframez_technologies\\\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/#\\\/schema\\\/person\\\/8b3b88007644501771d2452d3cc80f41\",\"name\":\"sajith achipra\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3d9f27c5311500982b6f19d03d0506f1c328f30f51d8d5f73f46577687fd81f8?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3d9f27c5311500982b6f19d03d0506f1c328f30f51d8d5f73f46577687fd81f8?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3d9f27c5311500982b6f19d03d0506f1c328f30f51d8d5f73f46577687fd81f8?s=96&d=mm&r=g\",\"caption\":\"sajith achipra\"},\"sameAs\":[\"http:\\\/\\\/www.zframez.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Chapter 6: Python While and For Loops: Examples and Explanations - Tutorials","description":"Learn how to use while and for loops in Python with practical examples. This tutorial covers while loops, else with while loops, for loops, and the range function.","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:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-6-python-while-and-for-loops-examples-and-explanations","og_locale":"en_US","og_type":"article","og_title":"Master Python Loops: While and For Loops Explained","og_description":"This Python tutorial covers the basics of while loops, for loops, and the range function with easy-to-follow examples. Start learning Python loops now!","og_url":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-6-python-while-and-for-loops-examples-and-explanations","og_site_name":"Tutorials","article_publisher":"https:\/\/www.facebook.com\/zframez\/","article_published_time":"2024-09-15T11:20:11+00:00","article_modified_time":"2024-10-16T15:52:29+00:00","author":"sajith achipra","twitter_card":"summary_large_image","twitter_creator":"@zframez","twitter_site":"@zframez","twitter_misc":{"Written by":"sajith achipra","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-6-python-while-and-for-loops-examples-and-explanations#article","isPartOf":{"@id":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-6-python-while-and-for-loops-examples-and-explanations"},"author":{"name":"sajith achipra","@id":"https:\/\/www.zframez.com\/articles\/#\/schema\/person\/8b3b88007644501771d2452d3cc80f41"},"headline":"Chapter 6: Python While and For Loops: Examples and Explanations","datePublished":"2024-09-15T11:20:11+00:00","dateModified":"2024-10-16T15:52:29+00:00","mainEntityOfPage":{"@id":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-6-python-while-and-for-loops-examples-and-explanations"},"wordCount":750,"commentCount":0,"publisher":{"@id":"https:\/\/www.zframez.com\/articles\/#organization"},"keywords":["for loop in Python","Python coding basics","Python else in while loop","Python examples","Python loop tutorial","Python loops","python programming","Python range function","Python tutorials","while loop in Python"],"articleSection":["python tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-6-python-while-and-for-loops-examples-and-explanations#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-6-python-while-and-for-loops-examples-and-explanations","url":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-6-python-while-and-for-loops-examples-and-explanations","name":"Chapter 6: Python While and For Loops: Examples and Explanations - Tutorials","isPartOf":{"@id":"https:\/\/www.zframez.com\/articles\/#website"},"datePublished":"2024-09-15T11:20:11+00:00","dateModified":"2024-10-16T15:52:29+00:00","description":"Learn how to use while and for loops in Python with practical examples. This tutorial covers while loops, else with while loops, for loops, and the range function.","breadcrumb":{"@id":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-6-python-while-and-for-loops-examples-and-explanations#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-6-python-while-and-for-loops-examples-and-explanations"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-6-python-while-and-for-loops-examples-and-explanations#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Tutorials","item":"https:\/\/www.zframez.com\/articles"},{"@type":"ListItem","position":2,"name":"Chapter 6: Python While and For Loops: Examples and Explanations"}]},{"@type":"WebSite","@id":"https:\/\/www.zframez.com\/articles\/#website","url":"https:\/\/www.zframez.com\/articles\/","name":"zframez tutorials","description":"Learn networking bit by bit","publisher":{"@id":"https:\/\/www.zframez.com\/articles\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.zframez.com\/articles\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.zframez.com\/articles\/#organization","name":"zframez technologies","url":"https:\/\/www.zframez.com\/articles\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.zframez.com\/articles\/#\/schema\/logo\/image\/","url":"https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/07\/zframez-logo.jpg?fit=864%2C864&ssl=1","contentUrl":"https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/07\/zframez-logo.jpg?fit=864%2C864&ssl=1","width":864,"height":864,"caption":"zframez technologies"},"image":{"@id":"https:\/\/www.zframez.com\/articles\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/zframez\/","https:\/\/x.com\/zframez","https:\/\/www.instagram.com\/zframez_technologies\/"]},{"@type":"Person","@id":"https:\/\/www.zframez.com\/articles\/#\/schema\/person\/8b3b88007644501771d2452d3cc80f41","name":"sajith achipra","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/3d9f27c5311500982b6f19d03d0506f1c328f30f51d8d5f73f46577687fd81f8?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/3d9f27c5311500982b6f19d03d0506f1c328f30f51d8d5f73f46577687fd81f8?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3d9f27c5311500982b6f19d03d0506f1c328f30f51d8d5f73f46577687fd81f8?s=96&d=mm&r=g","caption":"sajith achipra"},"sameAs":["http:\/\/www.zframez.com"]}]}},"jetpack_featured_media_url":"","jetpack-related-posts":[{"id":83,"url":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-1-introduction-to-python-programming","url_meta":{"origin":901,"position":0},"title":"Chapter 1: Introduction to Python Programming","author":"sajith achipra","date":"September 4, 2024","format":false,"excerpt":"\u25c0 Previous Python Tutorials Next \u25b6 In this Python tutorial, we'll explore Python's origins, key milestones, and its standout features and benefits. Whether you're new to programming or looking to expand your skills, this guide will provide you with a solid foundation in Python. If you want to understand why\u2026","rel":"","context":"In &quot;python tutorials&quot;","block_context":{"text":"python tutorials","link":"https:\/\/www.zframez.com\/articles\/category\/python-tutorials"},"img":{"alt_text":"Table filled with Python-related materials, including a computer displaying Python code, a Python textbook, and notes about Python","src":"https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/working-with-python.webp?fit=1024%2C1024&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/working-with-python.webp?fit=1024%2C1024&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/working-with-python.webp?fit=1024%2C1024&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/working-with-python.webp?fit=1024%2C1024&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":98,"url":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-3-working-with-variables-in-python","url_meta":{"origin":901,"position":1},"title":"Chapter 3: Working with Variables in Python","author":"sajith achipra","date":"September 4, 2024","format":false,"excerpt":"\u25c0 Previous Python Tutorials Next \u25b6 In Python, variables are one of the fundamental concepts you'll work with. They allow you to store data that your programs can manipulate and use. In this chapter, we\u2019ll explore what variables are, how to check the Python version you\u2019re using, and the different\u2026","rel":"","context":"In &quot;python tutorials&quot;","block_context":{"text":"python tutorials","link":"https:\/\/www.zframez.com\/articles\/category\/python-tutorials"},"img":{"alt_text":"Computer displaying a program, with a book and pen on the table, representing learning and coding in Python.","src":"https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/Understanding-Python-variables.jpg?fit=684%2C457&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/Understanding-Python-variables.jpg?fit=684%2C457&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/Understanding-Python-variables.jpg?fit=684%2C457&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":904,"url":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-7-working-with-strings-in-python","url_meta":{"origin":901,"position":2},"title":"Chapter 7: Working with Strings in Python","author":"sajith achipra","date":"September 15, 2024","format":false,"excerpt":"\u25c0 Previous Python Tutorials Next \u25b6 Python String Manipulation: Methods and Examples In Chapter 7, we\u2019ll be looking at how to work with strings in Python. Strings are used everywhere in programming, and Python provides a lot of methods to make string manipulation easy. In this chapter, we\u2019ll explore methods\u2026","rel":"","context":"In &quot;python tutorials&quot;","block_context":{"text":"python tutorials","link":"https:\/\/www.zframez.com\/articles\/category\/python-tutorials"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":751,"url":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-5-python-conditional-statements-if-else-and-elif","url_meta":{"origin":901,"position":3},"title":"Chapter 5:Python Conditional Statements: if, else, and elif","author":"sajith achipra","date":"September 11, 2024","format":false,"excerpt":"\u25c0 Previous Python Tutorials Next \u25b6 Table of Contents Introduction to Python `if` and `else` Understanding Python `if` Syntax Multiple Lines of Code in an `if` Statement Using `else` with `if` How does Python know when the `if` code block is finished? Using the `pass` Statement Checking Odd and Even\u2026","rel":"","context":"In &quot;python tutorials&quot;","block_context":{"text":"python tutorials","link":"https:\/\/www.zframez.com\/articles\/category\/python-tutorials"},"img":{"alt_text":"Block diagram illustrating an if expression and corresponding code block in Python, with a Python icon in the top right corner.","src":"https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/Python-conditional-statments-if-else.jpg?fit=1200%2C675&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/Python-conditional-statments-if-else.jpg?fit=1200%2C675&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/Python-conditional-statments-if-else.jpg?fit=1200%2C675&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/Python-conditional-statments-if-else.jpg?fit=1200%2C675&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/Python-conditional-statments-if-else.jpg?fit=1200%2C675&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":90,"url":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-2-setting-up-python","url_meta":{"origin":901,"position":4},"title":"Chapter 2: Setting Up Python","author":"sajith achipra","date":"September 4, 2024","format":false,"excerpt":"\u25c0 Previous Python Tutorials Next \u25b6 Before you can start writing and running Python code, you\u2019ll need to set up Python on your computer. In this chapter, we\u2019ll guide you through the process of installing Python on Windows, macOS, and Linux. We\u2019ll also explain what an Integrated Development Environment (IDE)\u2026","rel":"","context":"In &quot;python tutorials&quot;","block_context":{"text":"python tutorials","link":"https:\/\/www.zframez.com\/articles\/category\/python-tutorials"},"img":{"alt_text":"Screenshot of the official Python download page, highlighting the process of setting up Python.","src":"https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/download-and-install-python.png?fit=1200%2C499&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/download-and-install-python.png?fit=1200%2C499&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/download-and-install-python.png?fit=1200%2C499&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/download-and-install-python.png?fit=1200%2C499&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/download-and-install-python.png?fit=1200%2C499&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":908,"url":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-8-python-lists-and-tuples-operations-and-methods","url_meta":{"origin":901,"position":5},"title":"Chapter 8: Python Lists and Tuples -Operations and Methods","author":"sajith achipra","date":"September 15, 2024","format":false,"excerpt":"\u25c0 Previous Python Tutorials Next \u25b6 Working with Lists and Tuples in Python In this chapter, we\u2019ll explore two important data structures in Python: lists and tuples. Lists allow you to store a collection of items that can be modified, while tuples are immutable, meaning their elements cannot be changed.\u2026","rel":"","context":"In &quot;python tutorials&quot;","block_context":{"text":"python tutorials","link":"https:\/\/www.zframez.com\/articles\/category\/python-tutorials"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.zframez.com\/articles\/wp-json\/wp\/v2\/posts\/901","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zframez.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zframez.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zframez.com\/articles\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zframez.com\/articles\/wp-json\/wp\/v2\/comments?post=901"}],"version-history":[{"count":5,"href":"https:\/\/www.zframez.com\/articles\/wp-json\/wp\/v2\/posts\/901\/revisions"}],"predecessor-version":[{"id":1082,"href":"https:\/\/www.zframez.com\/articles\/wp-json\/wp\/v2\/posts\/901\/revisions\/1082"}],"wp:attachment":[{"href":"https:\/\/www.zframez.com\/articles\/wp-json\/wp\/v2\/media?parent=901"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zframez.com\/articles\/wp-json\/wp\/v2\/categories?post=901"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zframez.com\/articles\/wp-json\/wp\/v2\/tags?post=901"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}