Advertisement
jspill

webinar-for-loops-2022-04-09

Apr 9th, 2022
1,191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.61 KB | None | 0 0
  1. # 2022 Apr 9
  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. # Basic syntax of a for loop
  12. # for ___ in __someContainer__:
  13.  
  14. myList = ["Agent Scully", "Agent Mulder", "Walter Skinner", "CSM", "Mr. X"]
  15. for item in myList:
  16.     print(item)
  17.  
  18. myTuple = ("Gilligan", "Castaway002", "red", "crew") # return x, y
  19. for item in myTuple:
  20.     print(item) # print(end="\n")
  21.  
  22. myString = "Just sit right back and you'll hear a tale."
  23. for char in myString:
  24.     print(char, end="-")
  25. print() # if you use end param of print(), print() again to get a clean line
  26.  
  27. # looping over dictionaries
  28. scoobiesDCT = { # key: value pairs
  29.     "Scooby": "a blue collar",
  30.     "Shaggy": "green",
  31.     "Velma": "orange",
  32.     "Daphne": "purple",
  33.     "Fred": "an ascot"
  34. }
  35. # you can use the dict KEY to get its VALUE
  36. # myDictionary[key] # retrieve the value for that key... kinda like dict.get()
  37. # myDictionary[key] = value # set a new value, kinda like dict.update()
  38.  
  39. for key in scoobiesDCT: # for loop var with dicts is the KEY
  40.     value = scoobiesDCT[key]
  41.     print("{} wears {}.".format(key, value))
  42.  
  43. # loop some number of times... with range()
  44. # for n in 5: # can't do that! 5 is not iterable.
  45. for n in range(0, 5):
  46.     print(n)
  47.  
  48. # using range() to look at index of list items
  49. myList.append("Krycek")
  50. for i in range(len(myList)):
  51.     print("{}: {}".format(i, myList[i]))
  52. for i, item in enumerate(myList):
  53.     print("{}--> {}".format(i, item))
  54.  
  55. # CA 6.8.2
  56. # Given num_rows and num_cols, print a list of all seats in a theater. Rows are numbered, columns lettered, as in 1A or 3E. Print a space after each seat.
  57. # Sample output with inputs: 2 3
  58. # 1A 1B 1C 2A 2B 2C
  59.  
  60. num_rows = 2 # int(input())
  61. num_cols = 3 # int(input())
  62.  
  63. # x += 1
  64. # string incrementing
  65. # ord("a") + 1
  66. # chr(ord("a") + 1) # --< "b"
  67.  
  68. for row in range(1, num_rows+1):
  69.     ltr = "A"
  70.     for col in range(0, num_cols):
  71.      print("{}{}".format(row, ltr), end=" ")
  72.      ltr = chr(ord(ltr) + 1)
  73. print()
  74.  
  75. for i in range(0, 513):
  76.     print("{}: {}".format(i, chr(i)))
  77.  
  78. with open("mock_data.csv", "r") as f:
  79.     # read() --> returns whole file as one big str
  80.     # readlines() --> returns you a line by line list of strings
  81.     contents = f.readlines()
  82. # print(contents)
  83. for line in contents:
  84.     line = line.rstrip()
  85.     # print(line)
  86.     lineList = line.split(",")
  87.     # print(lineList[3])
  88.     print("{} is coming from: {}".format(lineList[3], lineList[4]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement