In this challenge we will write a set of functions to calculate how many Bytes there are in a given number of kilobytes, megabytes, gigabytes, terabytes or petabytes.
First let’s investigate the link between these storage units:
BytesKilobytesMegabytesGigabytesTerabytesPetabytesA Byte is a small storage unit that contains 8 bits. Here is what a Byte looks like:
01101110 A Byte can be used to store an integer between 0 and 255, or a single character (using the ASCII code). A kilobyte (KB) contains 1024 Bytes. 1 KB = 1024 Bytes A text file is often measured in Kilobytes as it would contain a few thousand characters. A megabyte (MB) contains 1024 kilobytes. 1 MB = 1024 KB= 1024 x 1024 Bytes A digital photograph or an mp3 would typically take up a few megabytes of disk space. A gigabyte (GB) contains 1024 megabytes. 1 GB = 1024 MB= 1024 x 1024 KB= 1024 x 1024 x 1024 Bytes A high quality movie (DVD) would take up a few gigabytes. An SD card or USB key would contain a few GB of data. A terabyte (TB) contains 1024 gigabytes. 1 TB = 1024 GB= 1024 x 1024 MB= 1024 x 1024 x 1024 KB= 1024 x 1024 x 1024 x 1024 Bytes A hard drive can contain a few Terabytes of data. A petabyte (PB) contains 1024 terabytes. 1 PB = 1024 TB= 1024 x 1024 GB= 1024 x 1024 x 1024 MB= 1024 x 1024 x 1024 x 1024 KB= 1024 x 1024 x 1024 x 1024 x 1024 Bytes Very large databases stored on web servers used by large websites (Google, Facebook, etc…) will contain a few Petabytes of data.
Subroutines?
By completing this challenge we will investigate how subroutines (functions/procedures) are used in Python to create sections of code that can be called/reused many times within a program. Subroutines mean that a section of code can be identified using a name (identifier). This enables us to add more instructions to the already existing functions used in Python such as print or input.
To create a new function in Python we have to declare it first using the keyword def in Python. Our first function will be called kilobytes_to_bytes() and will take one parameter: numberOfKilobytes: (integer value).
def kilobytes_to_bytes(numberOfKilobytes):
We will then add the code to calculate the number of bytes that this function should return: