Advertisement
tuomasvaltanen

Untitled

Nov 7th, 2024 (edited)
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.18 KB | None | 0 0
  1. # lecture 7, collections and loops, 7.11.2024
  2. print("Welcome!")
  3.  
  4. # NEW FILE
  5.  
  6. # create a list of products
  7. products = ["Washing machine", "Coffee maker", "Freezer", "Fridge"]
  8.  
  9. # use the raw format of the list printing
  10. # only to quickly see what is inside
  11. # in other words, only for debugging purposes
  12. # this is not the format the end-user / client wants to see
  13. # print(products)
  14.  
  15. # get a single element from the list
  16. # first index is 0, second is 1 etc.
  17. # always -1 of what you're seeking
  18. text = products[2]
  19. print(text)
  20.  
  21. # NEW FILE
  22.  
  23. # create a list of products
  24. products = ["Washing machine", "Coffee maker", "Freezer", "Fridge"]
  25.  
  26. # ask the index from the user
  27. choice = input("Which product do you wish to see?\n")
  28. choice = int(choice)
  29.  
  30. # we can also place a integer variable
  31. # as the index! notice how we don't have a single conditional
  32. # statement in this code this is one of the reasons why professionals
  33. # use collections all the time
  34. text = products[choice]
  35. print(text)
  36.  
  37. # NEW FILE
  38.  
  39. # create a list of products
  40. products = ["Washing machine", "Coffee maker", "Freezer", "Fridge", "Toothbrush", "Microwave"]
  41.  
  42. # ask the index from the user
  43. choice = input("Which product do you wish to see?\n")
  44. choice = int(choice)
  45.  
  46. # get the amount of products into a variable
  47. # if you have 4 products, this will be 4
  48. amount = len(products)
  49.  
  50. # let's check if user gave a possible index
  51. # in other words, 0 - 3 (if we have 4 products)
  52. # another common way is to use try-except
  53. if choice >= 0 and choice < amount:
  54.     # get the product the user wants
  55.     text = products[choice]
  56.     print(text)
  57. else:
  58.     print("No product with given index.")
  59.  
  60. # NEW FILE
  61.  
  62. # create a list of products
  63. products = ["Washing machine", "Coffee maker", "Freezer",
  64.             "Fridge", "Toothbrush", "Microwave", "Bookshelf",
  65.             "Oven", "Monitor", "Mouse", "Table"]
  66.  
  67.  
  68. # loop through your products
  69. for single_product in products:
  70.     print(single_product)
  71.  
  72. # NEW FILE, VERSION 2
  73.  
  74. # create a list of products
  75. products = ["Washing machine", "Coffee maker", "Freezer",
  76.             "Fridge", "Toothbrush", "Microwave", "Bookshelf",
  77.             "Oven", "Monitor", "Mouse", "Table"]
  78.  
  79. # let's build a text variable in the loop
  80. text = ""
  81.  
  82. # loop through your products
  83. # you can also do this with ,join() -function
  84. for p in products:
  85.     text = text + p + ", "
  86.  
  87. # show end result
  88. print(text)
  89.  
  90.  
  91. # NEW FILE
  92. # create a list of products
  93. products = ["Washing machine", "Coffee maker", "Freezer",
  94.             "Fridge", "Toothbrush", "Microwave", "Bookshelf",
  95.             "Oven", "Monitor", "Mouse", "Table"]
  96.  
  97. # always get the amount of products BEFOREHAND
  98. # into a variable this is because, most of the
  99. # programming languages will re-calculate the
  100. # amount of products on each iteration
  101. amount = len(products)
  102.  
  103. # if you have 11 products,
  104. # this is the same for x in range(11) etc.
  105. for index in range(amount):
  106.     product_name = products[index]
  107.     print(f"{index + 1} - {product_name}")
  108.  
  109.  
  110. # NEW FILE
  111.  
  112. # create a list of products
  113. products = ["Washing machine", "Coffee maker", "Freezer",
  114.             "Fridge", "Toothbrush", "Microwave", "Bookshelf",
  115.             "Oven", "Monitor", "Mouse", "Table"]
  116.  
  117. # always get the amount of products BEFOREHAND
  118. # into a variable this is because, most of the
  119. # programming languages will re-calculate the
  120. # amount of products on each iteration
  121. amount = len(products)
  122.  
  123. # if you have 11 products,
  124. # this is the same for x in range(11) etc.
  125. # don't use len() straight in the range()
  126. # of the for-loop, (e.g. for index in range(len(products)):)
  127. # because this way the loop calculates the amount of data
  128. # after each cycle
  129. # so if you have 100 products => 100 recalculations
  130. for index in range(amount):
  131.     product_name = products[index]
  132.     print(f"{index + 1} - {product_name}")
  133.  
  134. # NEW FILE
  135.  
  136. # let's create a tuple for weekdays
  137. # remember normal parentheses in tuples
  138. weekdays = ("Monday", "Tuesday", "Wednesday",
  139.             "Thursday", "Friday", "Saturday", "Sunday")
  140.  
  141. # note: you can't change values in a tuple
  142. # e.g. weekdays[3] = "Someday" will not work
  143. # if you need to change something, re-create the whole tuple
  144. # or just use a list instead
  145.  
  146. # remember to reduce 1 from the user's input
  147. # so it matches with the -1 logic of the collection index
  148. choice = input("Which weekday would you like to know?\n")
  149. choice = int(choice) - 1
  150.  
  151. # print out the user's weekday
  152. print(weekdays[choice])
  153.  
  154. # NEW FILE
  155.  
  156. # create a list of products
  157. products = ["Washing machine", "Coffee maker", "Freezer",
  158.             "Fridge", "Toothbrush", "Microwave", "Bookshelf",
  159.             "Oven", "Monitor", "Mouse", "Table"]
  160.  
  161. # let's change the value of 4th product
  162. products[3] = "Pogo stick"
  163.  
  164. # let's quickly see what's inside now
  165. # we can see now the 4th value
  166. # is now Pogo stick instead of Fridge
  167. print(products)
  168.  
  169. # NEW FILE
  170.  
  171. # create a list of products
  172. products = ["Washing machine", "Coffee maker", "Freezer",
  173.             "Fridge", "Toothbrush", "Microwave", "Bookshelf",
  174.             "Oven", "Monitor", "Mouse", "Table"]
  175.  
  176. # ask the user WHICH product to replace
  177. choice = input("Which product would you like to change?\n")
  178. choice = int(choice)
  179.  
  180. # new value for the chosen product
  181. new_product = input("What is the name of the replacement product?\n")
  182.  
  183. # replace the product chosen by user
  184. # with the value given by user
  185. # remember to use if/else or try/except
  186. # if the user tries to modify a non-existing index
  187. products[choice] = new_product
  188.  
  189. # let's quickly see what's inside now
  190. print(products)
  191.  
  192. # NEW FILE
  193.  
  194. # create a dictionary
  195. # containing all information regarding this
  196. # on person in one "variable"
  197. person = {
  198.     "name": "Test Person",
  199.     "age": 33,
  200.     "city": "Rovaniemi"
  201. }
  202.  
  203. # only print the whole dictionary
  204. # for testing purposes, because otherwise
  205. # you might print out sensitive information to the user
  206. # passwords, session data etc.
  207. # print(person)
  208. # print()
  209.  
  210. # print the name of the person
  211. print("Person's name:")
  212. print(person['name'])
  213.  
  214. # print the name of the person
  215. print("\nPerson's age:")
  216. print(person['age'])
  217.  
  218. # NEW FILE
  219.  
  220. products = ["ORDER143_A7685_2024", "ORDER254346564642_A68575465464643643_2023"]
  221.  
  222. # don't use substring for splitting down a string like this, use split()
  223. # since it doesn't matter what length each section has, it always works
  224. for product in products:
  225.     parts = product.split("_")
  226.  
  227.     order = parts[0]
  228.     middle = parts[1]
  229.     year = parts[2]
  230.  
  231.     print(order)
  232.     print(middle)
  233.     print(year)
  234.     print()
  235.  
  236. # NEW FILE
  237.  
  238. # TRY THIS IN PYTHON TUTOR
  239. # list of cities
  240. cities = ["rovaniemi", "oulu", "helsinki", "stockholm", "madrid", "oslo"]
  241.  
  242. # make two empty lists to reserve future data
  243. # one list for long city names, and one for short city names
  244. long_cities = []
  245. short_cities = []
  246.  
  247. # loop through cities, and SORT THEM into
  248. # one of the lists above based on their length
  249. for city in cities:
  250.     if len(city) < 6:
  251.         short_cities.append(city)
  252.     else:
  253.         long_cities.append(city)
  254.  
  255. # print the results for both lists
  256. print(short_cities)
  257. print(long_cities)
  258.  
  259. # NEW FILE
  260.  
  261. # combining lists
  262. foods = ["banana", "apple", "cherry"]
  263. drinks = ["water", "tea", "coffee"]
  264.  
  265. # combine into one list
  266. everything = foods + drinks
  267.  
  268. # print and see content
  269. print(everything)
  270.  
  271. # NEW FILE
  272.  
  273. # this is a very good example, why collections are super powerful
  274.  
  275. # list of grades
  276. grades = [2, 5, 4, 3, 5, 4, 3, 2, 4, 5, 1, 3]
  277.  
  278. # average = total / amount of data
  279. total = sum(grades)
  280. amount = len(grades)
  281.  
  282. # calculate the average
  283. average = total / amount
  284. average = round(average, 1)
  285. print(f"Average grade: {average}")
  286.  
  287. # NEW FILE
  288.  
  289. # list of cities
  290. cities = ["Rovaniemi", "Helsinki", "Athens", "mikkeli", "Stockholm", "Madrid"]
  291.  
  292. print("Original order:")
  293. print(cities)
  294.  
  295. # sort the list
  296. # cities.sort()
  297.  
  298. # sorted() might be more usable, since it doesn't
  299. # change the original list
  300. # sorted_cities = sorted(cities)
  301.  
  302. # lambda here will transfrom each city name
  303. # into all CAPITALS, so when comparing
  304. # they are correctly place into alphabetical order
  305. # v here is the temporary variable created by lambda
  306. cities.sort(key=lambda v: v.upper())
  307.  
  308. print("\nSorted order:")
  309. print(cities)
  310.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement