{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Data types\n", "\n", "![Banner - circuits](https://github.com/ianmcloughlin/images/raw/master/banner-circuit.jpg)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "#### Python\n", "\n", "
" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "i = 1\n", "f = 1.0\n", "s = \"1\"" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(i)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(f)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(s)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Operations\n", "\n", "
" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i * 10" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10.0" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f * 10" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'1111111111'" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s * 10" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Binary representations\n", "\n", "
" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "import struct" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "i = 123\n", "f = 123.0\n", "s = \"123\"" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0b1111011'" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bin(i)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0b1000010111101100000000000000000'" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bin(struct.unpack('!i',struct.pack('!f', f))[0])" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0b110001 0b110010 0b110011'" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "' '.join(map(bin, bytearray(s, 'ascii')))" ] } ], "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.8.5" } }, "nbformat": 4, "nbformat_minor": 4 }