{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "#Advanced Numbers\n", "In this lecture we will learn about a few more representations of numbers in Python." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##Hexadecimal\n", "\n", "Using the function hex() you can convert numbers into a [hexadecimal](https://en.wikipedia.org/wiki/Hexadecimal) format:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'0xf6'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hex(246)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'0x200'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hex(512)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##Binary \n", "Using the function bin() you can convert numbers into their [binary](https://en.wikipedia.org/wiki/Binary_number) format." ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'0b10011010010'" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bin(1234)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'0b10000000'" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bin(128)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'0b1000000000'" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bin(512)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##pow()\n", "With two arguments, equivalent to x^y. With three arguments,\n", "equivalent to (x^y) % z, but may be more efficient (e.g. for longs)." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "16" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pow(2,4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##abs\n", "Absolute Value" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "abs(-3)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "abs(3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##round\n", "Round a number to a given precision in decimal digits (default 0 digits).\n", "This always returns a floating point number. " ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "3.0" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "round(3)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "3.14" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "round(3.1415926535,2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python has a built-in math library that is also useful to play around with in case you are ever in need of some mathematical operations. Explore the documentation [here](https://docs.python.org/2/library/math.html)!" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.10" } }, "nbformat": 4, "nbformat_minor": 0 }