Solutions Python

Solutions Python#

Task 1: What is 5 to the power of 5 minus 5?

Hide code cell content

5**5 - 5
3120

Task 2: Create variables for the efficiency of 45% and the input_energy of 1000 MWh. Compute and print the output_energy using these variables.

Hide code cell content

efficiency = 0.45
input_energy = 1000  # MWh
output_energy = efficiency * input_energy
print(output_energy)
450.0

Task 3: Split the following string into a list by splitting on the space character:

Hide code cell content

s = "Data Science for Energy System Modelling"
s.split(" ")
['Data', 'Science', 'for', 'Energy', 'System', 'Modelling']

Task 4: Create a list with the names of every planet in the solar system (in order).

  • Have Python tell you the name of the third planet.

  • Have Python tell you the total number of planets.

  • Have Python tell you whether “Pluto” is in the list of planets.

  • Iterate through your planets and print the planet name only if it has an “s” at the end.

Hide code cell content

planets = [
    "Mercury",
    "Venus",
    "Earth",
    "Mars",
    "Jupyter",
    "Saturn",
    "Uranus",
    "Neptune",
]

Hide code cell content

planets[2]
'Earth'

Hide code cell content

len(planets)
8

Hide code cell content

for p in planets:
    if p.endswith("s"):
        print(p)
Venus
Mars
Uranus

Task 5: Create a dictionary that contains the main facts about the Reuter West power plant.

  • Have Python tell you the main fuel type used in the power plant.

  • Add the power plant’s approximate latitude and longitude to the dictionary.

Hide code cell content

rw = {
    "Country": "Germany",
    "Electricity Capacity": 564,
    "Heat Capacity": 878,
    "Technology": "Combined heat and power (CHP)",
    "Main Fuel": "Hard coal",
    "Vattenfall ownership share": "100%",
    "Status": "In Operation",
}

Hide code cell content

rw["Main Fuel"]
'Hard coal'

Hide code cell content

rw["x"] = 13.24
rw["y"] = 52.53

Task 6: Write a function that converts units of energy from ‘ktoe’ to ‘GWh’

Hide code cell content

def ktoe_to_gwh(x):
    return 11.63 * x

Task 7: Write a more general unit conversion function that converts between all units of energy listed under the link below. The function should take arguments: for the original value, the original unit and the target unit. Implement the function in a way that the default target unit is “Wh”. Verify that function by converting 200 ‘toe’ to ‘Wh’.

Note

You can also just pick three units to convert between if you don’t feel like going through all combinations.

Hide code cell content

def to_joule(value, from_unit):
    if from_unit.endswith("cal"):
        return value / 0.2390
    elif from_unit.endswith("Btu"):
        return value / 0.0009478
    elif from_unit.endswith("Wh"):
        return value / 0.0002778
    elif from_unit.endswith("toe"):
        return value * 2.388e11
    elif from_unit.endswith("tce"):
        return value * 3.412e11
    else:
        raise NotImplementedError()

Hide code cell content

def convert_unit(value, from_unit, to_unit="Wh"):
    x = to_joule(value, from_unit)
    if to_unit.endswith("cal"):
        x *= 0.2390
    elif to_unit.endswith("Btu"):
        x *= 0.0009478
    elif to_unit.endswith("Wh"):
        x *= 0.0002778
    elif to_unit.endswith("toe"):
        x /= 2.388e11
    elif to_unit.endswith("tce"):
        x /= 3.412e11
    else:
        raise NotImplementedError()

    return x

Hide code cell content

convert_unit(200, "toe")
13267727999.999998