{"id":91347,"date":"2023-07-18T17:31:19","date_gmt":"2023-07-18T12:01:19","guid":{"rendered":"https:\/\/www.mygreatlearning.com\/blog\/for-loop-in-python-with-examples\/"},"modified":"2024-08-15T22:26:22","modified_gmt":"2024-08-15T16:56:22","slug":"for-loop-in-python-with-examples","status":"publish","type":"post","link":"https:\/\/www.mygreatlearning.com\/blog\/for-loop-in-python-with-examples\/","title":{"rendered":"For Loop in Python with Examples"},"content":{"rendered":"\n<p>If you've ever wondered how to efficiently repeat a task in Python, you're in the right place. In this blog, we'll explore the world of loops, with a focus on the \"for\" loop in Python. In programming, loops are a powerful tool that allow us to repeat a block of code multiple times. They provide a way to automate repetitive tasks, making our lives as programmers a whole lot easier.<\/p>\n\n\n\n<p>Loops play a crucial role in programming\u2014imagine having to manually write the same code over and over again for every repetition. It would be time-consuming and error-prone. That's where loops come to the rescue! They let us write concise and efficient code by automating repetitive processes. Whether it's processing a large amount of data, iterating over a list, or performing calculations, loops are the go-to solution.<\/p>\n\n\n\n<p>For loop provides a convenient way to iterate over a sequence of elements such as lists, tuples, strings, and more. We'll explore how to use the for loop to iterate through each item in a collection and perform actions on them. Let's take a step-by-step approach to understand the for loop syntax, how it works, loop control statements, and advanced loop techniques.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"the-for-loop-syntax\">The \"for\" Loop Syntax<\/h2>\n\n\n\n<p>We use the keyword \"for\" followed by a variable name, the keyword \"in,\" and a sequence of elements. The loop then iterates over each item in the sequence, executing the code block inside the loop for each iteration. Here\u2019s what it looks like:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">fruits = [\"apple\", \"banana\", \"orange\"]\n\nfor fruit in fruits:\n\n&nbsp;&nbsp;&nbsp;&nbsp;print(fruit)<\/pre>\n\n\n\n<p>Here, the loop iterates over each item in the \"fruits\" list and prints it. We define a variable called \"fruit\" that takes on the value of each item in the list during each iteration. The loop executes the code block inside for each fruit, printing its name.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"iterating-over-different-types-of-objects-using-for-loops\">Iterating over different types of objects using \"for\" loops<\/h3>\n\n\n\n<p>Since \"for\" loops are versatile, they can iterate over various types of objects, including lists, tuples, strings, and more. Whether you have a collection of numbers, names, or even characters, you can easily loop through them using a \"for\" loop.<\/p>\n\n\n\n<p>For example, you can loop through a string's characters like this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">message = \"Hello, World!\"\n\nfor char in message:\n\n&nbsp;&nbsp;&nbsp;&nbsp;print(char)<\/pre>\n\n\n\n<p>This loop iterates over each character in the \"message\" string and prints it individually. The loop allows us to process each character separately.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"utilizing-the-range-function-in-for-loops\">Utilizing the range() function in \"for\" loops<\/h3>\n\n\n\n<p>Python provides a useful function called \"range()\" that works hand in hand with \"for\" loops. The \"range()\" function generates a sequence of numbers that can be used to control the number of loop iterations.<\/p>\n\n\n\n<p>Here's an example of using \"range()\" in a \"for\" loop:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">for num in range(1, 6):\n\n&nbsp;&nbsp;&nbsp;&nbsp;print(num)<\/pre>\n\n\n\n<p>In this case, the loop iterates over the numbers 1 to 5 (inclusive). The \"range(1, 6)\" generates a sequence from 1 to 5, and the loop prints each number in the sequence.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"nested-loops-and-their-applications\">Nested loops and their applications<\/h3>\n\n\n\n<p>Nested loops are loops within loops. They allow us to perform more complex tasks that involve multiple iterations. For example, if you want to print a pattern or iterate over a two-dimensional list, we can use nested loops.<\/p>\n\n\n\n<p>Here's an example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">for i in range(1, 4):\n\n&nbsp;&nbsp;&nbsp;&nbsp;for j in range(1, 4):\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(i, j)<\/pre>\n\n\n\n<p>In this case, we have two nested loops. The outer loop iterates over the numbers 1 to 3, and for each iteration, the inner loop also iterates over the numbers 1 to 3. The loop prints the combination of values from both loops.<\/p>\n\n\n\n<p>Nested loops are powerful tools that can handle complex scenarios and help us solve various programming challenges.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"loop-control-statements\">Loop Control Statements<\/h2>\n\n\n\n<p>When working with loops in Python, we have some handy control statements that let us modify the flow and behavior of the loops. These control statements are \"break,\" \"continue,\" and \"pass.\"<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\"break\" statement<\/li>\n<\/ol>\n\n\n\n<p>The \"break\" statement is used to immediately terminate the loop, regardless of whether the loop condition is still true or not. It provides a way to exit the loop prematurely based on a specific condition or event.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">fruits = [\"apple\", \"banana\", \"orange\", \"kiwi\", \"mango\"]\n\nfor fruit in fruits:\n\n&nbsp;&nbsp;&nbsp;&nbsp;if fruit == \"orange\":\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break\n\n&nbsp;&nbsp;&nbsp;&nbsp;print(fruit)<\/pre>\n\n\n\n<p>Here, the loop iterates over the \"fruits\" list. When it encounters the \"orange\" fruit, the \"break\" statement is triggered, and the loop ends immediately.&nbsp;<\/p>\n\n\n\n<p>The output will only be \"apple\" and \"banana.\"<\/p>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li>\"continue\" statement<\/li>\n<\/ol>\n\n\n\n<p>The \"continue\" statement is used to skip the remaining code within the current iteration and move on to the next iteration of the loop. It allows us to skip specific iterations based on certain conditions.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">numbers = [1, 2, 3, 4, 5]\n\nfor num in numbers:\n\n&nbsp;&nbsp;&nbsp;&nbsp;if num % 2 == 0:\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;continue\n\n&nbsp;&nbsp;&nbsp;&nbsp;print(num)<\/pre>\n\n\n\n<p>Here, the loop iterates over the \"numbers\" list. When it encounters an even number (divisible by 2), the \"continue\" statement is triggered, and the remaining code for that iteration is skipped. The loop proceeds to the next iteration.&nbsp;<\/p>\n\n\n\n<p>The output will only be the odd numbers: 1, 3, and 5.<\/p>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li>\"pass\" statement<\/li>\n<\/ol>\n\n\n\n<p>The \"pass\" statement is used as a placeholder when we need a statement syntactically but don't want to perform any action. It is often used as a temporary placeholder during development, allowing us to write incomplete code that doesn't raise an error.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">for i in range(5):\n\n&nbsp;&nbsp;&nbsp;&nbsp;if i == 3:\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pass\n\n&nbsp;&nbsp;&nbsp;&nbsp;print(i)<\/pre>\n\n\n\n<p>Here, the loop iterates over the range from 0 to 4. When the value of \"i\" is 3, the \"pass\" statement is encountered, and it does nothing.&nbsp;<\/p>\n\n\n\n<p>The loop continues to execute, and the output will be all the numbers from 0 to 4.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"best-practices-and-tips-for-using-loops\">Best Practices and Tips for Using Loops<\/h2>\n\n\n\n<p>There are a lot of tips and tricks you can utilize when working around loops, some of which are:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"writing-efficient-loop-code\">Writing efficient loop code<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Minimize unnecessary computations: <\/strong>Perform calculations or operations outside the loop when possible to avoid redundant calculations within each iteration.<\/li>\n\n\n\n<li><strong>Preallocate memory for lists or arrays: <\/strong>If you know the size of the data you'll be working with, allocate memory beforehand to avoid frequent resizing, improving performance.<\/li>\n\n\n\n<li><strong>Use appropriate data structures: <\/strong>Choose the right data structure for your task. For example, use sets for membership checks or dictionaries for quick lookups.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"avoiding-common-pitfalls-and-mistakes\">Avoiding common pitfalls and mistakes<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Infinite loops: <\/strong>Ensure that your loop has a clear exit condition to prevent infinite loops that can crash your program. Double-check your loop conditions and update variables correctly.<\/li>\n\n\n\n<li><strong>Off-by-one errors:<\/strong> Be careful with loop boundaries and indexes. Ensure that you're including all necessary elements and not exceeding the range of your data.<\/li>\n\n\n\n<li><strong>Unintentional variable modifications:<\/strong> Make sure you're not accidentally modifying loop variables within the loop body, as this can lead to unexpected results.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"optimizing-loop-performance\">Optimizing loop performance<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Use built-in functions and libraries: <\/strong>Utilize built-in functions like sum(), max(), or libraries like NumPy for optimized computations instead of manually iterating over elements.<\/li>\n\n\n\n<li><strong>Vectorize operations: <\/strong>Whenever possible, perform operations on arrays instead of iterating through individual elements, as array operations are typically faster.<\/li>\n\n\n\n<li><strong>Consider parallelization: <\/strong>If you have computationally intensive tasks, explore parallel processing libraries like<em> \u2018multiprocessing\u2019<\/em> or <em>\u2018concurrent.futures\u2019<\/em> to utilize multiple cores or threads.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"advanced-loop-techniques\">Advanced Loop Techniques<\/h2>\n\n\n\n<p>Now that we understand the basic foundation that loops sit on, let\u2019s look at its advanced techniques.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"list-comprehensions-and-their-advantages\">List comprehensions and their advantages<\/h3>\n\n\n\n<p><a href=\"https:\/\/www.mygreatlearning.com\/blog\/list-comprehension-python\/\">List comprehensions<\/a> are a concise and powerful way to create new lists by iterating over an existing sequence. They offer several advantages, including shorter and more readable code, reduced lines of code, and improved performance compared to traditional loops. List comprehensions can also incorporate conditions for filtering elements.<\/p>\n\n\n\n<p>numbers = [1, 2, 3, 4, 5]<\/p>\n\n\n\n<p>squared_numbers = [num ** 2 for num in numbers]<\/p>\n\n\n\n<p>Here, the list comprehension creates a new list called \"squared_numbers\" by squaring each element in the \"numbers\" list. The result will be [1, 4, 9, 16, 25].<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"generator-expressions-for-memory-efficient-iterations\">Generator expressions for memory-efficient iterations<\/h3>\n\n\n\n<p>Generator expressions are similar to list comprehensions, but instead of creating a new list, they generate values on the fly as they are needed. This makes them memory-efficient when working with large data sets or infinite sequences. Generator expressions are enclosed in parentheses instead of brackets.<\/p>\n\n\n\n<p>numbers = [1, 2, 3, 4, 5]<\/p>\n\n\n\n<p>squared_numbers = (num ** 2 for num in numbers)<\/p>\n\n\n\n<p>Here, the generator expression generates squared numbers on the fly without creating a new list. You can iterate over the generator expression to access the squared numbers one by one. This approach saves memory when dealing with large data sets.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"using-the-enumerate-function-for-indexing-in-loops\">Using the enumerate() function for indexing in loops<\/h3>\n\n\n\n<p>The enumerate() function is a handy tool when you need to iterate over a sequence and also track the index of each element. It returns both the index and the value of each element, making it easier to access or manipulate elements based on their positions.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">fruits = [\"apple\", \"banana\", \"orange\"]\n\nfor index, fruit in enumerate(fruits):\n\n&nbsp;&nbsp;&nbsp;&nbsp;print(f\"Index: {index}, Fruit: {fruit}\")<\/pre>\n\n\n\n<p>In this example, the enumerate() function is used to iterate over the \"fruits\" list. The loop prints the index and corresponding fruit for each iteration. The output will be:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Index: 0, Fruit: apple\n\nIndex: 1, Fruit: banana\n\nIndex: 2, Fruit: orange<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"real-world-examples-and-applications\">Real-world Examples and Applications<\/h2>\n\n\n\n<p>Loops find numerous applications in real-world scenarios, making it easier to process data, handle files, and perform various tasks. Here are a few practical examples:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Processing data:<\/strong> Loops are often used to process large data sets efficiently. You can read data from a file or a database and iterate over each record to perform calculations, filter data, or generate reports.<\/li>\n\n\n\n<li><strong>File handling<\/strong>: Loops are handy when working with files. For instance, you can iterate over lines in a text file, process each line, and extract relevant information.<\/li>\n\n\n\n<li><strong>Web scraping: <\/strong>Loops are essential in web scraping, where you extract data from websites. You can iterate over a list of URLs, send requests, parse the HTML content, and extract the desired information.<\/li>\n\n\n\n<li><strong>Image processing:<\/strong> Loops are frequently used in image processing tasks. For example, you can iterate over the pixels of an image to perform operations such as resizing, filtering, or enhancing the image.<\/li>\n<\/ul>\n\n\n\n<p>Combining loops with conditional statements enables you to create complex logic and make decisions based on specific conditions. Here's an example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">numbers = [1, 2, 3, 4, 5]\n\neven_squares = []\n\nfor num in numbers:\n\n&nbsp;&nbsp;&nbsp;&nbsp;if num % 2 == 0:\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;square = num ** 2\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;even_squares.append(square)\n\nprint(even_squares)<\/pre>\n\n\n\n<p>Here, the loop iterates over the \"numbers\" list. For each number, the conditional statement checks if it's even (num % 2 == 0). If it is, the number is squared, and the squared value is added to the \"even_squares\" list. Finally, the list is printed, resulting in [4, 16], as only the even numbers were squared.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"the-while-loop\">The \"while\" Loop<\/h2>\n\n\n\n<p>Now that we've covered the \"for\" loop, let's explore another essential loop in Python\u2014the \"while\" loop. We use the keyword \"while\" followed by a condition that determines whether the loop should continue or not. As long as the condition remains true, the loop keeps executing the code block inside it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"demonstration-of-basic-while-loop-usage\">Demonstration of basic \"while\" loop usage<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">counter = 0\n\nwhile counter &lt; 5:\n\n&nbsp;&nbsp;&nbsp;&nbsp;print(\"Loop iteration:\", counter)\n\n&nbsp;&nbsp;&nbsp;&nbsp;counter += 1<\/pre>\n\n\n\n<p>Here, the loop will continue running as long as the value of the counter variable is less than 5. With each iteration, the value of the counter increases by 1. The loop prints the current iteration number, starting from 0 and ending at 4.<\/p>\n\n\n\n<p>\"While\" loops are particularly useful when we don't know in advance how many times a loop should run. Some common scenarios where \"while\" loops shine include user input validation, game loops, and reading data until a specific condition is met. They let us keep looping until a desired outcome is achieved.<\/p>\n\n\n\n<p>You can use a \"while\" loop to prompt a user for valid input until they provide a correct answer. This ensures that your program doesn't progress until the necessary conditions are met.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"loop-control-statements-break-and-continue-within-while-loop\">Loop control statements (break and continue) within \"while\" loop<\/h3>\n\n\n\n<p>Within a \"while\" loop, we have two control statements: \"break\" and \"continue.\" These statements allow us to modify the flow of the loop.<\/p>\n\n\n\n<p>The \"break\" statement immediately terminates the loop, regardless of whether the loop condition is still true or not. It's handy when we want to exit the loop prematurely, usually based on a certain condition or event.<\/p>\n\n\n\n<p>On the other hand, the \"continue\" statement skips the remaining code within the current iteration and moves on to the next iteration of the loop. It's useful when we want to skip specific iterations based on certain conditions.<\/p>\n\n\n\n<p>By utilizing these control statements wisely, we can have more control over the flow and behavior of our \"while\" loops.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"concluding-thoughts\">Concluding Thoughts<\/h2>\n\n\n\n<p>We understood what loops are and their importance in programming. We also learned their syntax, usage, and loop control statements like \"break,\" \"continue,\" and \"pass\" which provide additional control over the loop's behavior. Additionally, we explored advanced loop techniques such as list comprehensions, generator expressions, and the use of the enumerate() function.<\/p>\n\n\n\n<p>Now, the best way to become proficient in using loops is through practice and experimentation. Don't hesitate to write your code, create small projects, and challenge yourself with different scenarios. The more you practice, the more comfortable and creative you'll become in applying loops to solve problems.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you've ever wondered how to efficiently repeat a task in Python, you're in the right place. In this blog, we'll explore the world of loops, with a focus on the \"for\" loop in Python. In programming, loops are a powerful tool that allow us to repeat a block of code multiple times. They provide [&hellip;]<\/p>\n","protected":false},"author":41,"featured_media":91359,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_uag_custom_page_level_css":"","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":"set","ast-page-background-enabled":"default","ast-page-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":"","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-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":"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":""},"mobile":{"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":""}},"footnotes":""},"categories":[25860],"tags":[36796],"content_type":[36252],"class_list":["post-91347","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software","tag-python","content_type-tutorials"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.3 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>For Loop in Python with Examples<\/title>\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.mygreatlearning.com\/blog\/for-loop-in-python-with-examples\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"For Loop in Python with Examples\" \/>\n<meta property=\"og:description\" content=\"If you&#039;ve ever wondered how to efficiently repeat a task in Python, you&#039;re in the right place. In this blog, we&#039;ll explore the world of loops, with a focus on the &quot;for&quot; loop in Python. In programming, loops are a powerful tool that allow us to repeat a block of code multiple times. They provide [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mygreatlearning.com\/blog\/for-loop-in-python-with-examples\/\" \/>\n<meta property=\"og:site_name\" content=\"Great Learning Blog: Free Resources what Matters to shape your Career!\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/GreatLearningOfficial\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-18T12:01:19+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-08-15T16:56:22+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2023\/07\/iStock-1319504085.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"747\" \/>\n\t<meta property=\"og:image:height\" content=\"467\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Great Learning Editorial Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/Great_Learning\" \/>\n<meta name=\"twitter:site\" content=\"@Great_Learning\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Great Learning Editorial Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/for-loop-in-python-with-examples\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/for-loop-in-python-with-examples\\\/\"},\"author\":{\"name\":\"Great Learning Editorial Team\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\"},\"headline\":\"For Loop in Python with Examples\",\"datePublished\":\"2023-07-18T12:01:19+00:00\",\"dateModified\":\"2024-08-15T16:56:22+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/for-loop-in-python-with-examples\\\/\"},\"wordCount\":2038,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/for-loop-in-python-with-examples\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2023\\\/07\\\/iStock-1319504085.jpg\",\"keywords\":[\"python\"],\"articleSection\":[\"IT\\\/Software Development\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/for-loop-in-python-with-examples\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/for-loop-in-python-with-examples\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/for-loop-in-python-with-examples\\\/\",\"name\":\"For Loop in Python with Examples\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/for-loop-in-python-with-examples\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/for-loop-in-python-with-examples\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2023\\\/07\\\/iStock-1319504085.jpg\",\"datePublished\":\"2023-07-18T12:01:19+00:00\",\"dateModified\":\"2024-08-15T16:56:22+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/for-loop-in-python-with-examples\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/for-loop-in-python-with-examples\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/for-loop-in-python-with-examples\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2023\\\/07\\\/iStock-1319504085.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2023\\\/07\\\/iStock-1319504085.jpg\",\"width\":747,\"height\":467},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/for-loop-in-python-with-examples\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"IT\\\/Software Development\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/software\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"For Loop in Python with Examples\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"name\":\"Great Learning Blog\",\"description\":\"Learn, Upskill &amp; Career Development Guide and Resources\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"alternateName\":\"Great Learning\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\",\"name\":\"Great Learning\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"width\":900,\"height\":900,\"caption\":\"Great Learning\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/GreatLearningOfficial\\\/\",\"https:\\\/\\\/x.com\\\/Great_Learning\",\"https:\\\/\\\/www.instagram.com\\\/greatlearningofficial\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/in.pinterest.com\\\/greatlearning12\\\/\",\"https:\\\/\\\/www.youtube.com\\\/user\\\/beaconelearning\\\/\"],\"description\":\"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.\",\"email\":\"info@mygreatlearning.com\",\"legalName\":\"Great Learning Education Services Pvt. Ltd\",\"foundingDate\":\"2013-11-29\",\"numberOfEmployees\":{\"@type\":\"QuantitativeValue\",\"minValue\":\"1001\",\"maxValue\":\"5000\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\",\"name\":\"Great Learning Editorial Team\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"caption\":\"Great Learning Editorial Team\"},\"description\":\"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.\",\"sameAs\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/\",\"https:\\\/\\\/in.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/Great_Learning\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCObs0kLIrDjX2LLSybqNaEA\"],\"award\":[\"Best EdTech Company of the Year 2024\",\"Education Economictimes Outstanding Education\\\/Edtech Solution Provider of the Year 2024\",\"Leading E-learning Platform 2024\"],\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/author\\\/greatlearning\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"For Loop in Python with Examples","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.mygreatlearning.com\/blog\/for-loop-in-python-with-examples\/","og_locale":"en_US","og_type":"article","og_title":"For Loop in Python with Examples","og_description":"If you've ever wondered how to efficiently repeat a task in Python, you're in the right place. In this blog, we'll explore the world of loops, with a focus on the \"for\" loop in Python. In programming, loops are a powerful tool that allow us to repeat a block of code multiple times. They provide [&hellip;]","og_url":"https:\/\/www.mygreatlearning.com\/blog\/for-loop-in-python-with-examples\/","og_site_name":"Great Learning Blog: Free Resources what Matters to shape your Career!","article_publisher":"https:\/\/www.facebook.com\/GreatLearningOfficial\/","article_published_time":"2023-07-18T12:01:19+00:00","article_modified_time":"2024-08-15T16:56:22+00:00","og_image":[{"width":747,"height":467,"url":"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2023\/07\/iStock-1319504085.jpg","type":"image\/jpeg"}],"author":"Great Learning Editorial Team","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/Great_Learning","twitter_site":"@Great_Learning","twitter_misc":{"Written by":"Great Learning Editorial Team","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.mygreatlearning.com\/blog\/for-loop-in-python-with-examples\/#article","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/for-loop-in-python-with-examples\/"},"author":{"name":"Great Learning Editorial Team","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad"},"headline":"For Loop in Python with Examples","datePublished":"2023-07-18T12:01:19+00:00","dateModified":"2024-08-15T16:56:22+00:00","mainEntityOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/for-loop-in-python-with-examples\/"},"wordCount":2038,"commentCount":0,"publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/for-loop-in-python-with-examples\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2023\/07\/iStock-1319504085.jpg","keywords":["python"],"articleSection":["IT\/Software Development"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.mygreatlearning.com\/blog\/for-loop-in-python-with-examples\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.mygreatlearning.com\/blog\/for-loop-in-python-with-examples\/","url":"https:\/\/www.mygreatlearning.com\/blog\/for-loop-in-python-with-examples\/","name":"For Loop in Python with Examples","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/for-loop-in-python-with-examples\/#primaryimage"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/for-loop-in-python-with-examples\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2023\/07\/iStock-1319504085.jpg","datePublished":"2023-07-18T12:01:19+00:00","dateModified":"2024-08-15T16:56:22+00:00","breadcrumb":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/for-loop-in-python-with-examples\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mygreatlearning.com\/blog\/for-loop-in-python-with-examples\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/for-loop-in-python-with-examples\/#primaryimage","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2023\/07\/iStock-1319504085.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2023\/07\/iStock-1319504085.jpg","width":747,"height":467},{"@type":"BreadcrumbList","@id":"https:\/\/www.mygreatlearning.com\/blog\/for-loop-in-python-with-examples\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/www.mygreatlearning.com\/blog\/"},{"@type":"ListItem","position":2,"name":"IT\/Software Development","item":"https:\/\/www.mygreatlearning.com\/blog\/software\/"},{"@type":"ListItem","position":3,"name":"For Loop in Python with Examples"}]},{"@type":"WebSite","@id":"https:\/\/www.mygreatlearning.com\/blog\/#website","url":"https:\/\/www.mygreatlearning.com\/blog\/","name":"Great Learning Blog","description":"Learn, Upskill &amp; Career Development Guide and Resources","publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"alternateName":"Great Learning","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mygreatlearning.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization","name":"Great Learning","url":"https:\/\/www.mygreatlearning.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","width":900,"height":900,"caption":"Great Learning"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/GreatLearningOfficial\/","https:\/\/x.com\/Great_Learning","https:\/\/www.instagram.com\/greatlearningofficial\/","https:\/\/www.linkedin.com\/school\/great-learning\/","https:\/\/in.pinterest.com\/greatlearning12\/","https:\/\/www.youtube.com\/user\/beaconelearning\/"],"description":"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.","email":"info@mygreatlearning.com","legalName":"Great Learning Education Services Pvt. Ltd","foundingDate":"2013-11-29","numberOfEmployees":{"@type":"QuantitativeValue","minValue":"1001","maxValue":"5000"}},{"@type":"Person","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad","name":"Great Learning Editorial Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","caption":"Great Learning Editorial Team"},"description":"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.","sameAs":["https:\/\/www.mygreatlearning.com\/","https:\/\/in.linkedin.com\/school\/great-learning\/","https:\/\/x.com\/https:\/\/twitter.com\/Great_Learning","https:\/\/www.youtube.com\/channel\/UCObs0kLIrDjX2LLSybqNaEA"],"award":["Best EdTech Company of the Year 2024","Education Economictimes Outstanding Education\/Edtech Solution Provider of the Year 2024","Leading E-learning Platform 2024"],"url":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"}]}},"uagb_featured_image_src":{"full":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2023\/07\/iStock-1319504085.jpg",747,467,false],"thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2023\/07\/iStock-1319504085-150x150.jpg",150,150,true],"medium":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2023\/07\/iStock-1319504085-300x188.jpg",300,188,true],"medium_large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2023\/07\/iStock-1319504085.jpg",747,467,false],"large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2023\/07\/iStock-1319504085.jpg",747,467,false],"1536x1536":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2023\/07\/iStock-1319504085.jpg",747,467,false],"2048x2048":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2023\/07\/iStock-1319504085.jpg",747,467,false],"web-stories-poster-portrait":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2023\/07\/iStock-1319504085-640x467.jpg",640,467,true],"web-stories-publisher-logo":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2023\/07\/iStock-1319504085-96x96.jpg",96,96,true],"web-stories-thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2023\/07\/iStock-1319504085-150x94.jpg",150,94,true]},"uagb_author_info":{"display_name":"Great Learning Editorial Team","author_link":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"},"uagb_comment_info":0,"uagb_excerpt":"If you've ever wondered how to efficiently repeat a task in Python, you're in the right place. In this blog, we'll explore the world of loops, with a focus on the \"for\" loop in Python. In programming, loops are a powerful tool that allow us to repeat a block of code multiple times. They provide&hellip;","_links":{"self":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/91347","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/comments?post=91347"}],"version-history":[{"count":13,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/91347\/revisions"}],"predecessor-version":[{"id":104898,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/91347\/revisions\/104898"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media\/91359"}],"wp:attachment":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media?parent=91347"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/categories?post=91347"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/tags?post=91347"},{"taxonomy":"content_type","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/content_type?post=91347"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}