Advertisement
jspill

webinar-for-loops-functions-2021-07-17

Jul 17th, 2021
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.79 KB | None | 0 0
  1. # WEBINAR: for loops --- We're almost there... starting in just a moment
  2. # we threw in a Two Fer today and looked at FUNCTIONS + METHODS too
  3.  
  4. # We use loops to repeat actions
  5. # a WHILE loop is basically an IF statement
  6. # that repeats as long as its condition is true
  7.  
  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 FOR LOOP syntax with a list
  13. myList = ["Agent Scully", "Agent Mulder", "Walter Skinner", "CSM", "Mr. X"] # X-Files characters
  14. myTuple = ("Gilligan", "Castaway002", "red", "crew") # things about Gilligan
  15.  
  16. # for __ in __:
  17. for item in myList: # I use "item" as my var name with lists, sets, and tuples
  18.     print(item)
  19.  
  20. for item in myTuple:
  21.     print(item)
  22.  
  23. myString = "Just sit right back and you'll hear a tale."
  24. for char in myString:
  25.     print(char, end=" ")
  26.  
  27. # dictionary
  28. # myDCT{
  29. #     key: value,
  30. #     key1: value1
  31. # }
  32. scoobiesDCT = {
  33.     "Scooby": "a blue collar",
  34.     "Shaggy": "green",
  35.     "Velma": "orange",
  36.     "Daphne": "purple",
  37.     "Fred": "an ascot"
  38. }
  39. # for __ in __someDictionary__: # one var?
  40. print() # clear out that extra " " from the last loop
  41. for key in scoobiesDCT:
  42.     # someDictionary[key] --> get the value for that key
  43.     # val = scoobiesDCT[key]
  44.     print("{} always wears {}.".format(key, scoobiesDCT[key]))
  45.     # print("{} always wears {}.".format(key, val))
  46.  
  47. for k, v in scoobiesDCT.items():
  48.     print("{} almost always wears {}.".format(k, v))
  49.  
  50. # R A N G E
  51. # range() function creates an iterable sequence from a number or numbers
  52. for n in range(0, 5):
  53.     print(n)
  54.  
  55. # for item in myList
  56. for i in range(len(myList)):
  57.     print(i, myList[i])
  58.  
  59. # see also the enumerate() function
  60. for i, item in enumerate(myList):
  61.     print("{} --> {}".format(i, item))
  62.  
  63. # how many numbers b/n 1 and 100 are div by 25?
  64. count = 0
  65. for n in range(1, 101):
  66.     if n % 25 == 0:
  67.         count += 1
  68. print("How many were divisible?", count)
  69.  
  70. # how many numbers b/n 32 and 45687987 are div by 77? # I commented this out b/c it takes a long time to run!
  71. # same question as above really
  72. # count = 0
  73. # for n in range(32, 45687987+1):
  74. #     if n % 77 == 0:
  75. #         count += 1
  76. # print("How many were divisible?", count)
  77.  
  78. # Functions
  79. # Complete the function to PRINT all of the words in the given string
  80. def printWords(mystring):
  81.     # x = 5 # don't do this to parameters
  82.     print(mystring.split()) # correct for this question
  83.     # return mystring.split() # would be wrong here, but would allow us to loop over the returned list
  84.  
  85.  
  86. printWords('WGU College of IT')# expected output: ['WGU', 'College', 'of', 'IT']
  87. printWords('Night Owls Rock')  # expected output: ['Night', 'Owls', 'Rock']
  88. printWords('WKRP in Cincinatti') # ['WKRP', 'in', 'Cincinatti']
  89.  
  90. # function definition, Gallant
  91. def squareThis(num):
  92.     return num * num
  93.  
  94. print(squareThis(5)) # 25
  95. print(squareThis(9)) # 91
  96.  
  97. # bad function from Goofus
  98. def squareSomething(num):
  99.     num = 5 # don't do this!
  100.     return num * num
  101.  
  102. print(squareSomething(5))
  103. print(squareSomething(9))
  104. print(squareSomething(4))
  105.  
  106. print(1 + squareThis(8)) # 65, we can do this because the function RETURNS or EVALUATES TO an integer value
  107.  
  108.  
  109. # METHODS are functions that belong to a class or type, like list.append() or string.replace()
  110. # You'll spend a lot of the later chapters getting to know methods of the common types
  111.  
  112. # I've modified this so we can leave the printWords() function above alone...
  113. for item in "Come and knock on our door".split(): # the str split() method returns a list, so I can use it like a list
  114.     print(item)
  115.  
  116. for item in "WKRP in Cincinatti".split():
  117.     print(item)
  118.  
  119. print(dir(dict)) # dir() returns a list...
  120.  
  121. for item in dir(list): # ... so I can use it like a list
  122.     if not item.startswith("_"):
  123.         print(item)
  124.  
  125.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement