Hacking Secret Ciphers with Python

(Ann) #1
Chapter 9 – The Transposition Cipher, Decrypting 129




21 / 7
3.0





This is useful because if a number does not divide evenly, the numbers after the decimal point
will be needed. For example, 22 / 5 evaluates to 4.4:





22 / 5
4.4





(If the expression 22 / 5 evaluates to 4 instead of 4 .4, then you are using version 2 of Python
instead of version 3. Please go to the http://python.org website and download and install Python
3.)


If you want to round this number to the nearest integer, you can use the round() function. Type
the following into the interactive shell:





round(4.2)
4
round(4.5)
4
round(4.9)
5
round(5.0)
5
round(22 / 5)
4





If you only want to round up then use the math.ceil() function, which stands for “ceiling”. If
you only want to round down then use the math.floor() function. These functions exist in
the math module, so you will need to import the math module before calling them. Type the
following into the interactive shell:





import math
math.floor(4.0)
4
math.floor(4.2)
4
math.floor(4.9)
4
math.ceil(4.0)




Free download pdf