Advertisement
jspill

webinar-for-loops-2021-08-07

Aug 7th, 2021
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.81 KB | None | 0 0
  1. # WEBINAR: For Loops 2021 Aug 7
  2. # We use loops to repeat actions
  3.  
  4. # a WHILE loop is basically an IF statement
  5. # that repeats as long as its condition is true
  6.  
  7.  
  8. # FOR LOOPS are used for repeating actions for every element
  9. # in a container like a list, string, tuple, etc...
  10.  
  11. # Focus on FOR LOOPS for the OA (don't worry about while loops so much)
  12. # Don't worry about nested loops either :), just one loop at a time
  13.  
  14. # BASIC LOOP syntax with a list
  15. # for __ in __:
  16. # for _myVar_ in _someContainer_:
  17. myList = ["Agent Scully", "Agent Mulder", "Walter Skinner", "CSM", "Mr. X"] # X-Files characters
  18. for item in myList:
  19.     print(item)
  20.  
  21. myString = "Just sit right back and you'll hear a tale."
  22. # for char in myString:
  23. #     print(char, end=" ")
  24. # print("Be careful on the next call.")
  25.  
  26. # tuple
  27. myTuple = ("Gilligan", "Castaway002", "red", "crew") # things about Gilligan
  28. for item in myTuple:
  29.     print(item)
  30.  
  31. # dictionaries
  32. scoobiesDCT = {
  33.     "Scooby": "a blue collar",
  34.     "Shaggy": "green",
  35.     "Velma": "orange",
  36.     "Daphne": "purple",
  37.     "Fred": "an ascot"
  38. }
  39. for key in scoobiesDCT:
  40.     # nameOfDictionary[key] = value
  41.     print(key, scoobiesDCT[key])
  42. for k, v in scoobiesDCT.items():
  43.     # print(k + " always wears " + v + ".") # string CONCATENATION
  44.     # print("%s always wears %s" % (k, v)) # data conversion specifiers/string modulo
  45.     print("{} always wears {}.".format(k, v)) # string class .format() method - BEST!
  46.  
  47. for n in range(0, 5):
  48.     print(n)
  49.  
  50. for i in range(len(myList)):
  51.     item = myList[i]
  52.     # print(i, myList[i])
  53.     print(i, item)
  54.  
  55. for i, item in enumerate(myList):
  56.     print(i, item)
  57.  
  58. # break - ends the loop entirely
  59. # continue - end the CURRENT ITERATION of the loop
  60. # for n in range(0, 5):
  61. #   if n == 3:
  62. #       # break # 0 1 2 and we're done
  63. #       continue # 0 1 2 4
  64. #   print(n)
  65.  
  66. myString = "Just sit right back and you'll hear a tale."
  67. # IN keyword
  68. if "back" in myString:
  69.     print("It's there!")
  70. print(myString.find("back")) # 15
  71. print(myString.count("back")) # 1
  72. # for item in myList:
  73.  
  74. # Ch 8 Task 10
  75. # Complete the function to return the number of upper case letters in the given string
  76. def countUpper(mystring):
  77.     # string method isupper()
  78.     count = 0
  79.     for char in mystring:
  80.         if char.isupper():
  81.             count += 1
  82.     return count
  83.  
  84. print(countUpper('Welcome to WGU'))# expected output: 4
  85. print(countUpper('Hello Mary'))# expected output: 2
  86.  
  87. # Question:
  88. # How to look up class/type methods on the exam...
  89.  
  90. # help(str)# outputs the entire help document on something
  91. # help(list) # etc
  92. # print(dir(str)) # dir() returns a list of just attribute (method and property) names
  93. # exam has a Ref List for imported modules like math, random, os, datetime
  94.  
  95. # Since dir() returns a list, you can loop over it!
  96. for item in dir(str):
  97.     if not item.startswith("_"): # ignore the __methods()__ for this course
  98.         print(item)
  99.  
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement