Convert a denary number to a binary number.
Select the button below to open the Python program in a new window. Run the program and read the lines of code to see if you can understand how it works. It will be helpful to arrange your display so that you can have this browser window on one side of the screen and the code on the other.
Watch this video to learn about the new concepts shown in the program:
The new commands used in this program and others that may be useful. Select them below to learn more:
This is not percentage in Python, it is the modulo operator that calculates the remainder from a division, known as the `modulus`. E.g. seven sweets divided by two people is exactly two sweets each with one left over. The one remaining is the modulus, one.
7 % 2 = 1.
Is an integer (floor) division operator. It returns the result of a division as an integer with no fractional part. Any decimal places are discarded. The number is also known to be `truncated`. E.g. 7 / 2 = 3.5 but 7 // 2 = 3.
This can also be achieved with:
int(7 / 2)
Is exponentiation, or to the power of. E.g. 3 ** 5 = 243. It is the same as 3 * 3 * 3 * 3 * 3.
Casts (converts) the number y into the string x. Remember that the number 6 and the string "6" are not the same because numbers and strings are held in memory using different binary sequences. 6 is 110 (assuming unsigned binary) and "6" is 00110110.
x is assigned the absolute value of y. Absolute values are always positive. E.g. `abs(-5)` is 5. `abs(5)` is also 5. The `abs` function is a useful way to turn a negative number into a positive number.
x is assigned the rounded value of y to z decimal places using the 0.5 rule.
x is assigned the value of y rounded up to the nearest integer. Requires import math command to access this command.
x is assigned the value of y rounded down to the nearest integer. Requires import math command to access this command.
x is assigned the square root of y. Requires import math command to access this command.
x is assigned to be the constant pi.
Questions to think about with this program to check your understanding:
Why is the variable `binary` concatenated to the same variable `binary` in line 10? I.e. why can't the line read `binary = str(remainder)` instead?
Because the new digit calculated from the division must be added on to the end of the existing binary sequence that has been previously calculated.
What is the difference between using `number / 2` and `number // 2` in line 11? Could the programmer use `int(number / 2)` instead?
`number / 2` means divide the number by two whereas `number // 2` means divide the number by two and only keep the integer part of the result, known as integer or floor division.
Yes, the programmer could cast the result of the division (which is always a float) to an integer instead. It is just a matter of preference.
Change the program so that it:
Enter the denary number to convert: 10
Binary: 1010
Octal: 12
Hexadecimal: A
Enter the denary number to convert: 23
Binary: 10111
Octal: 27
Hexadecimal: 17
Enter the denary number to convert: 75
Binary: 1001011
Octal: 113
Hexadecimal: 4B
Use the flowcharts if you need more support in understanding the steps required to meet the criteria. Use the Parsons code sorting exercise only if you are really stuck.
Run your code to check that your program has met the success criteria and produces the required output.
Enter the denary number to convert: 10
Binary: 1010
Octal: 12
Hexadecimal: A
Enter the denary number to convert: 95
Binary: 1011111
Octal: 137
Hexadecimal: 5F
Enter the denary number to convert: 101
Binary: 1100101
Octal: 145
Hexadecimal: 65
Enter the denary number to convert: 253
Binary: 11111101
Octal: 375
Hexadecimal: FD