Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # complex data structures
- print("Welcome!")
- day_1 = [-14.0, -20.3, -18.5, -17.1]
- day_2 = [-11.3, -23.5, -14.3, -15.2]
- day_3 = [-12.1, -17.4, -19.6, -16.6]
- # add days to one list
- temperatures = [day_1, day_2, day_3]
- # loop through all days
- for day in temperatures:
- print("A new day!")
- # loop through all temperatures within one day
- for temp in day:
- print(temp)
- # dictionary inside another dictionary
- book = {
- "name": "My Lady Jane",
- "year": 2016,
- "publisher": {
- "name": "HarperTeen",
- "organization": "HarperCollins Publishers",
- "location": "New York"
- }
- }
- # getting book's name and year
- print(book['name'])
- print(book['year'])
- # getting location from publisher dictionary
- print(book['publisher']['location'])
- # this is also okay if it's easier at first
- publisher = book['publisher']
- location = publisher['location']
- print(location)
- # hotels data
- # example 1
- import var_dump as vd
- # create first hotel
- hotel_1 = {
- "name": "Snow Line Hotels",
- "rating": 4.3,
- "wifi": True,
- "free_breakfast": True,
- "services": ["sauna", "meetings", "restaurant", "parking", "safaris"],
- "price_level": 4
- }
- # create second hotel
- hotel_2 = {
- "name": "North Ice Hostel",
- "rating": 3.5,
- "wifi": True,
- "free_breakfast": False,
- "services": ["sauna", "parking"],
- "price_level": 2
- }
- # place both hotels into one list
- hotels = [hotel_1, hotel_2]
- # first_hotel = hotels[0]['services']
- # vd.var_dump(first_hotel)
- sauna_hotels = []
- for hotel in hotels:
- print(f"{hotel['name']}, services:")
- # traditional looping
- # for service in hotel['services']:
- # print(service)
- # if only a list of text, we can also do this
- services = "\n".join(hotel['services'])
- # print(services)
- # we can build a new list by using an if-statement
- if "sauna" in services:
- sauna_hotels.append(hotel['name'])
- # filter out hotels with a meeting room
- if "meetings" in services:
- print("The hotel has a meeting room!")
- else:
- print("No meeting room in this hotel")
- print()
- print(sauna_hotels)
- # NEW FILE
- import urllib.request
- import json
- # this module needs to be installed separately
- # in PyCharm you can install the package if its not found!
- import var_dump as vd
- # get internet data
- url = 'https://edu.frostbit.fi/api/events/en'
- req = urllib.request.Request(url)
- raw_data = urllib.request.urlopen(req).read().decode("UTF-8")
- # the needed data from internet is now in the "data" -variable!
- data = json.loads(raw_data)
- # vd.var_dump(data)
- for event in data:
- print(event['name'])
- categories = ", ".join(event['categories'])
- print(categories)
- print()
Add Comment
Please, Sign In to add comment