jspill

webinar-for-loops-2022-01-08

Jan 8th, 2022 (edited)
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.60 KB | None | 0 0
  1. # 2022 Jan 8
  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"]
  12. for item in myList:
  13.     print(item)
  14.  
  15. myTuple = ("Gilligan", "Castaway002", "red", "crew")
  16. for item in myTuple:
  17.     print(item)
  18.  
  19. myString = "Just sit right back and you'll hear a tale."
  20. for char in myString:
  21.     print(char, end=" ") # don't leave the cursor hanging here though!
  22. print() # next print() starts on a clean line
  23.  
  24. # dictionaries
  25. # someDictionary = {"key": "value"}
  26. scoobiesDCT = {
  27.     "Scooby": "a blue collar",
  28.     "Shaggy": "green",
  29.     "Velma": "orange",
  30.     "Daphne": "purple",
  31.     "Fred": "an ascot"
  32. }
  33. # myDictionary[someKey]
  34. # myDictionary[someKey] = value
  35. for key in scoobiesDCT:
  36.     print("{} wears {}.".format(key, scoobiesDCT[key])) # str.format()
  37.     # print(f"{key} wears {scoobiesDCT[key]}.") # f string
  38.     # print(str(key) + " wears " + scoobiesDCT[key] + ".") # concatenation with +
  39.     # print("%s wears %s." % (key, scoobiesDCT[key])) # "data conversion specifiers" or "string modulo"
  40.  
  41. for n in range(0, 5): # range() function creates a list-like container of those numbers
  42.     print("hey")
  43.  
  44. for i in range(0, len(myList)):
  45.     print("{}: {}".format(i, myList[i]))
  46.  
  47.  
  48. # Lab 6.15 Password Modifier
  49. # There are different ways we could approach this one....
  50.  
  51. word = input()
  52. password = "" # sitting there waiting for us to add the right chars...
  53.  
  54. # You could just loop over the first string and use a long if/elif/else to check the chars
  55.  
  56. for ltr in word:
  57.     if ltr == 'i':
  58.         ltr = '1'
  59.         password += ltr
  60.     if ltr == 'm':
  61.         ltr = 'M'
  62.         password += ltr
  63.     if ltr == 'B':
  64.         ltr = '8'
  65.         password += ltr
  66.     if ltr == 's':
  67.         ltr = '$'
  68.         password += ltr
  69.     else:
  70.         ltr == ltr
  71.         password += ltr
  72. password += '!'
  73. print(password)
  74.  
  75. # OR... this would be a good place to use a dictionary!
  76. newLetters = {
  77.     "i": "1",
  78.     "a" : "@",
  79.     "m" : "M",
  80.     "B" : "8",
  81.     "s" : "$"
  82.     }
  83. for ltr in word:
  84.     if ltr in newLetters: # checking ltr as a key in the dictionary
  85.         password += newLetters[ltr]
  86.     else:
  87.         password += ltr
  88. # and again AFTER the loop...
  89. password += '!'
  90. print(password)
  91.  
  92. # A great student suggestion here: you could use the str replace() method and not even need to loop
  93. password = word.replace("i", "1")
  94. password = word.replace("a", "@") # and so on...
  95.  
  96. # Note that since str.replace() RETURNS a string itself, you can daisy-chain those methods:
  97. password = word.replace("i", "1").replace("a", "@").replace("m", "M") #... and so on again!
  98. # just don't forget to add the "!" at the end again
  99.  
  100. # or combining the dictionary and .replace() approaches
  101. for ltr in word:
  102.     if ltr in newLetters: # checking ltr as a key in the dictionary
  103.         # if you're using the original input variable, don't forget to re-assign!
  104.         word = word.replace(ltr, newLetters[ltr])
  105. # again add the "!" and print
  106.  
  107. # All of these are good ways to approach this problem!
  108.  
  109.  
  110. # Calling help() and dir() in the Labs, Pre, and OA:
  111. help(str) # whole help doc on strings
  112. print(dir(str)) # dir() returns a list, fyi
  113. help(str.find) # so see help on just this method
  114.  
  115. # Want to weed out the "__" special attributes?
  116. for item in dir(str):
  117.     if not item.startswith("_"):
  118.         print(item)
  119.  
  120. # help() and dir() are your friends!
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
Add Comment
Please, Sign In to add comment