tuomasvaltanen

Untitled

Nov 4th, 2021 (edited)
890
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.45 KB | None | 0 0
  1. # Classroom lecture, 4.11.2021, Collections!
  2. print("Welcome!")
  3.  
  4. # NEW FILE
  5.  
  6. products = ['Bosch washing machine',
  7.             'Philips coffee maker',
  8.             'Siemens freezer',
  9.             'Samsung smartphone']
  10.  
  11. # only use this if just testing what's inside a collection
  12. # print(products)
  13.  
  14. # ask user for index
  15. choice = input("Which product you want to see?\n")
  16. choice = int(choice)
  17.  
  18. # only print the product if index is within acceptable range, in this case, 0-3
  19. if choice < len(products) and choice >= 0:
  20.     p = products[choice]
  21.     print(p)
  22. else:
  23.     print("No product with given index.")
  24.  
  25. # NEW FILE
  26. products = ['Bosch washing machine',
  27.             'Philips coffee maker',
  28.             'Siemens freezer',
  29.             'Samsung smartphone',
  30.             'LG 4K television']
  31.  
  32. # looping through products
  33. for p in products:
  34.     print(p)
  35.  
  36. # NEW FILE
  37.  
  38. products = ['Bosch washing machine',
  39.             'Philips coffee maker',
  40.             'Siemens freezer',
  41.             'Samsung smartphone',
  42.             'LG 4K television']
  43.  
  44. amount = len(products)
  45.  
  46. # numbered list with traditional loop
  47. for index in range(amount):
  48.     p = products[index]
  49.     print(f"{index + 1}. {p}")
  50.  
  51. # NEW FILE
  52.  
  53. # tuple is a list that can't be changed afterwards
  54. weekdays = ('Monday', 'Tuesday', 'Wednesday', 'Thursday',
  55.             'Friday', 'Saturday', 'Sunday')
  56.  
  57. # ask the weekday from user
  58. # -1 fixes the problem with collections starting from 0!
  59. choice = input("Which weekday number?\n")
  60. choice = int(choice) - 1
  61.  
  62. day = weekdays[choice]
  63. print(day)
  64.  
  65. # NEW FILE
  66.  
  67. # a person dictionary, key-value pairs
  68. # basically a variable containing multiple variables
  69. # that are related to each other
  70. person = {
  71.     "name": "Dave Developer",
  72.     "age": 32,
  73.     "location": "Rovaniemi"
  74. }
  75.  
  76. # printing snippets of info from the dictionary
  77. print(person['name'])
  78. print(f"Person's age: {person['age']} years old.")
  79. print()
  80.  
  81. # if you need to loop through dictionary, it can be done like this
  82. for key in person:
  83.     value = person[key]
  84.     print(value)
  85.  
  86. # NEW FILE
  87.  
  88. products = ['Bosch washing machine',
  89.             'Philips coffee maker',
  90.             'Siemens freezer',
  91.             'Samsung smartphone',
  92.             'LG 4K television']
  93.  
  94. # which index
  95. choice = input("Which product would you like to change?\n")
  96. choice = int(choice)
  97.  
  98. # which new value
  99. new_product = input("What is the new product?\n")
  100.  
  101. # replace the value in the collection
  102. products[choice] = new_product
  103.  
  104.  
  105. for p in products:
  106.     print(p)
  107.  
  108. print("done")
  109.  
  110. # NEW FILE
  111.  
  112. # list of lists
  113. day_1 = [20.0, 15.6, 17.8, 12.6, 11.4]
  114. day_2 = [21.0, 14.3, 17.5, 13.3, 15.3]
  115. day_3 = [19.0, 11.6, 14.9, 15.6, 14.9]
  116.  
  117. temperatures = [day_1, day_2, day_3]
  118. #print(temperatures)
  119.  
  120. counter = 1
  121. # because we have a list of lists
  122. # we need a for loop inside a for loop
  123. for day in temperatures:
  124.     print(f"Day number {counter}!")
  125.     counter += 1
  126.  
  127.     for temp in day:
  128.         print(temp)
  129.  
  130. print("done")
  131.  
  132. # NEW FILE
  133.  
  134. # the original code
  135. text = "ORDER142_A1567_2021"
  136.  
  137. # split into a list
  138. parts = text.split('_')
  139.  
  140. # save list parts into variables
  141. code = parts[0]
  142. client = parts[1]
  143. year = parts[2]
  144.  
  145. # let's test
  146. print(code)
  147. print(client)
  148. print(year)
  149.  
  150. # NEW FILE
  151.  
  152. numbers = [5, 6, 7, 7, 8, 9, 3, 4, 5, 6, 7, 5]
  153.  
  154. # average = total sum of values / amount of values
  155.  
  156. amount = len(numbers)
  157. total = sum(numbers)
  158.  
  159. # collections can be helpful like this
  160. average = total / amount
  161. print(average)
  162.  
Add Comment
Please, Sign In to add comment