{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Answer 1" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "b'\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00'" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bytes(15)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Answer 2\n", "In the output below, notice how the interpreter attempts to show you the string version of the binary value, as in the \\t in place of \\x09 and \\r in place of \\x0d. Try not to be confused by this." ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "b'\\x01\\x03\\x05\\x07\\t\\x0b\\r\\x0f\\x11\\x13\\x15\\x17\\x19\\x1b\\x1d'" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bytes(range(1, 31, 2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Answer 3\n", "Since the byte values must be modified after initial creation, a bytearray object should be used." ] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "bytearray(b'\\x00\\xff\\xff\\xff\\xff\\x00\\xff\\xff\\xff\\xff')" ] }, "execution_count": 80, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_bytes = bytearray()\n", "my_bytes = bytearray([255] * 10)\n", "my_bytes[::5] = bytearray([0] * 2)\n", "my_bytes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Answer 4" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "b'\\xaa\\xaa\\xaa\\xaa\\x00\\xaa\\xaa\\xaa\\xaa'" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_bytes = b'\\x00'.join((bytearray([170] * 4), \n", " bytearray([170] * 4)))\n", "my_bytes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Answer 5" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "b'\\n\\x00'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b'\\x0a\\x00'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Answer 6\n", "You could do this using the string character for the copyright symbol, or, it's unicode code point. Both result in the same binary value. Both methods are shown independently below." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "b'\\xc2\\xa92018'" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'©2018'.encode()" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "b'\\xc2\\xa92018'" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'\\xa92018'.encode()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Answer 7" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'📞123-456-7890'" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "phone_display_str = '\\N{Telephone Receiver}123-456-7890'\n", "phone_display_str" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "b'\\xff\\xfe\\x0e&1\\x002\\x003\\x00-\\x004\\x005\\x006\\x00-\\x007\\x008\\x009\\x000\\x00'" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "phone_display_str.encode(encoding='UTF-16')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Answer 8" ] }, { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [], "source": [ "bin_seq = b'\\xe2\\x98\\xa2\\x20\\x43\\x61\\x6c\\x6c\\x20\\x46\\x61\\x63\\x69\\x6c\\x69\\x74\\x69\\x65\\x73'" ] }, { "cell_type": "code", "execution_count": 84, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'☢ Call Facilities'" ] }, "execution_count": 84, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bin_seq.decode()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Answer 9" ] }, { "cell_type": "code", "execution_count": 85, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 85, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bin_seq.count(b'\\x6c')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Answer 10" ] }, { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 86, "metadata": {}, "output_type": "execute_result" } ], "source": [ " b'\\x20' in bin_seq" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Answer 11" ] }, { "cell_type": "code", "execution_count": 88, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 88, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bin_seq.startswith(b'\\xe2')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Answer 12" ] }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[b'\\xe2\\x98\\xa2', b'Call', b'Facilities']" ] }, "execution_count": 89, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bin_seq.split(b'\\x20')" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.4.6" } }, "nbformat": 4, "nbformat_minor": 2 }