{ "metadata": { "name": "", "signature": "sha256:96b94d1de1d6823d9a78b989a8d10ff64a07b6baa9628b3106f6b5533559f1a7" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Assignment Operators" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- **Author:** [Chris Albon](http://www.chrisalbon.com/), [@ChrisAlbon](https://twitter.com/chrisalbon)\n", "- **Date:** -\n", "- **Repo:** [Python 3 code snippets for data science](https://github.com/chrisalbon/code_py)\n", "- **Note:** -" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create some variables" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = 2\n", "b = 1\n", "c = 0\n", "d = 3" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 41 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Assigns values from right side to left side" ] }, { "cell_type": "code", "collapsed": false, "input": [ "c = a + b\n", "c" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 42, "text": [ "3" ] } ], "prompt_number": 42 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Add right to the left and assign the result to left (c = a + c)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "c += a\n", "c" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 43, "text": [ "5" ] } ], "prompt_number": 43 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Subtract right from the left and assign the result to left (c = a - c)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "c -= a\n", "c" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 44, "text": [ "3" ] } ], "prompt_number": 44 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Multiply right with the left and assign the result to left (c = a * c)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "c *= a\n", "c" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 45, "text": [ "6" ] } ], "prompt_number": 45 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Divide left with the right and assign the result to left (c = c / a)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "c /= a\n", "c" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 46, "text": [ "3.0" ] } ], "prompt_number": 46 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Takes modulus using two operands and assign the result to left (a = d % a)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "d %= a\n", "d" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 47, "text": [ "1" ] } ], "prompt_number": 47 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exponential (power) calculation on operators and assign value to the left (d = d ^ a)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "d **= a\n", "d" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 48, "text": [ "1" ] } ], "prompt_number": 48 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Floor division on operators and assign value to the left (d = d // a)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "d //= a\n", "d" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 24, "text": [ "0" ] } ], "prompt_number": 24 } ], "metadata": {} } ] }