Advertisement
jspill

webinar-for-loops-2021-05-08

May 8th, 2021
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.74 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.  
  6. # (as opposed to a WHILE loop, which is like an IF statement that repeats as long as it's true)
  7.  
  8. # let's get some iterable objects in here
  9. myList = ["Gilligan", "Scooby", "Agent Scully", "Fonzie"] # tv characters
  10. myTuple = ("Gilligan", "Castaway002", "red", "crew") # things about Gilligan
  11.  
  12. # Basic syntax of a for loop
  13. # for __ in ___:
  14.  
  15. # Looping over a L I S T
  16. for item in myList:        # I use "item" as my loop variable, any valid name works but keep it simple and descriptive!
  17.     print(item) # print(item, end=" ")
  18.  
  19. # Looping over a T U P L E.. it's the same! Same with SETS too.
  20. for item in myTuple:
  21.     print(item)
  22.  
  23. # S T R I N G S --> each loop iteration is a single character
  24. myString = "Just sit right back and we'll talk about Gilligan and company."
  25. # for char in myString:     # I tend to use "char" or "ch" or "letter" when looping over a string
  26. #     print(char, end="\n")
  27.  
  28. # D I C T I O N A R I E S
  29. # dictionaries have KEYS and VALUES
  30. scoobiesDCT = {
  31.     "Scooby": "a blue collar", # key: value,
  32.     "Shaggy": "green",
  33.     "Velma": "orange",
  34.     "Daphne": "purple",
  35.     "Fred": "an ascot"
  36. }
  37. for key in scoobiesDCT: # for loop var for a dictionary is the KEY
  38.     # dict[key] --> gets you the value for that key
  39.     print(key, "always wears", scoobiesDCT[key])
  40.  
  41. # an alternate way to for loop over a dictionary
  42. # for __, __ in dict.items()
  43. for k, v in scoobiesDCT.items():
  44.     print(k, "almost always wears", v)
  45.  
  46. # R A N G E
  47. # range() function creates an iterable sequence from a number (integer)
  48. for n in range(5):
  49.     print(n)
  50.  
  51. # sometimes the INDEX is as important as the value at that index
  52. for i in range(len(myList)):
  53.     print(i, myList[i])
  54.  
  55. # or with ENUMERATE we get 2 variables
  56. for i, item in enumerate(myList):
  57.     # print(str(i), "-->", item)
  58.     print("{} --> {}".format(i, item))
  59.  
  60.  
  61. # some ZyBooks loop problems...
  62.  
  63. # CA 6.5.2
  64. contact_emails = {
  65.     'Sue Reyn' : 's.reyn@email.com',
  66.     'Mike Filt': 'mike.filt@bmail.com',
  67.     'Nate Arty': 'narty042@nmail.com'
  68. }
  69. # output for each:   s.reyn@email.com is Sue Reyn, etc
  70. for contact in contact_emails:
  71.     print(contact_emails[contact], "is", contact)
  72. # or...
  73. for key in contact_emails: # the book used "contact" as the var, I'll stick my fav "item"
  74.     print("{} is {}".format(contact_emails[key], key))
  75.  
  76.  
  77. # CA 6.10.1
  78. user_score = 0
  79. simon_pattern = 'RRGBRYYBGY'
  80. user_pattern  = 'RRGBBRYBGY'
  81. # for i in range(10):
  82. for i in range(len(simon_pattern)):
  83.     if simon_pattern[i] == user_pattern[i]:
  84.         user_score += 1
  85.     else:
  86.         break # end loop when they no longer match
  87. print("User score: ", user_score) # 4
  88.  
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement