jspill

webinar-for-loops-2021-04-10

Apr 10th, 2021
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.49 KB | None | 0 0
  1. # WEBINAR: for loops
  2.  
  3. # FOR LOOPS are used for repeating actions for every element
  4. # in a container like a list, string, tuple, etc...
  5. myList = ["Gilligan", "Scooby", "Agent Scully"] # tv characters
  6. myTuple = ("Gilligan", "Castaway002", "red", "crew") # things about Gilligan
  7.  
  8. for item in myList:
  9.     print(item)
  10.  
  11. for item in myTuple:
  12.     print(item)
  13.  
  14. mySet = {"Gilligan", "Maynard", "Kelp"} # Bob Denver roles
  15. for item in mySet:
  16.     print(item)
  17.  
  18. myString = "Just sit right back and we'll talk about Gilligan and company."
  19. # for char in myString: # commenting out so we don't print so much...
  20. #     print(char)
  21.  
  22. # how about looping over dictionaries?
  23. scoobies = {
  24.     "Scooby": "a blue collar",
  25.     "Shaggy": "green",
  26.     "Velma": "orange",
  27.     "Daphne": "purple",
  28.     "Fred": "an ascot"
  29. }
  30. for key in scoobies:
  31.     # someDict[key] --> value of that key
  32.     # print(key, scoobies[key])
  33.     print("{} wears {}.".format(key, scoobies[key]))
  34.  
  35. # another way to loop over dictionaries
  36. for key, value in scoobies.items(): # 2 loop vars with dictionary items method
  37.     print("{} always wears {}.".format(key, value))
  38.  
  39. for item in range(5): # range() makes an iterable sequence from an integer
  40.     print(item)
  41.  
  42. # what if I'm interesting in the index position?
  43. for i in range(len(myList)):
  44.     print("{} is at position {}".format(myList[i], i))
  45.  
  46. for i, item in enumerate(myList): # enumerate() also gets you 2 loop vars: index and value at that index
  47.     print(i,item)
  48.  
  49. # CA 6.10.1
  50. # "Simon Says" is a memory game where "Simon" outputs a sequence of 10 characters
  51. # (R, G, B, Y) and the user must repeat the sequence. Create a for loop that compares
  52. # the two strings. For each match, add one point to user_score.
  53. # Upon a mismatch, end the game. Ex: The following patterns yield a user_score of 4:
  54.  
  55. simon_pattern = 'RRGBRYYBGY'
  56. user_pattern  = 'RRGBBRYBGY'
  57. user_score = 0
  58. for i in range(len(simon_pattern)):
  59.     if simon_pattern[i] == user_pattern[i]:
  60.         user_score += 1
  61.     else:
  62.         break
  63. print("User score:", user_score)
  64.  
  65. # HTML is plain text... strings!
  66. myContainer = ("peanut butter", "bread", "milk", "cheese puffs")
  67. # we want this output:
  68. # <ul>
  69. # <li>peanut butter</li>
  70. # <li>bread</li>
  71. # <li>milk</li>
  72. # <li>cheese puffs</li>
  73. # </ul>
  74. myHTMLString = "<ul>\n"
  75. for item in myContainer:
  76.     myHTMLString += "<li>{}</li>\n".format(item)
  77. myHTMLString += "</ul>"
  78. print(myHTMLString)
  79.  
  80. # use \ for escaping (excluding) characters, or special escaped char values like \n, \t, etc
  81.  
Advertisement
Add Comment
Please, Sign In to add comment