Page 39 - Programming With Python 3
P. 39

An Introduction to STEM Programming with Python — 2019-09-03a                               Page 26
            Chapter 2 — Numbering Systems



                             Power of 16
                                                                                       Decimal
             Free                                   Decimal    Power of 16            1,048,576
                                                                  4
                               0
                                                               16
                             16
                                                          1
                                                                                         65,536
                               1
                                                                  5
                             16
                                                               16
                                                         16
                                                        256
                                                                                     16,777,216
                             16
                             16 2 3                   4,096    16 6 7              268,435,456
                                                               16
             eBook
                                                    Table 3: Powers of 16
            Hexadecimal in Python
             Edition
            In Python, you may directly include base 16 integers exactly the same as you would a decimal number.
            You just need to add a "0x" to the front of the number so that Python knows it is not a string or a
            decimal number. You will use the symbols 0-9 for 0-9 and a-f for 10-15. The "0x" prefix is not case-
            sensitive and the letters a-f may also be in upper and lower case. In the example below, you see the
            difference adding the "0x" prefix does. Print will always out a number as a decimal.
            Please support this work at

               1|  print(0X42, 0Xff, 0xfffa, 0XABBACAB)

                                  http://syw2l.org
                    66 255 65530 180071595

            Python will convert a decimal number into a hexadecimal string, using the hex() function. You will
                                                                               Free
            be able to print it out, but the result is no longer a number and can not be directly used in mathematics.



             hex(expression)                                                                         Function
             The hex function will return a string of hexadecimal digits prefixed with '0x'
             representing the number passed.
                                                                   eBook
             https://docs.python.org/3.7/library/functions.html#hex
             http://bit.ly/2EMo6dJ




                                                                Edition
               1|  print(hex(42))

                    0x2a





            Copyright 2019 — James M. Reneau Ph.D. — http://www.syw2l.org — This work is licensed
            under a Creative Commons Attribution-ShareAlike 4.0 International License.
   34   35   36   37   38   39   40   41   42   43   44