Advertisement
jspill

webinar-for-loops-2022-10-08

Oct 8th, 2022
1,491
-1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.37 KB | None | 0 1
  1. # 2022 Oct 8
  2. # WEBINAR: For Loops
  3.  
  4. # We use loops to repeat actions
  5.  
  6. # a WHILE loop... btw.. is basically an IF statement
  7. # that repeats as long as its condition is true
  8.  
  9. # FOR LOOPS are used for repeating actions for every element
  10. # in a container like a list, string, tuple, etc...
  11.  
  12. # Basic syntax of a for loop
  13. # for __ in _someContainer_:
  14.  
  15. myList = ["Agent Scully", "Agent Mulder", "Walter Skinner", "CSM", "Mr. X"]
  16. for item in myList:
  17.     print(item) # print(end="\n")
  18.  
  19. for item in myList:
  20.     print(item, end=" ")
  21. print()
  22.  
  23. print("Something else on a clean line...")
  24.  
  25. # tuples
  26. myTuple = ("Gilligan", "Castaway002", "red", "crew")
  27. for item in myTuple:
  28.     print(item)
  29.  
  30. # strings
  31. myString = "It was the best of times. \nIt was the\t worst of times."
  32. # for char in myString:
  33. #     print(char)
  34.  
  35. # side note: string is___() methods
  36. for char in myString:
  37.     if char.isupper():
  38.         print(char)
  39.  
  40. # "fill the basket" of a new container
  41. newStr = ""
  42. for char in myString:
  43.     if not char.isspace():
  44.         newStr += char
  45. print(newStr)
  46.  
  47. # use str.replace()
  48. # just a literal space... myString = myString.replace(" ", "")
  49. for char in myString:
  50.     if char.isspace():
  51.         myString = myString.replace(char, "")
  52. print(myString)
  53.  
  54. # dictionaries
  55. # myDict[key] # retrieve the value for that key
  56. # myDict[key] = value # assign a value to that key
  57.  
  58. #for _key_ in myDict: # loop var holds the KEY, whatever we name it
  59.  
  60. bestOfXF = {
  61.     "1x00": "Pilot",
  62.     "2x10": "Red Museum",
  63.     "2x14": "Die Hand Die Verletzt",
  64.     "3x04": "Clyde Bruckman's Final Repose",
  65.     "3x12": "War of the Coprophages",
  66.     "3x20": "Jose Chung's From Outer Space",
  67.     "4x05": "The Field Where I Died",
  68.     "5x05": "The Post Modern Prometheus",
  69.     "5x17": "All Souls"
  70. }
  71. for key in bestOfXF:
  72.     # print: Check out Episode KEY or 'VALUE'
  73.     print("Check out Episode {} or '{}'!".format(key, bestOfXF[key])) # myDict[key] gets the value
  74.  
  75. # range()
  76. # the RANGE object and just doing things some # of times
  77. for num in range(5): # range(0, 5)
  78.     print(num)
  79.  
  80. # looping over a list but interested in indices (that's when I use "i" as a var name)
  81. for i in range(len(myList)):
  82.     print("{}: {}".format(i, myList[i]))
  83.  
  84. # Student question: Can you use help() and dir() in C859 v4/D335 OA?
  85. # Yes! They're built-in functions and part of Python
  86.  
  87. # help(str)
  88. # print(dir(str))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement