{ "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": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0xf6'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hex(246)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0x200'" ] }, "execution_count": 2, "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": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0b10011010010'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bin(1234)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0b10000000'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bin(128)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0b1000000000'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bin(512)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exponentials\n", "The function pow() takes two arguments, equivalent to ```x^y```. With three arguments it is equivalent to ```(x^y)%z```, but may be more efficient for long integers." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "81" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pow(3,4)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pow(3,4,5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Absolute Value\n", "The function abs() returns the absolute value of a number. The argument may be an integer or a floating point number. If the argument is a complex number, its magnitude is returned.\n" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.14" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "abs(-3.14)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "abs(3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Round\n", "The function round() will round a number to a given precision in decimal digits (default 0 digits). It does not convert integers to floats." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "round(3,2)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "400" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "round(395,-2)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.14" ] }, "execution_count": 12, "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/3/library/math.html)!" ] } ], "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.6.2" } }, "nbformat": 4, "nbformat_minor": 1 }