tuomasvaltanen

Untitled

Oct 26th, 2022 (edited)
1,020
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.70 KB | None | 0 0
  1. # lecture 6, collections!
  2. print("Welcome!")
  3.  
  4. # NEW FILE
  5.  
  6. # a list of 4 products
  7. products = ["Washing machine", "Coffee maker", "Freezer", "Fridge"]
  8.  
  9. # access the second element
  10. # first index starts from zero!
  11. p = products[1]
  12.  
  13. # print it out
  14. print(p)
  15.  
  16. # NEW FILE
  17.  
  18. # a list of 4 products
  19. products = ["Washing machine", "Coffee maker", "Freezer", "Fridge"]
  20.  
  21. # ask the index from the user
  22. choice = input("Which product do you want to see?\n")
  23. choice = int(choice)
  24.  
  25. # get the product with given index
  26. p = products[choice]
  27. print(p)
  28.  
  29. # NEW FILE
  30.  
  31. # a list of 4 products
  32. products = ["Washing machine", "Coffee maker", "Freezer", "Fridge", "Oven"]
  33.  
  34. # ask the index from the user
  35. choice = input("Which product do you want to see?\n")
  36. choice = int(choice)
  37.  
  38. # get the amount of elements in the list (products)
  39. amount = len(products)
  40.  
  41. # the given index has to be between 0 and amount of products
  42. if choice < amount and choice >= 0:
  43.     # get the product with given index
  44.     p = products[choice]
  45.     print(p)
  46. else:
  47.     print("That product does not exist! (bad index)")
  48.  
  49. # NEW FILE
  50.  
  51. # a list of 4 products
  52. products = ["Washing machine", "Coffee maker", "Freezer", "Fridge", "Oven"]
  53.  
  54. # loop through the items in list
  55. for p in products:
  56.     print(p)
  57.    
  58. # try also:
  59. # print(f"Product name: {p}")
  60.  
  61. # NEW FILE
  62.  
  63. # a list of 5 products
  64. products = ["Washing machine", "Coffee maker", "Freezer", "Fridge", "Oven", "Clock"]
  65.  
  66. amount = len(products)
  67.  
  68. # loop through products and print a numbered list
  69. # index contains the index number
  70. # p contains the actual product
  71. for index in range(amount):
  72.     p = products[index]
  73.     print(f"{index + 1}. {p}")
  74.  
  75. # NEW FILE
  76. # tuple uses normal parentheses
  77. weekdays = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
  78.  
  79. # ask the number from user
  80. choice = input("Which weekday?\n")
  81.  
  82. # collections start at 0, but user might
  83. # want to user the actual numbers, therefore - 1
  84. # for example, if user gives 3, we transform it to 2,
  85. # which is then, Wednesday
  86. choice = int(choice) - 1
  87.  
  88. # print out the required weekday
  89. print(weekdays[choice])
  90.  
  91. # NEW FILE
  92.  
  93. # a list of 5 products
  94. products = ["Washing machine", "Coffee maker", "Freezer", "Fridge", "Oven", "Clock"]
  95.  
  96. # change the 3rd element to Television
  97. products[2] = "Television"
  98.  
  99. # let's quickly see if this worked:
  100. # print(products)
  101.  
  102. # ask user the index that we want to change
  103. choice = input("Which product do you want to change?\n")
  104. choice = int(choice)
  105.  
  106. # ask user what is the replacement value
  107. new_product = input("What is the new product?\n")
  108.  
  109. # change the actual product
  110. products[choice] = new_product
  111.  
  112. # test quickly what is inside the list now
  113. # use print(products) only for testing, for final
  114. # code, always use indexes or print in a loop
  115. print(products)
  116.  
  117. # NEW FILE
  118.  
  119. # person-dictionary, consists of 3 different data: name, age and city
  120. person = {
  121.     "name": "John Doe",
  122.     "age": 33,
  123.     "city": "Rovaniemi"
  124. }
  125.  
  126. # print out only the person name
  127. print("Person name:")
  128. print(person["name"])
  129.  
  130. print()
  131.  
  132. # print out only the person age
  133. age = person['age']
  134.  
  135. print("Person age:")
  136. print(f"{age} years old.")
  137.  
  138. # NEW FILE
  139.  
  140. day_1 = [0.5, 1.4, 2.5, 3.4]
  141. day_2 = [1.2, 1.1, 2.8, 3.9]
  142. day_3 = [0.2, 2.4, 1.1, 0.7]
  143.  
  144. # list of lists, contains three days
  145. # each day contains 4 temperatures
  146. temperatures = [day_1, day_2, day_3]
  147.  
  148. # loop through all days in list (3 days in total)
  149. for day in temperatures:
  150.     print("\nNew day!")
  151.  
  152.     # loop through all temperatures in one day (4 temperatures in total)
  153.     for t in day:
  154.         print(t)
  155.  
  156.  
  157. print("All done!")
  158.  
  159. # NEW FILE
  160.  
  161. # try this version in Python Tutor to see what happens in the background!
  162. temperatures = [[0.5, 1.4, 2.5, 3.4], [1.2, 1.1, 2.8, 3.9], [0.2, 2.4, 1.1, 0.7]]
  163.  
  164. # loop through all days in list (3 days in total)
  165. for day in temperatures:
  166.     print("\nNew day!")
  167.  
  168.     # loop through all temperatures in one day (4 temperatures in total)
  169.     for t in day:
  170.         print(t)
  171.  
  172.  
  173. print("All done!")
  174.  
  175. # NEW FILE
  176.  
  177. book = {
  178.     "name": "My Lady Jane",
  179.     "year": 2016,
  180.     "publisher": {
  181.         "name": "HarperTeen",
  182.         "organization": "HarperCollins Publishers",
  183.         "location": "New York"
  184.     }
  185. }
  186.  
  187. # name and year are simple, just get the data
  188. name = book['name']
  189. print(name)
  190.  
  191. year = book['year']
  192. print(year)
  193.  
  194. # publisher is another dictionary, so we need to access it with chained
  195. # accessors
  196.  
  197. # approach 1, use helper variables
  198. publisher = book["publisher"]
  199. publisher_location = publisher['location']
  200. print(publisher_location)
  201.  
  202. # approach 2, get the directly
  203. publisher_location = book['publisher']['location']
  204. print(publisher_location)
  205.  
  206. # NEW FILE
  207.  
  208. text = "HOTEL"
  209.  
  210. # a string is basically a list of characters in certain order!
  211. for character in text:
  212.     print(character)
  213.  
  214. # NEW FILE
  215.  
  216. # try replacing text with "ORDER1412332_C156786_2022"
  217. # you'll notice the substring logic will break!
  218. text = "ORDER1432_C1786_2022"
  219.  
  220. # get the pieces of order!
  221. order = text[0:9]
  222. print(order)
  223.  
  224. customer = text[10:15]
  225. print(customer)
  226.  
  227. year = text[-4:]
  228. print(year)
  229.  
  230. # NEW FILE, BETTER WAY, USE SPLIT
  231.  
  232. # try replacing text with "ORDER1412332_C156786_2022"
  233. # you'll notice the substring logic will break!
  234. text = "ORDER1423132_C17123286_2022"
  235.  
  236. # remember, parts is a LIST of three elements
  237. # by using split(), it doesn't matter how long
  238. # different parts are! this works always!
  239. parts = text.split("_")
  240.  
  241. # first part is the order code
  242. order = parts[0]
  243. print(order)
  244.  
  245. # second part is the client id
  246. customer = parts[1]
  247. print(customer)
  248.  
  249. # third part is the year
  250. year = parts[2]
  251. print(year)
  252.  
  253. # NEW FILE
  254.  
  255. # TRY THIS EXAMPLE IN PYTHON TUTOR!
  256.  
  257. cities = ["rovaniemi", "oulu", "helsinki", "stockholm", "madrid", "london", "amsterdam"]
  258.  
  259. # initialize two empty lists
  260. short_city_names = []
  261. long_city_names = []
  262.  
  263. # loop through all cities
  264. for city in cities:
  265.     # if city name is longer than 6 letters => add it to long city names
  266.     if len(city) > 6:
  267.         long_city_names.append(city)
  268.     else:
  269.         # otherwise add it to short city names
  270.         short_city_names.append(city)
  271.  
  272. # let's see what's inside now
  273. print(long_city_names)
  274. print(short_city_names)
  275.  
  276. # NEW FILE
  277.  
  278. cities = ["oulu", "helsinki", "stockholm", "rovaniemi", "madrid", "london", "amsterdam"]
  279.  
  280. # loop through all cities
  281. for city in cities:
  282.     # when the first long city name is found, break out of loop
  283.     if len(city) > 8:
  284.         print("The first long city name we found, was:")
  285.         print(city)
  286.         break
  287.  
  288. # NEW FILE
  289.  
  290. cities = ["oulu", "helsinki", "stockholm", "rovaniemi", "madrid", "london", "amsterdam"]
  291.  
  292. # loop through all cities
  293. for city in cities:
  294.     # if the city name is long, skip the cycle
  295.     if len(city) > 8:
  296.         continue
  297.  
  298.     print(city)
  299.  
  300. # NEW FILE
  301.  
  302. # combining lists is very easy in Python
  303. foods = ["banana", "apple", "cherry"]
  304. drinks = ["water", "tea", "coffee"]
  305.  
  306. everything = foods + drinks
  307. print(everything)
  308.  
  309. # NEW FILE
  310.  
  311. grades = [1, 3, 5, 4, 3, 4, 3, 2, 5, 5, 4, 3, 5, 5, 5, 5, 5]
  312.  
  313. # get the total sum and amount of values
  314. total = sum(grades)
  315. amount = len(grades)
  316.  
  317. # average => total sum of values / number of values
  318. # if you use lists, you can do neat tricks like this:
  319. average = total / amount
  320. average = round(average, 1)
  321. print(average)
  322.  
  323. # NEW FILE
  324.  
  325. cities = ["Rovaniemi", "Helsinki", "Athens", "mikkeli", "Stockholm", "Madrid"]
  326.  
  327. # note, sort(), you don't assign a variable
  328. cities.sort()
  329.  
  330. # create a new version of the list, sorted()
  331. # sorted also works tuples and dictionaries
  332. cities = sorted(cities)
  333.  
  334. # if you also want to take account lower and capital letters
  335. cities.sort(key=lambda v: v.upper())
  336.  
  337. print(cities)
Advertisement
Add Comment
Please, Sign In to add comment