Tracks
/
Python
Python
/
Exercises
/
Electric Bill
Electric Bill

Electric Bill

Learning Exercise

Introduction

Python has three different types of built-in numbers: integers (int), floating-point (float), and complex (complex). Fractions (fractions.Fraction) and Decimals (decimal.Decimal) are also available via import from the standard library.

Whole numbers including hexadecimal (hex()), octal (oct()) and binary (bin()) numbers without decimal places are also identified as ints:

# Ints are whole numbers.
>>> 1234
1234
>>> type(1234)
<class 'int'>

>>> -12
-12

Numbers containing a decimal point (with or without fractional parts) are identified as floats:

>>> 3.45
3.45
>>> type(3.45)
<class 'float'>

Arithmetic

Python fully supports arithmetic between these different number types, and will convert narrower numbers to match their less narrow counterparts when used with the binary arithmetic operators (+, -, *, /, //, and %).

Addition and subtraction

Addition and subtraction operators behave as they do in normal math. If one or more of the operands is a float, the remaining ints will be converted to floats as well:

>>> 5 - 3
2
# The int is widened to a float here, and a float is returned.
>>> 3 + 4.0
7.0

Multiplication

As with addition and subtraction, multiplication will convert narrower numbers to match their less narrow counterparts:

>>> 3 * 2
6

>>> 3 * 2.0
6.0

Division

Division always returns a float, even if the result is a whole number:

>>> 6/5
1.2

>>> 6/2
3.0

Floor division

If an int result is needed, you can use floor division to truncate the result. Floor division is performed using the // operator:

>>> 6//5
1

>>> 6//2
3

Modulo

The modulo operator (%) returns the remainder of the division of the two operands:

# The result of % is zero here, because dividing 8 by 2 leaves no remainder
>>> 8 % 2
0

# The result of % is 2 here, because 3 only goes into 5 once, with 2 leftover
>>> 5 % 3
2

Another way to look at 5 % 3:

>>> whole_part = int(5/3)
1

>>> decimal_part = 5/3 - whole_part
0.6666666666666667

>>> whole_remainder = decimal_part * 3
2.0

Round

Python provides a built-in function round(number, <decimal_places>) to round off a floating point number to a given number of decimal places. If no number of decimal places is specified, the number is rounded off to the nearest integer and will return an int:

>>> round(3.1415926535, 2)
3.14

>>> round(3.1415926535)
3

Priority and parentheses

Python allows you to use parentheses to group expressions. This is useful when you want to override the default order of operations.

>>> 2 + 3 * 4
14

>>> (2 + 3) * 4
20

Python follows the PEMDAS rule for operator precedence. This means calculations within () have the highest priority, followed by **, then *, /, //, %, +, and -:

>>> 2 + 3 - 4 * 4
-11

>>> (2 + 3 - 4) * 4
4

# In the following example, the `**` operator has the highest priority, then `*`, then `+`
# Meaning we first do 4 ** 4, then 3 * 64, then 2 + 192
>>> 2 + 3 * 4 ** 4
770

Precision & Representation

Integers in Python have arbitrary precision -- the number of digits is limited only by the available memory of the host system.

Floating point numbers are usually implemented using a double in C (15 decimal places of precision), but will vary in representation based on the host system. Complex numbers have a real and an imaginary part, both of which are represented by floating point numbers.

For a more detailed discussions of the issues and limitations of floating point arithmetic across programming languages, take a look at 0.30000000000000004.com and The Python Tutorial.

Instructions

The company you work for wants to reduce their carbon footprint, so they want you to write a program to calculate the power usage and cost of running their electronics.

1. Get extra hours

Your employer has a program that calculates the time it takes to run different electronics. Currently, the time is stored in hours. When your employer added the hours, they noticed that the time duration was not correct. They want you to add 3 extra hours to the time data. They would also like to know how many "extra" hours there are after converting the data to "full" days (a day is 24 hours). The time to convert may not be in full days.

Implement a function get_extra_hours() that accepts an integer which holds the number of hours. The function should make the appropriate "extra hours" adjustment, and then return an integer representing how many hours needs to be removed from the total to get the time in "full" days.

>>> get_extra_hours(25)
4

2. Get kW value

Your employer wants to know the power usage of the different electronics in kW. kW stands for kilowatt, where watts are a unit of power. Kilo in the unit name is a prefix in the metric system meaning 1000. One kilowatt == 1000 watts.

Implement a function get_kW_value() that accepts an integer which holds the number of watts. The function should then return the watts as kilowatts rounded to 1 decimal place.

>>> get_kW_value(1150)
1.2

3. Get kWh value

To be able to calculate the cost of running the electronics, your employer needs to know the power usage in kWh. kWh stands for kilowatt-hour, where hour is a unit of time. One kilowatt-hour == 1000 watts used for 1 hour. An hour is made up of 60 minutes and a minute is made up of 60 seconds. One hour is equal to 3600 seconds. To calculate the kWh value, you must convert watts to kW, and then floor-divide the result by 3600.

Implement a function get_kWh_value() that accepts an integer which holds the number of watts. The function should then return the kilowatt-hours as an integer.

>>> get_kWh_value(5000000)
1

4. Get efficiency

Electronics are not 100% efficient. Therefore, your employer wants you to calculate the efficiency of the electronics. To get efficiency, you must divide the power factor (a float between 0 and 100) by 100.

Implement a function get_efficiency() that accepts a float that holds the power factor. The function should then return the calculated efficiency as a float.

>>> get_efficiency(80)
0.8

5. Get cost

Your employer wants to know the cost of running the electronics. The cost of running the electronics is the power used multiplied by the cost per kWh. The power used is the power given divided by the calculated efficiency.

Implement a function get_cost(<watts>,<power_factor>,<price>) that accepts an integer that holds the number of watts, a float that has the power factor, and a float that holds the cost per kWh. The function should then return the cost of running the electronics as a float.

>>> get_cost(5000000, 80, 0.25)
0.3125
Edit via GitHub The link opens in a new window or tab
Python Exercism

Ready to start Electric Bill?

Sign up to Exercism to learn and master Python with 17 concepts, 140 exercises, and real human mentoring, all for free.