Advertisement
jspill

webinar-for-loops-2021-12-11

Dec 11th, 2021
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.41 KB | None | 0 0
  1. # Sat Dec 11 2021
  2. # WEBINAR: For Loops
  3. # We use loops to repeat actions
  4.  
  5. # a WHILE loop... btw.. is basically an IF statement
  6. # that repeats as long as its condition is true
  7.  
  8. # FOR LOOPS are used for repeating actions for every element
  9. # in a container like a list, string, tuple, etc...
  10.  
  11. myList = ["Agent Scully", "Agent Mulder", "Walter Skinner", "CSM", "Mr. X"] # X-Files characters
  12. for item in myList:
  13.     print(item)
  14. myTuple = ("Gilligan", "Castaway002", "red", "crew")
  15. for item in myTuple:
  16.     print(item)
  17. myString = "Just sit right back and you'll hear a tale."
  18. for char in myString:
  19.     print(char, end=" ") # take this into account
  20. print() # print(end="\n")
  21. print("And the next call")
  22.  
  23. scoobiesDCT = {
  24.     "Scooby": "a blue collar",
  25.     "Shaggy": "green",
  26.     "Velma": "orange",
  27.     "Daphne": "purple",
  28.     "Fred": "an ascot"
  29. }
  30. # nameOfDict[key] # retrieves the value for that key
  31. # nameOfDict[key] = value # whether that key/value pair were there before or not
  32. for key in scoobiesDCT:
  33.     print("{} wears {}.".format(key, scoobiesDCT[key]))
  34.  
  35. for n in range(0, 10): # range() creates a list-like container of a series of numbers
  36.     print(n)
  37.  
  38. for i in range(len(myList)):
  39.     # print(i)
  40.     # item = myList[i]
  41.     print("{} - {}".format(i, myList[i]))
  42.  
  43. for i, item in enumerate(myList):
  44.     print("{} --- {}".format(i, item))
  45.  
  46. for k, v in scoobiesDCT.items():
  47.     print("{} ALWAYS wears {}.".format(k, v))
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement