tuomasvaltanen

Untitled

Nov 22nd, 2021 (edited)
575
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.77 KB | None | 0 0
  1. # complex data structures
  2. print("Welcome!")
  3.  
  4. day_1 = [-14.0, -20.3, -18.5, -17.1]
  5. day_2 = [-11.3, -23.5, -14.3, -15.2]
  6. day_3 = [-12.1, -17.4, -19.6, -16.6]
  7.  
  8. # add days to one list
  9. temperatures = [day_1, day_2, day_3]
  10.  
  11. # loop through all days
  12. for day in temperatures:
  13.     print("A new day!")
  14.  
  15.     # loop through all temperatures within one day
  16.     for temp in day:
  17.         print(temp)
  18.  
  19.  
  20. # dictionary inside another dictionary
  21. book = {
  22.     "name":  "My Lady Jane",
  23.     "year":  2016,
  24.     "publisher": {
  25.         "name": "HarperTeen",
  26.         "organization": "HarperCollins Publishers",
  27.         "location": "New York"
  28.     }
  29. }
  30.  
  31. # getting book's name and year
  32. print(book['name'])
  33. print(book['year'])
  34.  
  35. # getting location from publisher dictionary
  36. print(book['publisher']['location'])
  37.  
  38. # this is also okay if it's easier at first
  39. publisher = book['publisher']
  40. location = publisher['location']
  41. print(location)
  42.  
  43. # hotels data
  44.  
  45. # example 1
  46.  
  47. import var_dump as vd
  48.  
  49. # create first hotel
  50. hotel_1 = {
  51.     "name": "Snow Line Hotels",
  52.     "rating": 4.3,
  53.     "wifi": True,
  54.     "free_breakfast": True,
  55.     "services": ["sauna", "meetings", "restaurant", "parking", "safaris"],
  56.     "price_level": 4
  57. }
  58.  
  59. # create second hotel
  60. hotel_2 = {
  61.     "name": "North Ice Hostel",
  62.     "rating": 3.5,
  63.     "wifi": True,
  64.     "free_breakfast": False,
  65.     "services": ["sauna", "parking"],
  66.     "price_level": 2
  67. }
  68.  
  69. # place both hotels into one list
  70. hotels = [hotel_1, hotel_2]
  71.  
  72. # first_hotel = hotels[0]['services']
  73. # vd.var_dump(first_hotel)
  74.  
  75. sauna_hotels = []
  76.  
  77. for hotel in hotels:
  78.     print(f"{hotel['name']}, services:")
  79.  
  80.     # traditional looping
  81.     # for service in hotel['services']:
  82.     #     print(service)
  83.  
  84.     # if only a list of text, we can also do this
  85.     services = "\n".join(hotel['services'])
  86.     # print(services)
  87.  
  88.     # we can build a new list by using an if-statement
  89.     if "sauna" in services:
  90.         sauna_hotels.append(hotel['name'])
  91.  
  92.     # filter out hotels with a meeting room
  93.     if "meetings" in services:
  94.         print("The hotel has a meeting room!")
  95.     else:
  96.         print("No meeting room in this hotel")
  97.  
  98.     print()
  99.  
  100.  
  101. print(sauna_hotels)
  102.  
  103. # NEW FILE
  104.  
  105. import urllib.request
  106. import json
  107.  
  108. # this module needs to be installed separately
  109. # in PyCharm you can install the package if its not found!
  110. import var_dump as vd
  111.  
  112. # get internet data
  113. url = 'https://edu.frostbit.fi/api/events/en'
  114. req = urllib.request.Request(url)
  115. raw_data = urllib.request.urlopen(req).read().decode("UTF-8")
  116.  
  117. # the needed data from internet is now in the "data" -variable!
  118. data = json.loads(raw_data)
  119. # vd.var_dump(data)
  120.  
  121. for event in data:
  122.     print(event['name'])
  123.     categories = ", ".join(event['categories'])
  124.     print(categories)
  125.     print()
  126.  
Add Comment
Please, Sign In to add comment