jspill

webinar-for-loops-2021-11-13

Nov 13th, 2021 (edited)
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.57 KB | None | 0 0
  1. # WEBINAR: For Loops
  2. # We use loops to repeat actions
  3.  
  4. # a WHILE loop... btw.. is basically an IF statement
  5. # that repeats as long as its condition is true
  6.  
  7. # FOR LOOPS are used for repeating actions for every element
  8. # in a container like a list, string, tuple, etc...
  9.  
  10. myList = ["Agent Scully", "Agent Mulder", "Walter Skinner", "CSM", "Mr. X"]
  11.  
  12. # for _loop_variable_ in _someContainer_:
  13. for item in myList:
  14.     print(item)
  15.  
  16. myTuple = ("Gilligan", "Castaway002", "red", "crew")
  17. for item in myTuple:
  18.     print(item)
  19.  
  20. myString = "Just sit right back and you'll hear a tale."
  21. for char in myString:
  22.     # if char.isupper():
  23.     #   print(char)
  24.     print(char, end=" ")
  25. print()
  26. print(myString)
  27.  
  28. tvLovesDCT = {
  29.     "Joannie": "Chaci",
  30.     "Buffy": "Angel",
  31.     "Mulder": "Scully",
  32.     "The Mandalorian": "Grogu"
  33. }
  34. # myDictionary[someKey] = someValue
  35.  
  36. for key in tvLovesDCT:
  37.     # print(key)
  38.     print("{} loves {}".format(key, tvLovesDCT[key]))
  39. # if "Mulder" in tvLovesDCT:
  40. #   print("found!")
  41. # if "Scully" in tvLovesDCT:
  42. #   print("found?")
  43. for k, v in tvLovesDCT.items():
  44.     print("{} --> {}".format(v, k))
  45.  
  46. # for n in 5: # can't do that!
  47. for n in range(0, 5):
  48.     print(n)
  49.  
  50. # WHILE LOOP differences
  51. x = 0 # we must instantiate the variable
  52. while x < 5: # we must think of a useful condition
  53.     print("{} is still less than 5".format(x))
  54.     x += 1 # we have to do this: increment/decrement/change the variable
  55.  
  56. # FOR LOOP with INDEX position
  57. # combine range() and len()
  58. for i in range(0, len(myList)):
  59.     print(i, end=": ")
  60.     print(myList[i])
  61.    
  62. # Lab 6.14 example
  63. x = int(input())
  64. while x > 0:
  65.     print(x % 2, end="")
  66.     x = int(x / 2)
  67. print()
  68.  
  69. # Question, how can I reverse that?
  70. # To reverse it, you have to store it so you have something to work with.
  71. # Remember print() doensn't store anything for later.
  72. x = int(input())
  73. while x > 0:
  74.     print(x % 2, end="")
  75.     x = int(x / 2)
  76. print()
  77.  
  78. strX = ""
  79. while x > 0:
  80.     strX += str(x%2)
  81.     x = int(x/2)
  82. print(strX)
  83. # reverse it... and print() the reverse...
  84. print(strX[::-1])
  85. # But if you're asking because of the similar Lab 7.21...
  86. # the CONTEXT of the questions in Lab 6.14 and Lab 7.21 differs!!!
  87. # Lab 7.21 involves two FUNCTIONS that each have a single job
  88. # one to get the binary
  89. # the second to reverse the result of the first
  90. # note that this involves return values, not printing directly
  91.  
  92. # Lab 23.5 example
  93. # Declare variable to hold running count.
  94. count = 0
  95.  
  96. low = int(input())
  97. high = int(input())
  98. x = int(input())
  99.  
  100. for i in range(low, high + 1):
  101.     if i % x == 0:
  102.         count = count + 1
  103.  
  104. print(count)
Add Comment
Please, Sign In to add comment