Featured Posts

Start Your Journey with Linux Command Line

Image
Start Your Journey with the Linux Command Line: A Comprehensive Guide Whether you are a software developer, system administrator, analytics engineer, or cybersecurity enthusiast, mastering the Linux command line (terminal) is one of the single most effective skills you can acquire. While graphical user interfaces (GUIs) offer visual convenience, the command line interface (CLI) delivers unmatched speed, fine-grained system control, and seamless automation capabilities. Linux Command Line This tutorial breaks down more than 35 core Linux commands into structured, practical modules complete with real-world examples, flags, and command-chaining techniques. By building muscle memory around these fundamentals, you will elevate your daily technical workflow from basic navigation to advanced command orchestration. Why Learn the Command Line? The Linux CLI is not merely a legacy tool—it remains the foundation of modern cloud architecture, server maintenance, DevOps pipelines, and enterp...

Python 4.3.1.10 LAB: Converting fuel consumption

 Python 4.3.1.10 LAB: Converting fuel consumption

4.3.1.10 LAB: Converting fuel consumption

4.3.1.10 LAB

Objectives

  • improving the student's skills in defining, using and testing functions.

Scenario

A car's fuel consumption may be expressed in many different ways. For example, in Europe, it is shown as the amount of fuel consumed per 100 kilometers.

In the USA, it is shown as the number of miles traveled by a car using one gallon of fuel.

Your task is to write a pair of functions converting l/100km into mpg "mile per gallon", and vice versa.

Python 4.3.1.10 LAB: Converting fuel consumption
Sandbox

The functions:

are named liters_100km_to_miles_gallon and miles_gallon_to_liters_100km respectively;

take one argument (the value corresponding to their names)

Complete the code in the editor.

Run your code and check whether your output is the same as ours.

Here is some information to help you:

1 American mile = 1609.344 metres; 1 mile = "1.60934" meters

1 American gallon = 3.785411784 litres.

Expected output

  • 60.31143162393162
  • 31.36194444444444
  • 23.52145833333333
  • 3.9007393587617467
  • 7.490910297239916
  • 10.009131205673757

Solution Code:

def liters_100km_to_miles_gallon(liters):
    gallons = liters / 3.785411784
    # 1 mile = "1.60934" meters
    # 1 km   = 1000 meters .. 1000 * 1.60934 = 1609.344
    # 100km = 100 * 1000 meters (The function works on 100 km)
    miles = 100 * 1000 / 1609.344
    return miles / gallons

def miles_gallon_to_liters_100km(miles):
    km_100 = miles * 1609.344 * 1000 / 100
    liters = 3.785411784
    return liters / km_100

print(liters_100km_to_miles_gallon(3.9))
print(liters_100km_to_miles_gallon(7.5))
print(liters_100km_to_miles_gallon(10.))
print(miles_gallon_to_liters_100km(60.3))
print(miles_gallon_to_liters_100km(31.4))
print(miles_gallon_to_liters_100km(23.5))

=================================================================================

Follow the Python page on the Blog to be the first to know

Two Ways to Measure Fuel

LAB 4.3.1.10 asks you to convert between two common ways of expressing a car's fuel consumption. In Europe fuel use is reported as liters consumed per 100 kilometers, while in the United States it is shown as miles traveled per gallon of fuel, abbreviated mpg. Converting between these units requires knowing the relationships between meters and miles, and between liters and gallons: one American mile equals 1609.344 meters, and one American gallon equals 3.785411784 liters.

The lab asks for two functions. The first, liters_100km_to_miles_gallon, takes the liters per 100 km and returns the equivalent fuel economy in miles per gallon. The second, miles_gallon_to_liters_100km, does the reverse, taking an mpg value and returning the liters consumed per 100 km. Writing these two reciprocals is the core exercise in defining, using, and testing functions.

Working Out the Conversion

Conceptually, both conversions begin by finding how far a car travels on a fixed quantity of fuel. In the first function, the liters are converted to gallons by dividing by 3.785411784. The distance for 100 km in miles is computed as 100 * 1000 / 1609.344, since 100 km equals 100,000 meters and dividing by the number of meters in a mile gives the miles. The miles per gallon is then the miles divided by the gallons, which is returned as the final result.

In the reverse function, the miles are first converted back into a 100 km equivalent by multiplying by 1609.344 and 1000 and dividing by the 100 km scale, then the constant 3.785411784 liters per gallon is divided by that value to recover the liters per 100 km. Testing the functions against the expected outputs confirms the math, and because the two relationships are exact reciprocals, they transitively verify each other.

Key Takeaways

  • European fuel use is liters per 100 km, while US fuel economy is miles per gallon.
  • One American mile equals 1609.344 meters and one American gallon equals 3.785411784 liters.
  • Two functions handle the conversion: liters_100km_to_miles_gallon and miles_gallon_to_liters_100km.
  • Each function takes a single argument and returns the converted value computed by multiplying or dividing the unit factors.
  • Testing both functions against known outputs verifies that the conversions are correct reciprocals.

Frequently Asked Questions

What units are used on each side of the conversion?

The European side reports liters consumed per 100 kilometers, and the US side reports miles traveled per gallon. The two functions move a value from one side to the other.

Why are the conversion factors important?

Because meters, miles, liters, and gallons are independent units, you must apply the exact factors 1609.344 meters per mile and 3.785411784 liters per gallon to get correct numbers.

How do I verify my functions are correct?

Call each function with the sample values from the lab and confirm the printed outputs match the expected results. Because the functions perform inverse operations, a correct pair will also convert a value there and back unchanged.

Does the number of arguments matter?

Each function is designed to take exactly one argument, the value that matches its name. Passing the liters per 100 km into the first function and the mpg into the second produces the correct converted results.

Comments

Popular Posts

PROJECT MAVEN | The Architecture of Algorithmic Warfare

Python 3.2.1.14 LAB: Essentials of the while loop

Python for Windows Beginners: Build Your First Automated Workflow in 10 Minutes

Data Analysis Roadmap 2026: From Excel Lover to Python-Powered Analyst