Advertisement
jspill

webinar-for-loops-2021-09-11

Sep 11th, 2021
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.46 KB | None | 0 0
  1. # WEBINAR: For Loops
  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. # FOR LOOPS are used for repeating actions for every element
  8. # in a container like a list, string, tuple, etc...
  9.  
  10. # Focus on FOR LOOPS for the OA (don't worry about while loops so much)
  11. # Don't worry about nested loops either :), just one loop at a time
  12.  
  13. # BASIC LOOP syntax with a list
  14. # for __ in ___:
  15. # for _myVar_ in _someContainer_:
  16. myList = ["Agent Scully", "Agent Mulder", "Walter Skinner", "CSM", "Mr. X"] # X-Files characters
  17. for item in myList:
  18.     print(item)
  19.  
  20. # tuple
  21. myTuple = ("Gilligan", "Castaway002", "red", "crew") # things about Gilligan
  22. for item in myTuple:
  23.     print(item)
  24.  
  25. myString = "Just sit right back and you'll hear a tale."
  26. # for char in myString:
  27. #   print(char)
  28.  
  29. # dict
  30. scoobiesDCT = {
  31.     "Scooby": "a blue collar",
  32.     "Shaggy": "green",
  33.     "Velma": "orange",
  34.     "Daphne": "purple",
  35.     "Fred": "an ascot"
  36. }
  37. # nameOfDict[key] = value
  38. for key in scoobiesDCT:
  39.     v = scoobiesDCT[key]
  40.     print(key, "-->", v)
  41.  
  42. # range() turns numbers into an iterable container
  43. for n in range(0, 5): # integers aren't iterable, but range objects are
  44.     print(n)
  45.  
  46. # Getting the index of a list or other container's values
  47. # 2 ways to do this...
  48. # range() + len()
  49. for i in range(len(myList)):
  50.     item = myList[i]
  51.     print(i, ":", myList[i])
  52. # enumerate()
  53. for i, item in enumerate(myList):
  54.     print(i, ">>", item)
  55.  
  56.  
  57. # examples and student questions...
  58. # Ch 8 Task 10
  59. # Complete the function to return the number of upper case letters in the given string
  60. def countUpper(mystring):
  61.     # string method isupper()
  62.     count = 0
  63.     for letter in mystring:
  64.         if letter.isupper():
  65.             # print(letter)
  66.             count += 1
  67.     return count
  68.  
  69. print(countUpper('Welcome to WGU'))# expected output: 4
  70. print(countUpper('Hello Mary'))# expected output: 2
  71.  
  72. # using help() or dir() in test system
  73. def myFunction(x, y, z):
  74.     # help(str)
  75.     h = help(str)
  76.     return h
  77.     # return x + y + z
  78.  
  79. print(myFunction(1, 2, 3))
  80. print(myFunction(4, 5, 6))
  81.  
  82. # Ch 12 Task 5
  83. import os
  84. # Complete the function to print all files in the given directory
  85. def printFiles(someDirectory):
  86.     # return os.listdir(someDirectory) # wrong! b/c that's a list
  87.     # fileList = os.listdir(someDirectory)
  88.     for item in os.listdir(someDirectory):
  89.         print(item)
  90.  
  91. # expected output: main.py # <--- ALWAYS watch the output! It's not a list!
  92. printFiles(os.getcwd())
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement