tuomasvaltanen

Untitled

Nov 9th, 2021 (edited)
479
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.99 KB | None | 0 0
  1. # Video lecture, Collections
  2. print("Welcome!")
  3.  
  4. # collection of products
  5. products = ['Washing machine', 'Coffee maker', 'Freezer', 'Vacuum cleaner']
  6.  
  7. # you can just get a single variable by accessing a certain index
  8. test = products[2]
  9. print(test)
  10.  
  11. # ask user for a integer
  12. choice = input("Which product would you like to see?\n")
  13. choice = int(choice)
  14.  
  15. # check that the user gave a proper index (less than the amount of products)
  16. if choice < len(products) and choice >= 0:
  17.     # or we can also ask this from the user and place it as index
  18.     first = products[choice]
  19.     print(first)
  20. else:
  21.     print("Bad index! Product doesn't exist!")
  22.  
  23. # NEW FILE
  24. products = ['Washing machine', 'Coffee maker', 'Freezer', 'Vacuum cleaner']
  25.  
  26. # looping through all products and printing one name at a time
  27. for p in products:
  28.     print(p)
  29.  
  30. # NEW FILE
  31.  
  32. products = ['Washing machine', 'Coffee maker', 'Freezer', 'Vacuum cleaner']
  33.  
  34. # get the number of products
  35. amount = len(products)
  36.  
  37. # process each product one at a time
  38. for index in range(amount):
  39.     # index only contains the index number, not the value of the product
  40.     # print(index)
  41.     p = products[index]
  42.     # print(p)
  43.  
  44.     # we can now combine both the index and the product!
  45.     # here a numbered list from 1 to 4
  46.     print(f"{index + 1}. {p}")
  47.    
  48. # NEW FILE
  49. # a tuple, just like list, but can't be changed afterwards
  50. weekdays = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
  51.  
  52. # tuples can't be changed afterwards (TypeError)
  53. # weekdays[2] = "Someday"
  54.  
  55. # ask from user and reduce 1 so numbers and days match
  56. # for example 3 = Wednesday
  57. choice = input("Which day would you like to see?\n")
  58. choice = int(choice) - 1
  59.  
  60. print(weekdays[choice])
  61.  
  62. # NEW FILE
  63. # a dictionary is a "variable" containing
  64. # other variables related to each other
  65. # for example, one person
  66. person = {
  67.     "name": "Test Person",
  68.     "age": 31,
  69.     "location": "Rovaniemi"
  70. }
  71.  
  72. # getting data from dictionary
  73. print("Your name:")
  74. print(person["name"])
  75. print()
  76.  
  77. # ... or the age, another way.
  78. person_age = person['age']
  79. print("Your age:")
  80. print(f"{person_age} years old.")
  81.  
  82. # NEW FILE
  83. products = ['Washing machine', 'Coffee maker', 'Freezer', 'Vacuum cleaner']
  84.  
  85. # changing a certain product with index
  86. products[1] = "CHANGED PRODUCT"
  87.  
  88. print(products)
  89.  
  90. # ask user for the index instead
  91. choice = input("Which product to change?\n")
  92. choice = int(choice)
  93.  
  94. # ask user for the new product for the previous index
  95. new_product = input("What is the new product?\n")
  96.  
  97. products[choice] = new_product
  98.  
  99. print(products)
  100.  
  101. # NEW FILE
  102.  
  103. # three day's temperature data
  104. day_1 = [13.5, 12.8, 11.9, 6.8]
  105. day_2 = [11.6, 11.7, 8.4, 7.5]
  106. day_3 = [12.5, 9.5, 10.3, 8.1]
  107.  
  108. # add all days into one list
  109. all_days = [day_1, day_2, day_3]
  110.  
  111. # because we have a list of lists
  112. # we need a for-loop inside a for-oop
  113. # go through each day
  114. for day in all_days:
  115.     print("NEW DAY!")
  116.  
  117.     # go through each temperature of one day at a time
  118.     for temperature in day:
  119.         print(temperature)
  120.  
  121. # NEW FILE
  122.  
  123. # dictionary in dictionary
  124.  
  125. book = {
  126.     "name": "My Lady Jane",
  127.     "year": 2016,
  128.     "publisher": {
  129.         "name": "HarperTeen",
  130.         "organization": "HarperCollins Publishers",
  131.         "location": "New York"
  132.     }
  133. }
  134.  
  135. # getting the book name
  136. print(book['name'])
  137.  
  138. # getting the publisher, then gettin the location of publisher
  139. publisher = book['publisher']
  140. print(publisher['location'])
  141.  
  142. # we can also get everything with one code line
  143. print(book['publisher']['location'])
  144.  
  145. # NEW FILE
  146. # string variable containing three pieces of data
  147. # separated by underscode
  148. # in one of the exercises, you can just put this
  149. # in a for-loop!
  150. text = "ORDER2327_C5162_2021"
  151.  
  152. # split into a list by underscore
  153. parts = text.split("_")
  154. print(parts)
  155.  
  156. # get the data into separate variables
  157. order = parts[0]
  158. client = parts[1]
  159. year = parts[2]
  160.  
  161. print(order)
  162. print(client)
  163. print(year)
  164.  
  165. # NEW FILE
  166. # append is useful, here we filter
  167. # short and long city names in two
  168. # seperate lists!
  169.  
  170. # cities
  171. cities = ["rovaniemi", "oulu", "helsinki", "stockholm", "madrid", "oslo"]
  172.  
  173. # intiialize "empty buckets" or lists for short and long city names
  174. short_city_names = []
  175. long_city_names = []
  176.  
  177. # go through cities and filter them into short and long city names
  178. for city in cities:
  179.     # less or equal to 6 characters long
  180.     if len(city) <= 6:
  181.         short_city_names.append(city)
  182.     else:
  183.         long_city_names.append(city)
  184.  
  185.  
  186. print(short_city_names)
  187. print(long_city_names)
  188.  
  189. # NEW FILE
  190. # cities
  191. cities = ["rovaniemi", "helsinki", "stockholm", "oulu", "madrid", "oslo"]
  192.  
  193.  
  194.  
  195. # break can be used to end the loop earlier
  196. # if some condition is met
  197. for city in cities:
  198.     # less or equal to 6 characters long
  199.     if len(city) <= 6:
  200.         # short city name, quit the loop
  201.         break
  202.     else:
  203.         # otherwise print city name
  204.         print(city)
  205.  
  206. # continue can be used to skip a turn if needed
  207. for city in cities:
  208.     # less or equal to 6 characters long
  209.     if len(city) <= 6:
  210.         # short city name, skip this cycle
  211.         continue
  212.     else:
  213.         # otherwise print city name
  214.         print(city)
  215.        
  216.  
  217. # NEW FILE
  218. # sometimes using collections allow you to
  219. # use powerful collection functions
  220. # for example, average of numbers
  221. # notice, we don't need a loop this way!
  222. grades = [5, 7, 5, 6, 9, 8, 6, 10]
  223.  
  224. # get sum + amount
  225. total = sum(grades)
  226. amount = len(grades)
  227.  
  228. # average = total sum / number of elements
  229. average = total / amount
  230. print("The average is:")
  231. print(average)
  232.  
  233. # NEW FILE
  234. cities = ["Rovaniemi", "helsinki", "London", "Madrid", "Rome"]
  235.  
  236. # default way of sorting
  237. # doesn't work correctly if some words
  238. # start with capital and some with lowercase letters
  239. # sorted_cities = sorted(cities)
  240. # print(sorted_cities)
  241.  
  242. # a trick by using a lambda function
  243. # before comparing cities to each other
  244. # change the city names CAPITAL
  245. cities.sort(key=lambda v: v.upper())
  246.  
  247. print(cities)
  248.  
  249.  
Add Comment
Please, Sign In to add comment