{"id":3492,"date":"2023-08-01T09:54:59","date_gmt":"2023-08-01T09:54:59","guid":{"rendered":"https:\/\/www.copahost.com\/blog\/?p=3492"},"modified":"2023-08-05T15:59:41","modified_gmt":"2023-08-05T15:59:41","slug":"for-python","status":"publish","type":"post","link":"https:\/\/www.copahost.com\/blog\/for-python\/","title":{"rendered":"For in Python: complete guide with 18 examples"},"content":{"rendered":"\n<p>The function for in python fundamental control structures. It is very useful for <strong>iterating over objects such as lists, tuples, dictionaries and sets<\/strong>, executing a block of code for each element in the sequence. With the for function, you can efficiently process sequences of data and perform operations on each element of a <a href=\"https:\/\/www.copahost.com\/blog\/list-python\/\">list<\/a>, tuple, or dictionary. It is a powerful tool for object-oriented programming, allowing you to run reusable code for each element of a sequence.<\/p>\n\n\n\n<p><strong>We&#8217;ll explore Python&#8217;s for loop<\/strong>&nbsp;syntax&nbsp;, how to use the for() function, looping through lists, tuples, and dictionaries, nested loops, using break and continue, understanding lists with the for loop, and examples of using the for loop in Python .&nbsp;By the end of this article, you&#8217;ll be better equipped to use the for function in your next Python project, and better equipped to understand how to use this tool to make your code more efficient and readable.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_69_1 ez-toc-wrap-center counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.copahost.com\/blog\/for-python\/#Basic_for_loop_syntax\" title=\"Basic for loop syntax\">Basic for loop syntax<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.copahost.com\/blog\/for-python\/#Examples_of_using_the_for_function_in_Python\" title=\"Examples of using the for function in Python\">Examples of using the for function in Python<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.copahost.com\/blog\/for-python\/#Example_1_Iterating_over_a_list\" title=\"Example 1: Iterating over a list\">Example 1: Iterating over a list<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.copahost.com\/blog\/for-python\/#Example_2_Iterating_over_a_string\" title=\"Example 2: Iterating over a string\">Example 2: Iterating over a string<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.copahost.com\/blog\/for-python\/#Example_3_Iterating_over_a_numeric_range\" title=\"Example 3: Iterating over a numeric range\">Example 3: Iterating over a numeric range<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.copahost.com\/blog\/for-python\/#Using_the_range_function_in_a_for_loop\" title=\"Using the range() function in a for loop\">Using the range() function in a for loop<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.copahost.com\/blog\/for-python\/#For_function_in_python_with_lists_tuples_and_dictionaries\" title=\"For function in python with lists, tuples and dictionaries\">For function in python with lists, tuples and dictionaries<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.copahost.com\/blog\/for-python\/#Nested_for_function_in_python\" title=\"Nested for function in python\">Nested for function in python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.copahost.com\/blog\/for-python\/#Using_the_break_and_continue_statement_in_a_for_loop\" title=\"Using the break and continue statement in a for loop\">Using the break and continue statement in a for loop<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.copahost.com\/blog\/for-python\/#List_comprehension_with_for_function_in_python\" title=\"List comprehension with for function in python\">List comprehension with for function in python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.copahost.com\/blog\/for-python\/#while_loop_vs_for_loop_in_python\" title=\"while loop vs.&nbsp;for loop in python\">while loop vs.&nbsp;for loop in python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/www.copahost.com\/blog\/for-python\/#Recommendations_for_using_the_for_function_in_Python\" title=\"Recommendations for using the for function in Python\">Recommendations for using the for function in Python<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Basic_for_loop_syntax\"><\/span>Basic for loop syntax<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The basic Python for loop syntax lets you iterate over a sequence of elements, such as a list, tuple, set, or string.&nbsp;The for loop is extremely versatile and efficient for iteratively traversing elements in a data structure.&nbsp;The basic structure of the for loop in Python is as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for element in sequence:\n # do something with the element<\/code><\/pre>\n\n\n\n<p>Where \u201celement\u201d is a variable that assumes the value of each element of the sequence at each iteration.&nbsp;\u201csequence\u201d represents the data structure that the for loop will iterate through.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Examples_of_using_the_for_function_in_Python\"><\/span>Examples of using the for function in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Through the use of statements like else and break, it is also possible to add additional logic to the loop, making it even more flexible and powerful.&nbsp;Let&#8217;s look at some examples to better understand how the for loop works in Python:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Example_1_Iterating_over_a_list\"><\/span>Example 1: Iterating over a list<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>In this example, the for loop loops through each element of the list \u201cfruits\u201d and assigns the value of each element to the variable \u201cfruit\u201d.&nbsp;Next, we print the \u201cfruit\u201d variable to the screen.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fruits= &#091;'apple', 'banana', 'orange']\nfor fruit in fruits:\n print(fruit)<\/code><\/pre>\n\n\n\n<p>The output will be:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>apple\nbanana\norange<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Example_2_Iterating_over_a_string\"><\/span>Example 2: Iterating over a string<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>In this case, the for loop iterates over each character of the string \u201cmessage\u201d and prints each character to the screen.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>message = \"Hello, world!\" for character in message:\nprint(character)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Example_3_Iterating_over_a_numeric_range\"><\/span>Example 3: Iterating over a numeric range<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>We use the range() function to generate a sequence of numbers.&nbsp;In this example, the for loop iterates over the numbers 1 through 5 and prints each number to the screen.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">for i in range(1, 6):\n print(i)<\/pre>\n\n\n\n<p>After the normal Python for loop finishes, when all iterations are done, we execute the else statement.&nbsp;This example prints the numbers 0 through 4 and then prints the message \u201cLoop complete!\u201d.&nbsp;Look:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for i in range(5):\n print(i)\nelse:\n print(\"Tie completed!\")<\/code><\/pre>\n\n\n\n<p>The \u201cbreak\u201d instruction is an efficient way to avoid unnecessary iterations in situations where we modify the iteration exit condition at some point in the loop.&nbsp;In this example, the for loop loops through the list \u201cfruits\u201d and prints each element until we find the word \u201cbanana\u201d.&nbsp;At this point, we break the loop.&nbsp;Look:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>frutas = &#091;'apple', 'banana', 'orange']\nfor fruit in fruits:\n if fruit == 'banana':\n break\n print(fruit)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Using_the_range_function_in_a_for_loop\"><\/span>Using the range() function in a for loop<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The range() function in Python is a convenient way to generate a sequence of numbers in a for loop.<\/p>\n\n\n\n<p>The basic syntax of the range() function is range(start, stop, step), where \u201cstart\u201d is the starting number of the sequence, \u201cstop\u201d is the ending number (not included) and \u201cstep\u201d is the increment between the numbers of the sequence.<\/p>\n\n\n\n<p>For example, if we want to iterate over the numbers 1 to 10 with an increment of 2, we can use the range(1, 10, 2) function.&nbsp;Let&#8217;s look at an example in which the for loop iterates over the sequence generated by the range(1, 10, 2) function and prints each number to the screen:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for i in range(1, 10, 2):\n print(i)<\/code><\/pre>\n\n\n\n<p>The output will be:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1\n3\n5\n7\n9<\/code><\/pre>\n\n\n\n<p>We can omit the value of \u201cstart\u201d by using only the \u201cstop\u201d argument in the range() function.&nbsp;In this case, the value of \u201cstart\u201d is being considered as 0 by default.&nbsp;For example, range(5) generates the sequence of numbers from 0 to 4. If we omit the value of \u201cstep\u201d, the increment is assumed to be 1. For example, range(1, 5) generates the sequence of numbers from 1 to 4.<\/p>\n\n\n\n<p>In other words, the range() function plays a key role when working with for loops in Python.&nbsp;In addition, it offers a more practical and efficient approach to generating numerical sequences.&nbsp;With the range() function, we can define the start, end and step of the sequence, allowing precise control over the iterations of the loop.&nbsp;In this way, this function becomes a powerful ally to traverse a specific range of values \u200b\u200bin a simplified and optimized way.&nbsp;We can combine the range() function with other control structures, such as conditionals, to create custom iterations and perform specific tasks for each element of the generated sequence.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"For_function_in_python_with_lists_tuples_and_dictionaries\"><\/span>For function in python with lists, tuples and dictionaries<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The for loop in Python is especially useful when used with data structures like lists, tuples, and dictionaries.&nbsp;It allows you to go through the elements of these structures and perform some action for each element.&nbsp;Let&#8217;s explore how we can use the for loop in each of these situations:<\/p>\n\n\n\n<p>The for loop in Python is especially useful when used with data structures like lists, tuples, and dictionaries.&nbsp;It allows you to go through the elements of these structures and perform some action for each element.&nbsp;Let&#8217;s explore how we can use the for loop in each of these situations:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">For loop with lists:<\/h4>\n\n\n\n<p>Lists are data structures that can store multiple elements in a single variable.&nbsp;We can iterate through the elements of a python list&nbsp;using the for loop, as exemplified in this example.&nbsp;The for loop iterates over each element of the list \u201cfruits\u201d and, at each iteration, we assign the value of the element to the variable \u201cfruit\u201d.&nbsp;Next, we print the value of the \u201cfruit\u201d variable on the screen.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>frutas = &#091;'apple', 'banana', 'orange']\nfor fruit in fruits:\n print(fruit)<\/code><\/pre>\n\n\n\n<p>The output will be:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>apple\nbanana\norange<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">For loop with tuples:<\/h4>\n\n\n\n<p>A tuple is a list-like data structure.<\/p>\n\n\n\n<p>The for loop is a control structure that can be used to iterate over the elements of a collection of objects, such as lists, tuples, or sets.&nbsp;In our example, we are using the for loop to iterate over the elements of the \u201ccoordinates\u201d tuple and, with each iteration, assign the value of the element to the \u201ccoordinate\u201d variable.<\/p>\n\n\n\n<p>A constant is a variable that cannot change its value after it is created.&nbsp;It keeps the same value permanently and cannot be modified.&nbsp;In other words, once created, the constant remains unchanged.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>coordinates = (10, 20, 30)\nfor coordinate in coordinates:\n print(coordinate)<\/code><\/pre>\n\n\n\n<p>The output will be:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>10\n20\n30<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">for loop with dictionaries:<\/h4>\n\n\n\n<p>Dictionaries are data structures that allow you to store and retrieve information using key-value pairs.&nbsp;We can loop through the elements of a dictionary using the for loop, as exemplified here.&nbsp;We use the items() method to get a list of key-value pairs from the \u201cperson\u201d dictionary.&nbsp;The for loop iterates over each key-value pair, and at each iteration we assign the key value to the \u201ckey\u201d variable and the corresponding value to the \u201cvalue\u201d variable.&nbsp;We then print the \u201ckey\u201d and \u201cvalue\u201d values \u200b\u200bto the screen.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>person = {'name': 'John', 'age': 25, 'city': 'S\u00e3o Paulo'}\nfor key, value in person.items():\n print(key, value)<\/code><\/pre>\n\n\n\n<p>The output will be:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>name John\nage 25\ncity S\u00e3o Paulo<\/code><\/pre>\n\n\n\n<p>The for loop is a powerful tool for iterating over the elements of lists, tuples, and dictionaries in Python.&nbsp;With this functionality, we can conveniently and efficiently access and manipulate the elements of these structures.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Nested_for_function_in_python\"><\/span>Nested for function in python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The nested for loop (or \u201czero-length nested for\u201d) is a feature of Python that allows you to run a block of code without a specific loop, using just a for statement.<\/p>\n\n\n\n<p>Suppose you want to check whether a number is prime using a nested for loop.&nbsp;You can do this as shown in this example.&nbsp;The variable ia is incremented each iteration of the loop.&nbsp;However, the loop exits when the number is divisible by the value of i.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>number = 17\n\nfor i in range(2, number):\n    if number % i == 0:\n        break\n\nprint(\"The number\", number, \"is cousin.\")<\/code><\/pre>\n\n\n\n<p>If you want to run the same block of code without using a loop, you can do that using the for statement without an argument.&nbsp;Therefore, we will create an empty loop in which while, no further actions will be performed, but the code block inside the loop will be executed only once:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>number = 17\n\nfor i in ():\n    if number % i == 0:\n        break\nprint(\"The number\", number, \"is cousin.\")<\/code><\/pre>\n\n\n\n<p>In short, the nested for loop in Python is a powerful feature that lets you execute a block of code efficiently and concisely without the need for a specific loop.&nbsp;This functionality offers flexibility and simplicity when dealing with complex iteration tasks in Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Using_the_break_and_continue_statement_in_a_for_loop\"><\/span>Using the break and continue statement in a for loop<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>We commonly use break and continue statements in for loops in Python to control loop behavior.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Break<\/h4>\n\n\n\n<p>We use the break statement to exit a for loop before it terminates normally.&nbsp;When we execute the break statement, we break the loop and the program continues its execution after the loop.&nbsp;For example, the for loop iterates from 0 to 9, but when i equals 5, we execute the break statement and break the loop.&nbsp;We ignore values \u200b\u200bof i that are odd:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for i in range(10):\n    if i == 5:\n        break\n    print(i)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Continue<\/h4>\n\n\n\n<p>We use the continue statement to skip a step in a for loop and continue with the next iterator.&nbsp;When we execute the continue statement, the for loop ignores the current value of i and advances to the next iterator.&nbsp;For example, the for loop iterates from 0 to 9, but when i is an even number, we execute the continue statement and ignore the value of i.&nbsp;We only print the values \u200b\u200bof i that are odd:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for i in range(10):\n    if i % 2 == 0:\n        continue\n    print(i) <\/code><\/pre>\n\n\n\n<p>In Python, the use of loops is common and in certain cases, it is necessary to use break and continue statements to control the behavior of the loop.&nbsp;We use the break statement to break the loop before it ends normally.&nbsp;On the other hand, we use the continue statement to skip a step in the loop and continue with the next iterator.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"List_comprehension_with_for_function_in_python\"><\/span>List comprehension with for function in python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>List comprehension is a concise way to create a list from an existing list or other iterable sequence.&nbsp;It is created using the [expression for each element of the sequence] syntax, where the expression is executed for each element of the sequence and the result is added to the list.<\/p>\n\n\n\n<p>For example, if you want to create a list of even numbers from the sequence of numbers 0 to 9, you can use list comprehension.&nbsp;In this example, we run the expression i for i in range(10) if i % 2 == 0 for each element of the sequence range(10), and add the even numbers to the list.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pair_numbers = &#091;i for i in range(10) if i % 2 == 0]<\/code><\/pre>\n\n\n\n<p>We use Python list comprehension to create more complex lists.&nbsp;We can combine list comprehension with the filter function to create more complex lists from existing data.&nbsp;List comprehension is a concise way of working with lists in Python.&nbsp;In this example, we use the filter function to filter only even numbers from the list generated by the for loop.&nbsp;We use the lambda lambda function x: x % 2 == 0 to check whether each element is even or odd.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pair_number = list(filter(lambda x: x % 2 == 0, range(10)))<\/code><\/pre>\n\n\n\n<p>List comprehension is a concise way to create a list from an existing list or other iterable sequence.&nbsp;The list comprehension function is a powerful way to work with lists in Python.&nbsp;We can combine it with other functions like filter to create more complex lists from existing data.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"while_loop_vs_for_loop_in_python\"><\/span>while loop vs.&nbsp;for loop in python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>You can use a for loop in Python to loop through a list of specific numbers or values.&nbsp;For example we have this code will print the numbers 0 to 4:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for i in range(5):\n    print(i)<\/code><\/pre>\n\n\n\n<p>The while loop is an important control structure in programming, as it allows you to iterate over a list indefinitely while satisfying a specific condition, regardless of the length of the list.&nbsp;For example, here in this code, we have the numbers 0 to 9 printed:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>i = 0\nwhile i &lt; 10:\n    print(i)\n    i += 1<\/code><\/pre>\n\n\n\n<p>We use the for loop to iterate over an array or sequence of known values, while we use the while to execute a task in an unknown number of iterations, checking a condition in each iteration.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Recommendations_for_using_the_for_function_in_Python\"><\/span>Recommendations for using the for function in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Here are some recommendations for using the for loop in Python:<br><br>1- Use the for loop when you need to iterate over an array of numbers or specific values.&nbsp;The for loop allows you to specify the start and end of the track, which can make your code more layman and maintainable.<br>2- Use the for loop when you need to do operations with each value in the range.<br>3- Use the for loop when you need to do operations with each value in the range and save the result to a new variable.<br>4- Use the for loop when you need to do operations with each value in the range and save the result to a new file.<br>5- Use the for loop when you need to do operations with each value in the range and save the result to a new database.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">CONCLUSION<\/h4>\n\n\n\n<p>The for function in Python is a powerful and versatile tool for solving problems involving iteration over sequences of data, such as lists, tuples and strings.&nbsp;With the for function, it is possible to perform unidirectional, bidirectional and negative indices iterations, in addition to controlling the iteration output condition.<br>By learning and practicing using the for function, you can become more efficient and effective in your work with Python programming, solving problems faster and more elegantly.<br>Always remember that practice is key to understanding and applying Python functions and features.&nbsp;As such, we strongly recommend that you experiment with different situations and exercises with the for function.&nbsp;Therefore, this will allow you to gain even more familiarity, confidence and skill in using this tool.<\/p>\n\n\n\n<p><br>Ultimately, therefore, it is crucial not to forget to look for additional resources to constantly improve your knowledge of Python programming.&nbsp;Furthermore, it is highly recommended to explore a variety of resources such as tutorials, books and programming communities in order to broaden your expertise in the language.&nbsp;With dedication and practice, you&#8217;ll be ready to tackle the most complex Python programming challenges.&nbsp;Besides the &#8216;for&#8217; function, another important function in Python is the <a href=\"https:\/\/www.copahost.com\/blog\/len-python\/\">&#8216;len&#8217; function<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The function for in python fundamental control structures. It is very useful for iterating over objects such as lists, tuples, dictionaries and sets, executing a block of code for each element in the sequence. With the for function, you can efficiently process sequences of data and perform operations on each element of a list, tuple, [&hellip;]<\/p>\n","protected":false},"author":17,"featured_media":3509,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[174],"tags":[],"class_list":["post-3492","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v23.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>For in Python: complete guide with 18 examples - Copahost<\/title>\n<meta name=\"description\" content=\"Learn how to use the for function in Python with this comprehensive guide. Practical examples and advantages\/limitations explained.\" \/>\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.copahost.com\/blog\/for-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"For in Python: complete guide with 18 examples - Copahost\" \/>\n<meta property=\"og:description\" content=\"Learn how to use the for function in Python with this comprehensive guide. Practical examples and advantages\/limitations explained.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.copahost.com\/blog\/for-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Copahost\" \/>\n<meta property=\"article:published_time\" content=\"2023-08-01T09:54:59+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-08-05T15:59:41+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2023\/07\/for-em-python-1.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1080\" \/>\n\t<meta property=\"og:image:height\" content=\"1080\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Schenia T\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Schenia T\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"11 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.copahost.com\/blog\/for-python\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/for-python\/\"},\"author\":{\"name\":\"Schenia T\",\"@id\":\"https:\/\/www.copahost.com\/blog\/#\/schema\/person\/2efb96f9dfaf6162f347abcd06b1429f\"},\"headline\":\"For in Python: complete guide with 18 examples\",\"datePublished\":\"2023-08-01T09:54:59+00:00\",\"dateModified\":\"2023-08-05T15:59:41+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/for-python\/\"},\"wordCount\":2385,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/for-python\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2023\/07\/for-em-python-1.png\",\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.copahost.com\/blog\/for-python\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.copahost.com\/blog\/for-python\/\",\"url\":\"https:\/\/www.copahost.com\/blog\/for-python\/\",\"name\":\"For in Python: complete guide with 18 examples - Copahost\",\"isPartOf\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/for-python\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/for-python\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2023\/07\/for-em-python-1.png\",\"datePublished\":\"2023-08-01T09:54:59+00:00\",\"dateModified\":\"2023-08-05T15:59:41+00:00\",\"description\":\"Learn how to use the for function in Python with this comprehensive guide. Practical examples and advantages\/limitations explained.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/for-python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.copahost.com\/blog\/for-python\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.copahost.com\/blog\/for-python\/#primaryimage\",\"url\":\"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2023\/07\/for-em-python-1.png\",\"contentUrl\":\"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2023\/07\/for-em-python-1.png\",\"width\":1080,\"height\":1080,\"caption\":\"for in python\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.copahost.com\/blog\/for-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.copahost.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"For in Python: complete guide with 18 examples\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.copahost.com\/blog\/#website\",\"url\":\"https:\/\/www.copahost.com\/blog\/\",\"name\":\"Copahost\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.copahost.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.copahost.com\/blog\/#organization\",\"name\":\"Copahost\",\"url\":\"https:\/\/www.copahost.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.copahost.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2016\/03\/copahostlogo.png\",\"contentUrl\":\"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2016\/03\/copahostlogo.png\",\"width\":223,\"height\":40,\"caption\":\"Copahost\"},\"image\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.copahost.com\/blog\/#\/schema\/person\/2efb96f9dfaf6162f347abcd06b1429f\",\"name\":\"Schenia T\",\"description\":\"Data scientist, passionate about technology tools and games. Undergraduate student in Statistics at UFPB. Her hobby is binge-watching series, enjoying good music working or cooking, going to the movies and learning new things!\",\"url\":\"https:\/\/www.copahost.com\/blog\/author\/schenia\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"For in Python: complete guide with 18 examples - Copahost","description":"Learn how to use the for function in Python with this comprehensive guide. Practical examples and advantages\/limitations explained.","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.copahost.com\/blog\/for-python\/","og_locale":"en_US","og_type":"article","og_title":"For in Python: complete guide with 18 examples - Copahost","og_description":"Learn how to use the for function in Python with this comprehensive guide. Practical examples and advantages\/limitations explained.","og_url":"https:\/\/www.copahost.com\/blog\/for-python\/","og_site_name":"Copahost","article_published_time":"2023-08-01T09:54:59+00:00","article_modified_time":"2023-08-05T15:59:41+00:00","og_image":[{"width":1080,"height":1080,"url":"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2023\/07\/for-em-python-1.png","type":"image\/png"}],"author":"Schenia T","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Schenia T","Est. reading time":"11 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.copahost.com\/blog\/for-python\/#article","isPartOf":{"@id":"https:\/\/www.copahost.com\/blog\/for-python\/"},"author":{"name":"Schenia T","@id":"https:\/\/www.copahost.com\/blog\/#\/schema\/person\/2efb96f9dfaf6162f347abcd06b1429f"},"headline":"For in Python: complete guide with 18 examples","datePublished":"2023-08-01T09:54:59+00:00","dateModified":"2023-08-05T15:59:41+00:00","mainEntityOfPage":{"@id":"https:\/\/www.copahost.com\/blog\/for-python\/"},"wordCount":2385,"commentCount":0,"publisher":{"@id":"https:\/\/www.copahost.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.copahost.com\/blog\/for-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2023\/07\/for-em-python-1.png","articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.copahost.com\/blog\/for-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.copahost.com\/blog\/for-python\/","url":"https:\/\/www.copahost.com\/blog\/for-python\/","name":"For in Python: complete guide with 18 examples - Copahost","isPartOf":{"@id":"https:\/\/www.copahost.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.copahost.com\/blog\/for-python\/#primaryimage"},"image":{"@id":"https:\/\/www.copahost.com\/blog\/for-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2023\/07\/for-em-python-1.png","datePublished":"2023-08-01T09:54:59+00:00","dateModified":"2023-08-05T15:59:41+00:00","description":"Learn how to use the for function in Python with this comprehensive guide. Practical examples and advantages\/limitations explained.","breadcrumb":{"@id":"https:\/\/www.copahost.com\/blog\/for-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.copahost.com\/blog\/for-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.copahost.com\/blog\/for-python\/#primaryimage","url":"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2023\/07\/for-em-python-1.png","contentUrl":"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2023\/07\/for-em-python-1.png","width":1080,"height":1080,"caption":"for in python"},{"@type":"BreadcrumbList","@id":"https:\/\/www.copahost.com\/blog\/for-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.copahost.com\/blog\/"},{"@type":"ListItem","position":2,"name":"For in Python: complete guide with 18 examples"}]},{"@type":"WebSite","@id":"https:\/\/www.copahost.com\/blog\/#website","url":"https:\/\/www.copahost.com\/blog\/","name":"Copahost","description":"","publisher":{"@id":"https:\/\/www.copahost.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.copahost.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.copahost.com\/blog\/#organization","name":"Copahost","url":"https:\/\/www.copahost.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.copahost.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2016\/03\/copahostlogo.png","contentUrl":"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2016\/03\/copahostlogo.png","width":223,"height":40,"caption":"Copahost"},"image":{"@id":"https:\/\/www.copahost.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.copahost.com\/blog\/#\/schema\/person\/2efb96f9dfaf6162f347abcd06b1429f","name":"Schenia T","description":"Data scientist, passionate about technology tools and games. Undergraduate student in Statistics at UFPB. Her hobby is binge-watching series, enjoying good music working or cooking, going to the movies and learning new things!","url":"https:\/\/www.copahost.com\/blog\/author\/schenia\/"}]}},"_links":{"self":[{"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/posts\/3492","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/users\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/comments?post=3492"}],"version-history":[{"count":19,"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/posts\/3492\/revisions"}],"predecessor-version":[{"id":3604,"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/posts\/3492\/revisions\/3604"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/media\/3509"}],"wp:attachment":[{"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/media?parent=3492"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/categories?post=3492"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/tags?post=3492"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}