Text Size
Text Size
Exit Full Screen
Python3 Editor
Show
Source Code
View Source Code in Full Screen
#------------------------------------------------------------------------------------- # Modular Arithmetic #------------------------------------------------------------------------------------- # The modulo operator in Python is % # Use this operator to get the remainder of a/b. # If a % b == 0, then a is divisible by b. Otherwise, a is not divisible by b. # While you can do this with floats, you will most likely only use this for integers. # Due to the slight lack of precision of floating-point numbers, some properties of modular arithmetic are not exact. print("13 % 5 =", 13 % 5) # 13 % 5 = 3 print() # prints out the remainder dividing each value by 5 from 0-15 inclusive for i in range(16): print(i, '% 5 =', i % 5) print() # Notice how the values rotate like a clock? # A % N is some number from 0 to N-1 for any integer A. for i in range(31): if i % 5 == 0: print(i, 'is divisible by 5.') print('--------------------------------------------------------------\n') #--------------------------------------------------------------------------------------- # Binary Numbers #--------------------------------------------------------------------------------------- # Our regular number system that we use to count is base-10, called 'decimal' and uses digits 0-9. # When we count in decimal, we go # 0 1 2 3 4 5 6 7 8 9 # when we reach the last digit, we add a 1 to the left and start over at zero # 10 11 12 13 14 15 16 17 18 19 # and so on # Notice that if you do any number mod 10, you would get 0-9 as your remainder. # These numbers are represented by powers of 10. # 67,342 = 60,000 + 7,000 + 300 + 40 + 2 = (6 * 10000) + (7 * 1000) + (3 * 100) + (4 * 10) + (2 * 1) # = (6 * 10^4) + (7 * 10^3) + (3 * 10^2) + (4 * 10^1) + (2 * 10^0) #---------------------------------------------------------------------------------- # Binary numbers are base-2. They use digits 0 and 1. # Let's count in Binary # 0 1 10 11 100 101 110 111 1000 1001 1010 1011 1100 1101 1110 1111 # If we have a number like 110101, this number is represented by powers of 2, if you were to convert back to decimal (base-10). # 2^0 is on the right, the power increases as you go left. # 110101 = (1 * 2^5) + (1 * 2^4) + (0 * 2^3) + (1 * 2^2) + (0 * 2^1) + (1 * 2^0) # = (1 * 32) + (1 * 16) + (0 * 8) + (1 * 4) + (0 * 2) + (1 * 1) # = 32 + 16 + 0 + 4 + 0 + 1 = 53 # so 110101 in binary is 53 in decimal # How do you convert from decimal to binary? There are a few ways to do this: # METHOD 1: Divide, Mod, and Build backwards (right-to-left) with the remainders. # 53/2 = 26, 53 % 2 = 1 # 26/2 = 13, 26 % 2 = 0 # 13/2 = 6, 13 % 2 = 1 # 6/2 = 3, 6 % 2 = 0 # 3/2 = 1, 3 % 2 = 1 # 1/2 = 0, 1 % 2 = 1 # since a/b = 0, build backwards with remainders, going back up: 110101 # METHOD 2: Find first power of 2 that is less than the number. # 64 > 53, but 32 < 53 # Place a 1 for that spot, then subtract it from original number and repeat. # 100000, 53-32 = 21 # 16 < 21 # 110000, 21-16 = 5 # 8 > 5, place a 0 # 110000 # 4 < 5, place a 1 in that spot # 110100, 5-4 = 1 # 2 > 1, place a 0 # 110100 # 1 == 1, place a 1 # 110101, 1-1 = 0 --> STOP # It's up to you to figure out how to program these conversions. #---------------------------------------------------------------------------------------- # Hexadecimal numbers #---------------------------------------------------------------------------------------- # Hexadecimal numbers are base-16. They use digits 0-9 A-F. 10 = A, 11 = B, 12 = C, 13 = D, 14 = E, 15 = F # Counting with hexadecimal we have this # 0 = 0 # 1 = 1 # 2 = 2 # 3 = 3 # 4 = 4 # 5 = 5 # 6 = 6 # 7 = 7 # 8 = 8 # 9 = 9 # 10 = A # 11 = B # 12 = C # 13 = D # 14 = E # 15 = F # 16 = 10 # 17 = 11 # 18 = 12 # 19 = 13 ... # This works the same as binary and decimal, except it uses powers of 16. # 2A = (2 * 16^1) + (10 * 16^0) = (2 * 16) + (10 * 1) = 32 + 10 = 42 # FF = (15 * 16^1) + (15 * 16^0) = 240 + 15 = 255 # To convert from decimal to hexadecimal, you can do the same methods that were done converting from decimal to binary. # Though Method 1 is the easiest and I will show you this one specifically. # Divide, Mod, Build # NOTE: If the number is less than 255, you can build it easier by just Divide, Mod, Build with those two digits # 165/16 = 10 = A # 165 % 16 = 5 # 165 = A5 # If the number is greater than 255, it will look more like the decimal-to-binary conversions with multiple steps. # You need to BUILD BACKWARDS, so you take the last value from mod that you get and go up. # 314/16 = 19, 314 % 16 = 10 = A # 19/16 = 1, 19 % 16 = 3 # 1/16 = 0, 1 % 16 = 1 # 314 = 13A #---------------------------------------------------------------------------------------------------- # Built-in Methods for Conversions #---------------------------------------------------------------------------------------------------- # There are built-in functions you can do that will convert numbers for you. # However, you CANNOT use them in the challenges. # You can use them to double check if your functions are working correctly. # Use int(x, base) to convert a number x in the given base to base-10. print("13A in base-16 =", int('13A', 16)) print("110101 in base-2 =", int('110101', 2)) print() # Use bin() to convert to binary print("53 in binary =", bin(53)) # You can use the format function to get rid of the '0b' in front print("53 in binary =", format(53, 'b')) print() # Use hex() to convert to hexadecimal print("314 in hexadecimal =", hex(314)) # Again, you can use the format() function to get rid of the '0x' in front print("314 in hexadecimal =", format(314, 'x'))
CS110_Lesson9_Notes.py
( around 127 lines Python3 code )
Published By:
Mentor Sabrina
Created on
2020-12-07T15:53:28Z
Python3 Editor
- an
OYOclass
application,
own your own class today
.
Run
Result
Status:
Program is not running