Advertisement
jspill

webinar-for-loops-2023-01-07

Jan 7th, 2023
911
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.91 KB | None | 0 0
  1. # 2023 Jan 7
  2.  
  3. # WEBINAR: For Loops
  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 _something_ in _someContainer__:
  14.  
  15. # list
  16. myList = ["Agent Scully", "Agent Mulder", "Walter Skinner", "CSM", "Mr. X"]
  17. for item in myList:
  18.     print(item) # print(item, end="\n")
  19.  
  20. # tuples
  21. myTuple = ("Gilligan", "Castaway002", "red", "crew")
  22. for item in myTuple:
  23.     print(item)
  24.  
  25. # strings
  26. myString = "It was the best of times."
  27. for char in myString:
  28.     # print(char) # a lot of returns!
  29.     print(char, end="")
  30. print() # print(end="\n")
  31. # print("A clean new line!") # and comment out or delete after!
  32.  
  33. # dictionaries
  34. bestOfXF = {
  35.     "1x00": "Pilot",
  36.     "2x10": "Red Museum",
  37.     "2x14": "Die Hand Die Verletzt",
  38.     "3x04": "Clyde Bruckman's Final Repose",
  39.     "3x12": "War of the Coprophages",
  40.     "3x20": "Jose Chung's From Outer Space",
  41.     "4x05": "The Field Where I Died",
  42.     "5x05": "The Post Modern Prometheus",
  43.     "5x17": "All Souls"
  44. }
  45. # myDict[key] # retrieve the value for that key... similar to dict method .get()
  46. # myDict[key] = value # assign a new value to that key
  47.  
  48. for key in bestOfXF: # for key in nameOfDict:
  49.     # print("Check out Episode {} or {}".format(key, bestOfXF[key]))
  50.     # print("Check out Episode {} or {}".format(key, bestOfXF.get(key)))
  51.     value = bestOfXF[key]
  52.     print("Check out Episode {} or {}".format(key, value))
  53.  
  54. # print(bestOfXF.items())
  55. # x, y = [1, 3.14]
  56. # print(y)
  57. for k, v in bestOfXF.items():
  58.     print("Check out Episode {} or {}".format(k, v))
  59.  
  60.  
  61. # myDict[key] is like myList[num]
  62. # print(myList[0]) # retrieving a value for an index position in a list is similar to retrieving a dictionary value associated with a key
  63.  
  64. # range()
  65. for num in range(0, 5): # [0, 1, 2, 3, 4]
  66.     print(num)
  67.  
  68. # range() of len()
  69. for i in range(0, len(myList)):
  70.     print("{} - {}".format(i, myList[i]))
  71.  
  72.  
  73. # Student questions...
  74.  
  75.  
  76. # Lab 25.3
  77. # This is more of a while loop situation, and a great example of the kind of thing I'll go over next week in my Input Patterns webinar
  78. '''
  79. Write a program that reads a list of integers into a list as long as the integers are greater than zero, then outputs the smallest and largest integers in the list.
  80.  
  81. Ex: If the input is:
  82.  
  83. 10
  84. 5
  85. 3
  86. 21
  87. 2
  88. -6
  89. the output is:
  90.  
  91. 2 and 21
  92. You can assume that the list of integers will have at least 2 values.
  93. '''
  94. myInput = int(input())
  95. numList = []
  96. while myInput > 0: # another common sentinel value condition is while myInput != "-1":
  97.     numList.append(myInput)
  98.     myInput = int(input())
  99. # print("numList is: {}".format(numList)) # see? Looks like what we need, right?
  100. theMax = max(numList)
  101. theMin = min(numList)
  102. print("{} and {}".format(theMin, theMax))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement