# How many Bytes in…

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:

BytesKilobytesMegabytesGigabytesTerabytesPetabytes
A 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:

```def kilobytes_to_bytes(numberOfKilobytes):
bytes = 1024 * numberOfKilobytes
return bytes```

Note how the code for the subroutine/function is indented to the right.

We will then be able to call our the kilobytes_to_bytes() function to perform any conversion. T

`numberOfBytes = kilobytes_to_bytes(5)`

Check the Python code below where we have implemented and used the kilobytes_to_bytes() function to convert a user input into bytes.

• megabytes_to_bytes()
• gigabytes_to_bytes()
• terabytes_to_bytes()
• petabytes_to_bytes()

Create a new Python program that will:

• Ask the user how old they are (age in years),
• Convert this number in days knowing that there are 365.25 days in a year,
• Output this number of days to the end-user.

• How old is the user in days,
• How old is the user in hours,
• How old is the user in minutes,
• How old is the user in seconds.
[pms-restrict display_to=”not_logged_in” message=” “]

#### Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
[/pms-restrict] [pms-restrict subscription_plans=”14217″ message=” “]

#### Solution...

You are viewing this solution as part of your full membership subscription!

Python Code

#### Python Code

```#How many bytes in a kilobyte, megabyte, gigabyte, terabyte, petabyte

def kilobytes_to_bytes(numberOfKilobytes):
bytes = 1024 * numberOfKilobytes
return bytes

def megabytes_to_bytes(numberOfMegabytes):
bytes = 1024 * 1024 * numberOfMegabytes
return bytes

def gigabytes_to_bytes(numberOfGigabytes):
bytes = 1024 * 1024 * 1024 * numberOfGigabytes
return bytes

def terabytes_to_bytes(numberOfTerabytes):
bytes = 1024 * 1024 * 1024 * 1024 * numberOfTerabytes
return bytes

def petabytes_to_bytes(numberOfPetabytes):
bytes = 1024 * 1024 * 1024 * 1024 * 1024 * numberOfPetabytes
return bytes

#Main Program Starts Here
option = input("What unit would you like to convert from: KB, MB, GB, TB, PB?").upper()

if option=="KB":
numberOfKilobytes = int(input("Enter a number of kilobytes:"))
numberOfBytes = kilobytes_to_bytes(numberOfKilobytes)
print("There are " + str(numberOfBytes) + " Bytes in " + str(numberOfKilobytes) + " KB.")
elif option == "MB":
numberOfMegabytes = int(input("Enter a number of megabytes:"))
numberOfBytes = megabytes_to_bytes(numberOfMegabytes)
print("There are " + str(numberOfBytes) + " Bytes in " + str(numberOfMegabytes) + " MB.")
elif option == "GB":
numberOfGigabytes = int(input("Enter a number of gigabytes:"))
numberOfBytes = gigabytes_to_bytes(numberOfGigabytes)
print("There are " + str(numberOfBytes) + " Bytes in " + str(numberOfGigabytes) + " GB.")
elif option == "TB":
numberOfTerabytes = int(input("Enter a number of terabytes:"))
numberOfBytes = gigabytes_to_bytes(numberOfTerabytes)
print("There are " + str(numberOfBytes) + " Bytes in " + str(numberOfTerabytes) + " TB.")
elif option == "PB":
numberOfPetabytes = int(input("Enter a number of petabytes:"))
numberOfBytes = petabytes_to_bytes(numberOfPetabytes)
print("There are " + str(numberOfBytes) + " Bytes in " + str(numberOfPetabytes) + " PB.")
```
[/pms-restrict]

Did you like this challenge?

Click on a star to rate it!

Average rating 4.5 / 5. Vote count: 10

No votes so far! Be the first to rate this post.

As you found this challenge interesting...