{"id":2949,"date":"2022-06-17T14:10:54","date_gmt":"2022-06-17T13:10:54","guid":{"rendered":"https:\/\/hub.salford.ac.uk\/psytech\/?page_id=2949"},"modified":"2022-07-19T10:37:55","modified_gmt":"2022-07-19T09:37:55","slug":"loops-python","status":"publish","type":"page","link":"https:\/\/hub.salford.ac.uk\/psytech\/python\/loops-python\/","title":{"rendered":"Loops &#8211; Python"},"content":{"rendered":"<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/hub.salford.ac.uk\/psytech\/wp-content\/uploads\/sites\/95\/2022\/06\/image-11-1024x1024.png\" alt=\"\" class=\"wp-image-2880\" width=\"247\" height=\"247\" srcset=\"https:\/\/hub.salford.ac.uk\/psytech\/wp-content\/uploads\/sites\/95\/2022\/06\/image-11-1024x1024.png 1024w, https:\/\/hub.salford.ac.uk\/psytech\/wp-content\/uploads\/sites\/95\/2022\/06\/image-11-300x300.png 300w, https:\/\/hub.salford.ac.uk\/psytech\/wp-content\/uploads\/sites\/95\/2022\/06\/image-11-150x150.png 150w, https:\/\/hub.salford.ac.uk\/psytech\/wp-content\/uploads\/sites\/95\/2022\/06\/image-11-768x768.png 768w, https:\/\/hub.salford.ac.uk\/psytech\/wp-content\/uploads\/sites\/95\/2022\/06\/image-11.png 1200w\" sizes=\"auto, (max-width: 247px) 100vw, 247px\" \/><\/figure>\n<\/div>\n\n\n<p>Loops allow up to repeat code and iterate over large data sets. The latter, we&#8217;ll be getting more into in the next session. Loops come in two varieties, you have <strong>while loops<\/strong> and <strong>for loops<\/strong>. While loops will run the code while a condition is met. For loops will run the code for a specified number of times.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">While Loops<\/h2>\n\n\n\n<p>We&#8217;re going to create some code that allows the user to guess a number between 1 &#8211; 10, however, the user only has 3 guesses. Let&#8217;s run the code below to start off. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>i=1 #usually in loops the letter i is used, i stands for iterate (or index!)\nwhile i &lt;= 3: # What we're saying here is run this code while i is less than or \n              # equal to three - this is our three guesses\n              # note the syntax! while is a keyword is python!\n    print(\"Guess a number between 1 and 10 using the number pad:\")\n    guess = int(input())<\/code><\/pre>\n\n\n\n<p>What happened when you ran the code? You&#8217;ll have found it would continuously loop indefinitely. This is because, in our loop, we&#8217;re not increasing the value of i by 1. Go ahead and see if you can solve this problem.<\/p>\n\n\n\n<div class=\"wp-block-pb-accordion-item c-accordion__item js-accordion-item no-js\" data-initially-open=\"false\" data-click-to-close=\"true\" data-auto-close=\"true\" data-scroll=\"false\" data-scroll-offset=\"0\"><h3 id=\"at-29490\" class=\"c-accordion__title js-accordion-controller\" role=\"button\">Click to reveal \u2193<\/h3><div id=\"ac-29490\" class=\"c-accordion__content\">\n<p>Don&#8217;t worry if your answer is slightly different, there are many ways to achieve the same thing!<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>i=1\nwhile i &lt;= 3:\n    print(\"Guess a number between 1 and 10 using the number pad:\")\n    guess = int(input())\n    i += 1 #Adding this line means we now get three guesses, woo!<\/code><\/pre>\n<\/div><\/div>\n\n\n\n<p>Great, we have three guesses in our program now. Next, we need to get the program to generate a number between 1 and 10 then tell us if we&#8217;re right and what the right number is if we don&#8217;t guess it. We&#8217;ll start off by randomly generating a number.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import random #Base python doesn't have everything we need, at times we need \n              #to import packages, that have useful things for us to use. \n              # We'll be going over packages in future lessons.\n              # For now just know they exisit.\n\nnumber = random.randint(1,10) #the random.randint() is from the random that we \n                              # imported above. All this is doing is \n                              #generating a number between 1 and 10\ni=1\n\nprint(\"The correct answer is \", number)\n\nwhile i &lt;= 3:\n    print(\"Guess a number between 1 and 10 using the number pad:\")\n    guess = int(input())\n    i += 1\n\n<\/code><\/pre>\n\n\n\n<p>Now the program is generating a number. For testing purposes, I&#8217;ve added the program to print the correct answer before we guess so we can test it. Next, we need the program to tell the user if their guess is right or wrong. For you to do! I&#8217;d like you to add an if statement to the code above to tell the user if they&#8217;ve guessed correctly.<\/p>\n\n\n\n<div class=\"wp-block-pb-accordion-item c-accordion__item js-accordion-item no-js\" data-initially-open=\"false\" data-click-to-close=\"true\" data-auto-close=\"true\" data-scroll=\"false\" data-scroll-offset=\"0\"><h3 id=\"at-29491\" class=\"c-accordion__title js-accordion-controller\" role=\"button\">Click to reveal \u2193<\/h3><div id=\"ac-29491\" class=\"c-accordion__content\">\n<p>Don&#8217;t worry if your answer is slightly different, there are many ways to achieve the same thing!<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import random \n\nnumber = random.randint(1,10) \ni=1\n\nprint(\"The correct answer is \", number)\n\nwhile i &lt;= 3:\n    print(\"Guess a number between 1 and 10 using the number pad:\")\n    guess = int(input())\n    if guess == number:\n        print(\"You are right!\")\n    i += 1\n<\/code><\/pre>\n<\/div><\/div>\n\n\n\n<p>You may notice the program still gives us all our guesses even if we guess the number correctly the first time (which for now we can since the program tells us the correct answer). We&#8217;re going to get the program to exit out of the loop early when the correct answer is given. To do this we&#8217;re going to use the <strong>break statement<\/strong>. Our program now looks like this.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import random \n\nnumber = random.randint(1,10) \ni=1\n\nprint(\"this is the answer \", number)\n\nwhile i &lt;= 3:\n    print(\"Guess a number between 1 and 10 using the number pad:\")\n    guess = int(input())\n    if guess == number: # Note that this if statement is nested in the while loop\n        print(\"You are right!\")\n        break\n    i += 1\n<\/code><\/pre>\n\n\n\n<p>Give it a try. The program should stop if you guess the right number. In addition, to <strong>break statements<\/strong> that stop the program if the conditions are met, we also can <strong>continue statements<\/strong> that continue if the conditions are met. We&#8217;re not using <strong>continue statements<\/strong> in this example but they are useful tools to remember!<\/p>\n\n\n\n<p>Now I&#8217;d like you to add an <strong>elif statement<\/strong> so the program will tell the user when an incorrect number is guessed and how many guesses they have remaining.<\/p>\n\n\n\n<div class=\"wp-block-pb-accordion-item c-accordion__item js-accordion-item no-js\" data-initially-open=\"false\" data-click-to-close=\"true\" data-auto-close=\"true\" data-scroll=\"false\" data-scroll-offset=\"0\"><h3 id=\"at-29492\" class=\"c-accordion__title js-accordion-controller\" role=\"button\">Click to reveal \u2193<\/h3><div id=\"ac-29492\" class=\"c-accordion__content\">\n<p>Don&#8217;t worry if your answer is slightly different, there are many ways to achieve the same thing!<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import random \n\nnumber = random.randint(1,10) \ni=1\n\nprint(\"this is the answer \", number)\n\nwhile i &lt;= 3:\n    print(\"Guess a number between 1 and 10 using the number pad:\")\n    guess = int(input())\n    if guess == number:\n        print(\"You are right!\")\n        break\n    elif guess != number:\n        remaining = 3 - i\n        print(\"Incorrect! You have \", remaining, \" attempts remaining.\")\n    i += 1<\/code><\/pre>\n<\/div><\/div>\n\n\n\n<p>The last part of our program is to tell the user what they number is if they guessed it wrong with their 3 guesses! To do this, we use an else statement that is in line with the while keyword. Give it a go!<\/p>\n\n\n\n<div class=\"wp-block-pb-accordion-item c-accordion__item js-accordion-item no-js\" data-initially-open=\"false\" data-click-to-close=\"true\" data-auto-close=\"true\" data-scroll=\"false\" data-scroll-offset=\"0\"><h3 id=\"at-29493\" class=\"c-accordion__title js-accordion-controller\" role=\"button\">Click to reveal \u2193<\/h3><div id=\"ac-29493\" class=\"c-accordion__content\">\n<p>Don&#8217;t worry if your answer is slightly different, there are many ways to achieve the same thing!<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import random \n\nnumber = random.randint(1,10) \ni=1\n\n# print(\"this is the answer \", number) # this line of code is commented out \n\n                                       # we only needed to print the answer\n                                       # when we were testing\n\nwhile i &lt;= 3:\n    print(\"Guess a number between 1 and 10 using the number pad:\")\n    guess = int(input())\n    if guess == number:\n        print(\"You are right!\")\n        break\n    elif guess != number:\n        remaining = 3 - i\n        print(\"Incorrect! You have \", remaining, \" attempts remaining.\")\n    i += 1\nelse: \n    print(\"You've run out of attempts, the correct number was\", number)\n<\/code><\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">For Loops<\/h2>\n\n\n\n<p>Let&#8217;s move onto for loops that run for a number of iterations. The syntax is the same as while loops, we just replace while with for. In our for loop, we&#8217;re going to be using the <strong>range() function<\/strong>, which allows us to specify the number of times the code will run. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for i in range(10):\n  print(i)<\/code><\/pre>\n\n\n\n<p>Note how we don&#8217;t have to specify a value for i when we&#8217;re using <strong>for loops<\/strong>. Although when you run the code, notice how the program prints the numbers 0 through to 9. This is because python uses <strong>zero-indexing<\/strong>. The very first time the <strong>for loop <\/strong>runs, python will assume i = 0 unless you specify otherwise. Using the range() function we can specify we want the numbers 1 to 10 by writing range(1,11). The program needs range(1,11) by standing at 1 and stopping at 11, therefore 11 is not included. Give it a try using the code below.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for i in range(1, 11):\n  print(i)<\/code><\/pre>\n\n\n\n<p>By default, python will increment the value i but 1. You can change this by adding a third <strong>argument <\/strong>to the <strong>range()<\/strong> function. We will be covering arguments, for now just know it&#8217;s the values we place in the parentheses of the function. To change the increments from the default of 1 to your specified number, I&#8217;m going to use 2, try the following code.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for i in range(1, 11, 2):\n  print(i)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Session Challenge!<\/h3>\n\n\n\n<p>I&#8217;d like you to create a program that simulates a coin toss. Your program will ask the user how many times they would like to toss a coin. Then simulate a coin toss as many times as the user requested. To finish with, the program will tell the users how tosses they were in addition to how many heads and how many tails. You can use the code below to get started.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import random\n\nprint(\"How many times would you like to toss a coin?\")\n\nfor i in range(#add here):\n    flip = random.randint(0, 1) #We're getting the code to generate randomly either a 0 or a 1. One value will represent heads the other value will represent tails. \n    #hint: you'll need to use if statements         <\/code><\/pre>\n\n\n\n<div class=\"wp-block-pb-accordion-item c-accordion__item js-accordion-item no-js\" data-initially-open=\"false\" data-click-to-close=\"true\" data-auto-close=\"true\" data-scroll=\"false\" data-scroll-offset=\"0\"><h3 id=\"at-29494\" class=\"c-accordion__title js-accordion-controller\" role=\"button\">Click to reveal \u2193<\/h3><div id=\"ac-29494\" class=\"c-accordion__content\">\n<p>This time I&#8217;ve provided two answers depending on how you&#8217;ve approached the range() function. As always, don&#8217;t worry if your answer is slightly different, there are many ways to achieve the same thing!<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import random\n\nprint(\"How many times would you like to toss a coin?\")\n\ntosses = int(input()) #Taking the amount of times you user would like to toss the coin\nheads = 0 #we're going to use these varibles to act as a counter for how many\ntails = 0 # heads and how many tails.\n\nfor i in range(tosses):\n    flip = random.randint(0, 1) # We're getting the code to generate \n                                # randomly either a 0 or a 1. \n    if flip == 0: # in my code if the code randomly generates a 0, then we're considering that a heads\n        print(\"Heads!\")# if this condition is met then we're prints heads to show what side of the coin\n        heads += 1  # and we're adding one to our heads variable\n    else: #if you wanted you could of wrote elif flip == 1: however, since this is a binary else works as well \n        print(\"Tails!\") #tells the user a tails was flipped\n        tails += 1 #adds 1 to the tails counter\n        \n        \nprint(\"The was a total of \", tosses, \" tosses.\", heads, \"heads and \", tails, \"tails.\") <\/code><\/pre>\n\n\n\n<p>Alternate answer if you chose to specify the range.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import random\n\nprint(\"How many times would you like to toss a coin?\")\ntosses = int(input())+1 #We add one since we want to include \n                        #the value the user specifies\n\nheads = 0 #we're going to use these varibles to act as a counter \ntails = 0 # for how many heads and how many tails.\n\nfor i in range(1, tosses):\n    flip = random.randint(0, 1) #We're getting the code to generate \n                                # randomly either a 0 or a 1. \n    if flip == 0: # in my code if the code randomly generates a 0, \n                  # then we're considering that a heads\n        print(\"Heads!\") # if this condition is met then we're prints \n                        # heads to show what side of the coin\n        heads += 1  # and we're adding one to our heads variable\n    else: #if you wanted you could of wrote elif flip == 1: however, \n          # since this is a binary else works as well \n        print(\"Tails!\") # tells the user a tails was flipped\n        tails += 1 # adds 1 to the tails counter\n        \n        \nprint(\"The was a total of \", tosses - 1, \" tosses.\", heads, \"heads and \", tails, \"tails.\")  #we have to minus off the one we added previously \n           #to ensure we display the correct number\n\n<\/code><\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">An important point to remember!<\/h2>\n\n\n\n<p>In all the examples of loops on this page, we&#8217;ve used i. You can replace i with and letter you would like, or a variable. <\/p>\n\n\n\n<p>In the next lesson, we&#8217;ll be taking a look at how to store and interact with larger data sets.<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-media-text alignwide is-stacked-on-mobile\" style=\"grid-template-columns:17% auto\"><figure class=\"wp-block-media-text__media\"><img loading=\"lazy\" decoding=\"async\" width=\"683\" height=\"1024\" src=\"https:\/\/hub.salford.ac.uk\/psytech\/wp-content\/uploads\/sites\/95\/2022\/08\/aaaPaige-Metcalf-683x1024.jpg\" alt=\"\" class=\"wp-image-3201 size-full\" srcset=\"https:\/\/hub.salford.ac.uk\/psytech\/wp-content\/uploads\/sites\/95\/2022\/08\/aaaPaige-Metcalf-683x1024.jpg 683w, https:\/\/hub.salford.ac.uk\/psytech\/wp-content\/uploads\/sites\/95\/2022\/08\/aaaPaige-Metcalf-200x300.jpg 200w, https:\/\/hub.salford.ac.uk\/psytech\/wp-content\/uploads\/sites\/95\/2022\/08\/aaaPaige-Metcalf-768x1152.jpg 768w, https:\/\/hub.salford.ac.uk\/psytech\/wp-content\/uploads\/sites\/95\/2022\/08\/aaaPaige-Metcalf-1024x1536.jpg 1024w, https:\/\/hub.salford.ac.uk\/psytech\/wp-content\/uploads\/sites\/95\/2022\/08\/aaaPaige-Metcalf-1365x2048.jpg 1365w, https:\/\/hub.salford.ac.uk\/psytech\/wp-content\/uploads\/sites\/95\/2022\/08\/aaaPaige-Metcalf-scaled.jpg 1707w\" sizes=\"auto, (max-width: 683px) 100vw, 683px\" \/><\/figure><div class=\"wp-block-media-text__content\">\n<h4 class=\"has-text-align-left has-large-font-size wp-block-heading\">Course Author<\/h4>\n\n\n\n<hr class=\"wp-block-separator aligncenter has-alpha-channel-opacity is-style-wide\" \/>\n\n\n\n<p style=\"font-style:italic;font-weight:500\">Paige Metcalfe<br>c.p.metcalfe@salford.ac.uk<\/p>\n<\/div><\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Loops allow up to repeat code and iterate over large data sets. The latter, we&#8217;ll be getting more into in the next session. Loops come in two varieties, you have while loops and for loops. While loops will run the code while a condition is met. For loops will run the code for a specified [&hellip;]<\/p>\n","protected":false},"author":177,"featured_media":0,"parent":2877,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_acf_changed":false,"footnotes":""},"class_list":["post-2949","page","type-page","status-publish","hentry"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Loops - Python - Salford PsyTech Home \u2192\u2302<\/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:\/\/hub.salford.ac.uk\/psytech\/python\/loops-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Loops - Python - Salford PsyTech Home \u2192\u2302\" \/>\n<meta property=\"og:description\" content=\"Loops allow up to repeat code and iterate over large data sets. The latter, we&#8217;ll be getting more into in the next session. Loops come in two varieties, you have while loops and for loops. While loops will run the code while a condition is met. For loops will run the code for a specified [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/hub.salford.ac.uk\/psytech\/python\/loops-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Salford PsyTech Home \u2192\u2302\" \/>\n<meta property=\"article:modified_time\" content=\"2022-07-19T09:37:55+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/hub.salford.ac.uk\/psytech\/wp-content\/uploads\/sites\/95\/2022\/06\/image-11-1024x1024.png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/hub.salford.ac.uk\\\/psytech\\\/python\\\/loops-python\\\/\",\"url\":\"https:\\\/\\\/hub.salford.ac.uk\\\/psytech\\\/python\\\/loops-python\\\/\",\"name\":\"Loops - Python - Salford PsyTech Home \u2192\u2302\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/hub.salford.ac.uk\\\/psytech\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/hub.salford.ac.uk\\\/psytech\\\/python\\\/loops-python\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/hub.salford.ac.uk\\\/psytech\\\/python\\\/loops-python\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/hub.salford.ac.uk\\\/psytech\\\/wp-content\\\/uploads\\\/sites\\\/95\\\/2022\\\/06\\\/image-11-1024x1024.png\",\"datePublished\":\"2022-06-17T13:10:54+00:00\",\"dateModified\":\"2022-07-19T09:37:55+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/hub.salford.ac.uk\\\/psytech\\\/python\\\/loops-python\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/hub.salford.ac.uk\\\/psytech\\\/python\\\/loops-python\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/hub.salford.ac.uk\\\/psytech\\\/python\\\/loops-python\\\/#primaryimage\",\"url\":\"https:\\\/\\\/hub.salford.ac.uk\\\/psytech\\\/wp-content\\\/uploads\\\/sites\\\/95\\\/2022\\\/06\\\/image-11.png\",\"contentUrl\":\"https:\\\/\\\/hub.salford.ac.uk\\\/psytech\\\/wp-content\\\/uploads\\\/sites\\\/95\\\/2022\\\/06\\\/image-11.png\",\"width\":1200,\"height\":1200},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/hub.salford.ac.uk\\\/psytech\\\/python\\\/loops-python\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/hub.salford.ac.uk\\\/psytech\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\",\"item\":\"https:\\\/\\\/hub.salford.ac.uk\\\/psytech\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Loops &#8211; Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/hub.salford.ac.uk\\\/psytech\\\/#website\",\"url\":\"https:\\\/\\\/hub.salford.ac.uk\\\/psytech\\\/\",\"name\":\"Salford PsyTech Home\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\\\/\\\/hub.salford.ac.uk\\\/psytech\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/hub.salford.ac.uk\\\/psytech\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/hub.salford.ac.uk\\\/psytech\\\/#organization\",\"name\":\"Salford PsyTech Home\",\"url\":\"https:\\\/\\\/hub.salford.ac.uk\\\/psytech\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/hub.salford.ac.uk\\\/psytech\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/hub.salford.ac.uk\\\/psytech\\\/wp-content\\\/uploads\\\/sites\\\/95\\\/2025\\\/02\\\/university-salford-logo-400x250-1.png\",\"contentUrl\":\"https:\\\/\\\/hub.salford.ac.uk\\\/psytech\\\/wp-content\\\/uploads\\\/sites\\\/95\\\/2025\\\/02\\\/university-salford-logo-400x250-1.png\",\"width\":400,\"height\":250,\"caption\":\"Salford PsyTech Home\"},\"image\":{\"@id\":\"https:\\\/\\\/hub.salford.ac.uk\\\/psytech\\\/#\\\/schema\\\/logo\\\/image\\\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Loops - Python - Salford PsyTech Home \u2192\u2302","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:\/\/hub.salford.ac.uk\/psytech\/python\/loops-python\/","og_locale":"en_US","og_type":"article","og_title":"Loops - Python - Salford PsyTech Home \u2192\u2302","og_description":"Loops allow up to repeat code and iterate over large data sets. The latter, we&#8217;ll be getting more into in the next session. Loops come in two varieties, you have while loops and for loops. While loops will run the code while a condition is met. For loops will run the code for a specified [&hellip;]","og_url":"https:\/\/hub.salford.ac.uk\/psytech\/python\/loops-python\/","og_site_name":"Salford PsyTech Home \u2192\u2302","article_modified_time":"2022-07-19T09:37:55+00:00","og_image":[{"url":"https:\/\/hub.salford.ac.uk\/psytech\/wp-content\/uploads\/sites\/95\/2022\/06\/image-11-1024x1024.png","type":"","width":"","height":""}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/hub.salford.ac.uk\/psytech\/python\/loops-python\/","url":"https:\/\/hub.salford.ac.uk\/psytech\/python\/loops-python\/","name":"Loops - Python - Salford PsyTech Home \u2192\u2302","isPartOf":{"@id":"https:\/\/hub.salford.ac.uk\/psytech\/#website"},"primaryImageOfPage":{"@id":"https:\/\/hub.salford.ac.uk\/psytech\/python\/loops-python\/#primaryimage"},"image":{"@id":"https:\/\/hub.salford.ac.uk\/psytech\/python\/loops-python\/#primaryimage"},"thumbnailUrl":"https:\/\/hub.salford.ac.uk\/psytech\/wp-content\/uploads\/sites\/95\/2022\/06\/image-11-1024x1024.png","datePublished":"2022-06-17T13:10:54+00:00","dateModified":"2022-07-19T09:37:55+00:00","breadcrumb":{"@id":"https:\/\/hub.salford.ac.uk\/psytech\/python\/loops-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/hub.salford.ac.uk\/psytech\/python\/loops-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/hub.salford.ac.uk\/psytech\/python\/loops-python\/#primaryimage","url":"https:\/\/hub.salford.ac.uk\/psytech\/wp-content\/uploads\/sites\/95\/2022\/06\/image-11.png","contentUrl":"https:\/\/hub.salford.ac.uk\/psytech\/wp-content\/uploads\/sites\/95\/2022\/06\/image-11.png","width":1200,"height":1200},{"@type":"BreadcrumbList","@id":"https:\/\/hub.salford.ac.uk\/psytech\/python\/loops-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/hub.salford.ac.uk\/psytech\/"},{"@type":"ListItem","position":2,"name":"Python","item":"https:\/\/hub.salford.ac.uk\/psytech\/python\/"},{"@type":"ListItem","position":3,"name":"Loops &#8211; Python"}]},{"@type":"WebSite","@id":"https:\/\/hub.salford.ac.uk\/psytech\/#website","url":"https:\/\/hub.salford.ac.uk\/psytech\/","name":"Salford PsyTech Home","description":"","publisher":{"@id":"https:\/\/hub.salford.ac.uk\/psytech\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/hub.salford.ac.uk\/psytech\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/hub.salford.ac.uk\/psytech\/#organization","name":"Salford PsyTech Home","url":"https:\/\/hub.salford.ac.uk\/psytech\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/hub.salford.ac.uk\/psytech\/#\/schema\/logo\/image\/","url":"https:\/\/hub.salford.ac.uk\/psytech\/wp-content\/uploads\/sites\/95\/2025\/02\/university-salford-logo-400x250-1.png","contentUrl":"https:\/\/hub.salford.ac.uk\/psytech\/wp-content\/uploads\/sites\/95\/2025\/02\/university-salford-logo-400x250-1.png","width":400,"height":250,"caption":"Salford PsyTech Home"},"image":{"@id":"https:\/\/hub.salford.ac.uk\/psytech\/#\/schema\/logo\/image\/"}}]}},"_links":{"self":[{"href":"https:\/\/hub.salford.ac.uk\/psytech\/wp-json\/wp\/v2\/pages\/2949","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hub.salford.ac.uk\/psytech\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/hub.salford.ac.uk\/psytech\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/hub.salford.ac.uk\/psytech\/wp-json\/wp\/v2\/users\/177"}],"replies":[{"embeddable":true,"href":"https:\/\/hub.salford.ac.uk\/psytech\/wp-json\/wp\/v2\/comments?post=2949"}],"version-history":[{"count":13,"href":"https:\/\/hub.salford.ac.uk\/psytech\/wp-json\/wp\/v2\/pages\/2949\/revisions"}],"predecessor-version":[{"id":3098,"href":"https:\/\/hub.salford.ac.uk\/psytech\/wp-json\/wp\/v2\/pages\/2949\/revisions\/3098"}],"up":[{"embeddable":true,"href":"https:\/\/hub.salford.ac.uk\/psytech\/wp-json\/wp\/v2\/pages\/2877"}],"wp:attachment":[{"href":"https:\/\/hub.salford.ac.uk\/psytech\/wp-json\/wp\/v2\/media?parent=2949"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}