Advertisement
tuomasvaltanen

Untitled

Oct 12th, 2023 (edited)
946
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.51 KB | None | 0 0
  1. # lecture 6, 12.10.2023, collections more loops today!
  2. print("Welcome!")
  3.  
  4. # NEW FILE
  5.  
  6. # a list of products, 4 in total
  7. products = ["Washing machine", "Coffee maker", "Freezer", "Fridge"]
  8.  
  9. # this is more or less only for testing/debug purposes
  10. # only to be used by the coder
  11. # don't show this to actual user
  12. print(products)
  13.  
  14. # NEW FILE
  15.  
  16. # a list of products, 4 in total
  17. products = ["Washing machine", "Coffee maker", "Freezer", "Fridge"]
  18.  
  19. # this is more or less only for testing/debug purposes
  20. # only to be used by the coder
  21. # don't show this to actual user
  22. # print(products)
  23.  
  24. # let's print the product in index 2 (which is the 3rd product)
  25. text = products[2]
  26. print(text)
  27.  
  28. # NEW FILE
  29.  
  30. # a list of products, 4 in total
  31. products = ["Washing machine", "Coffee maker", "Freezer", "Fridge"]
  32.  
  33. # ask user which product they want to see (index)
  34. choice = input("Which product do you wish to see?\n")
  35. choice = int(choice)
  36.  
  37. # based on the choice-variable, show a product
  38. # so if user gives 2, this is same as products[2]
  39. text = products[choice]
  40. print(text)
  41.  
  42. # NEW FILE
  43.  
  44. # a list of products, 4 in total
  45. products = ["Washing machine", "Coffee maker", "Freezer", "Fridge", "Toothbrush", "Microwave"]
  46.  
  47. # ask user which product they want to see (index)
  48. choice = input("Which product do you wish to see?\n")
  49. choice = int(choice)
  50.  
  51. # let's save the information of how many products
  52. # we have into a variable
  53. amount = len(products)
  54.  
  55. # if user gave an index, that is less than the max amount of products
  56. if choice < amount and choice >= 0:
  57.     # based on the choice-variable, show a product
  58.     # so if user gives 2, this is same as products[2]
  59.     text = products[choice]
  60.     print(text)
  61. else:
  62.     print("No product with given index.")
  63.  
  64. # NEW FILE
  65.  
  66. # a list of products, you can also put this in multiple lines
  67. products = ["Washing machine", "Coffee maker",
  68.             "Freezer", "Fridge", "Toothbrush",
  69.             "Microwave", "Bookshelf"]
  70.  
  71. # use a for loop to go through all the data in the products list
  72. # starts with 1st element, ends with last element
  73. # this always works, regardless of number of products
  74. for p in products:
  75.     print(p)
  76.  
  77. # VERSION 2, on same line on one text variable
  78.  
  79. # a list of products, you can also put this in multiple lines
  80. products = ["Washing machine", "Coffee maker",
  81.             "Freezer", "Fridge", "Toothbrush",
  82.             "Microwave", "Bookshelf"]
  83.  
  84. text = ""
  85.  
  86. # use a for loop to go through all the data in the products list
  87. # starts with 1st element, ends with last element
  88. # this always works, regardless of number of products
  89. for p in products:
  90.     text = text + p + ", "
  91.  
  92. print(text)
  93.  
  94. # NEW FILE
  95.  
  96. # a list of products, you can also put this in multiple lines
  97. products = ["Washing machine", "Coffee maker",
  98.             "Freezer", "Fridge", "Toothbrush",
  99.             "Microwave", "Bookshelf"]
  100.  
  101. # how many products we have
  102. amount = len(products)
  103.  
  104. # traditional for-loop with range
  105. # use the amount of products in the range
  106. # index contains the active index
  107. # p-variable: get a certain product with active index
  108. for index in range(amount):
  109.     p = products[index]
  110.     print(f"{index + 1}. {p}")
  111.  
  112.  
  113. # NEW FILE
  114.  
  115. # let's create a tuple, notice, normal parentheses
  116. weekdays = ("Monday", "Tuesday", "Wednesday", "Thursday",
  117.             "Friday", "Saturday", "Sunday")
  118.  
  119. # ask user for the index of the day to show
  120. choice = input("Which days would you like to see?\n")
  121.  
  122. # this way we can easily match the user's number
  123. # to the actual index (lists start at 0, not 1)
  124. choice = int(choice) - 1
  125.  
  126. # get the corresponding day and print it
  127. text = weekdays[choice]
  128. print(text)
  129.  
  130. # NEW FILE
  131.  
  132. # a list of products, you can also put this in multiple lines
  133. products = ["Washing machine", "Coffee maker",
  134.             "Freezer", "Fridge", "Toothbrush",
  135.             "Microwave", "Bookshelf"]
  136.  
  137. # change the value of product in index 3 to Smartphone
  138. products[3] = "Smartphone"
  139.  
  140. # we can now see that 4th product changed (index 3)
  141. print(products)
  142.  
  143. # NEW FILE
  144.  
  145. # a list of products, you can also put this in multiple lines
  146. products = ["Washing machine", "Coffee maker",
  147.             "Freezer", "Fridge", "Toothbrush",
  148.             "Microwave", "Bookshelf"]
  149.  
  150. # ask the user for which product to change
  151. choice = input("Which product to change?\n")
  152. choice = int(choice)
  153.  
  154. # ask the user what is the replacing product
  155. replacement = input("What is the replacement product?\n")
  156.  
  157. # replace this product in this index
  158. # with the user's new value
  159. products[choice] = replacement
  160.  
  161. # test if everything worked
  162. print(products)
  163.  
  164.  
  165. # NEW FILE
  166.  
  167. # a single dictionary, containing
  168. # all information regarding this one person
  169. # in one "variable"
  170. person = {
  171.     "name": "Test Person",
  172.     "age": 33,
  173.     "city": "Rovaniemi"
  174. }
  175.  
  176. # use this only for testing purposes
  177. # this prints everything, even secrets or sensitive information
  178. # print(person)
  179.  
  180. # printing from dictionary is done via the key
  181. # so to get the person's name, use the key "name"
  182. print("Person's name:")
  183. print(person["name"])
  184. print()
  185.  
  186. print("Person's age:")
  187. print(person["age"])
  188. print()
  189.  
  190. # NEW FILE
  191.  
  192. # a single dictionary, containing
  193. # all information regarding this one person
  194. # in one "variable"
  195. person = {
  196.     "name": "Test Person",
  197.     "age": 33,
  198.     "city": "Rovaniemi"
  199. }
  200.  
  201. # use this only for testing purposes
  202. # this prints everything, even secrets or sensitive information
  203. # print(person)
  204.  
  205. # printing from dictionary is done via the key
  206. # so to get the person's name, use the key "name"
  207. print("Person's name:")
  208. print(person["name"])
  209. print()
  210.  
  211. print("Person's age:")
  212. print(person["age"])
  213. print()
  214.  
  215. # NEW FILE
  216.  
  217. # some product identifier
  218. codes = ["ORDER142_A1567_2023", "ORDER5678142_A15674242_2023"]
  219.  
  220. # go through all codes
  221. # and split them into parts
  222. for code in codes:
  223.     # split by underscore to a list
  224.     parts = code.split("_")
  225.     first = parts[0]
  226.     second = parts[1]
  227.     year = parts[2]
  228.     print(first)
  229.     print(second)
  230.     print(year)
  231.     print()
  232.  
  233. print("ALL DONE")
  234.  
  235. # NEW FILE
  236.  
  237. # list of cities
  238. cities = ["rovaniemi", "oulu", "helsinki", "stockholm", "madrid", "oslo"]
  239.  
  240. # make two empty lists, one for short city names, and another for long ones
  241. long_cities = []
  242. short_cities = []
  243.  
  244. # loop through the cities, and place them
  245. # in either of the lists or "buckets" based on the
  246. # length of the city name
  247. for city in cities:
  248.     if len(city) < 6:
  249.         short_cities.append(city)
  250.     else:
  251.         long_cities.append(city)
  252.  
  253. # let's test what is inside the lists now
  254. print(long_cities)
  255. print(short_cities)
  256.  
  257. # NEW FILE
  258.  
  259. # combining lists is very easy in Python
  260. foods = ["banana", "apple", "cherry"]
  261. drinks = ["water", "tea", "coffee"]
  262.  
  263. # combine the two lists
  264. everything = foods + drinks
  265.  
  266. # test out the values
  267. print(everything)
  268.  
  269. # NEW FILE
  270.  
  271. # list of grades
  272. # you can also create this list based on user inputs (for-loop)
  273. grades = [7, 8, 9, 9, 10, 7, 6, 8, 8, 10]
  274.  
  275. # average = sum / count
  276. total = sum(grades)
  277. amount = len(grades)
  278.  
  279. # calculate the average
  280. average = total / amount
  281. average = round(average, 1)
  282. print(average)
  283.  
  284. # NEW FILE
  285.  
  286. cities = ["Rovaniemi", "Helsinki", "Athens", "mikkeli", "Stockholm", "Madrid"]
  287.  
  288. print("Original order:")
  289. print(cities)
  290.  
  291. #cities.sort()
  292. cities = sorted(cities)
  293.  
  294. print("Sorted order, cities.sort() / sorted(cities):")
  295. print(cities)
  296.  
  297. # sort with lambda so, that all letters are converted to
  298. # uppercase when comparing them to each other
  299. cities.sort(key=lambda v:v.upper())
  300.  
  301. print("Sorted with lambda:")
  302. print(cities)
  303.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement