{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h1 align=\"center\">Operators</h1>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##Arithmetic operators:\n",
    "- addition\n",
    "- subtraction\n",
    "- multiplication\n",
    "- division\n",
    "- floor division\n",
    "- modulus\n",
    "- exponentiation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "3 + +5, 3 + 5., -3 - True, 0X2a + -0xB, -0O70 - 0o30, -0B101 + 0b110, -2j + 5J"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "+3 - -5, 3. - 5, (-2.5 - -1j) - (3.5 - 1j)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "3e-4 * 5.0E0, -3 * False, -2.5j * 5J"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "13 / 5, -13. / 5, 13. / -5., -13. / -5., 13j / 5J"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "x // and x % y are such that:\n",
    "- x = (x // y) * y + x % y;\n",
    "- -|y|  <  x % y  <  |y|;\n",
    "- the sign of x % y is equal to the sign of y."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "13 // 5, -13. // 5, 13. // -5., -13 // -5., 3.5 // 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "13 % 5, -13. % 5, 13. % -5., -13 % -5., 3.5 % 2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$i^i = e^{-\\pi/2}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "2 ** 3, -2. ** -1, 4 ** -0.5, (-1) ** 0.5, -3.8 ** 0, 1j ** 1j"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##Bitwise operators:\n",
    "- ones complement\n",
    "- AND\n",
    "- OR (inclusive or)\n",
    "- XOR (exclusive or)\n",
    "- right shift\n",
    "- left shift "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "...\n",
    "\n",
    "-4&nbsp;:&nbsp;...1...100\n",
    "\n",
    "-3&nbsp;:&nbsp;...1...101\n",
    "\n",
    "-2&nbsp;:&nbsp;...1...110\n",
    "\n",
    "-1&nbsp;:&nbsp;...1...111\n",
    "\n",
    "&nbsp;0&nbsp;:&nbsp;...0...000\n",
    " \n",
    "&nbsp;1&nbsp;:&nbsp;...0...001\n",
    " \n",
    "&nbsp;2&nbsp;:&nbsp;...0...010\n",
    " \n",
    "&nbsp;3&nbsp;:&nbsp;...0...011\n",
    "\n",
    "&nbsp;4&nbsp;:&nbsp;...0...100\n",
    "\n",
    "..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "~34, ~3, ~2, ~1, ~0, ~-1, ~-2, ~-3, ~-34 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "9 & 10, -9 & 10, 9 & -10, -9 & -10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "9 | 10, -9 | 10, 9 | -10, -9 | -10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "9 ^ 10, -9 ^ 10, 9 ^ -10, -9 ^ -10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "5 >> 2, -5 >> 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "5 << 2, -5 << 2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Relational operators:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "1 == 1., 0 != False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "1 <= 1 < 2 >= 2 > 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Boolean operators"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "not 3.3, not []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "True and (1, 2, 3), () and 1 / 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "{} or {0}, {'A': 1, 'B': 2} or 1 / 0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##Identity operators"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "2 is 2, [] is [], 2 is not 2., True is not 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##Membership operators"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "'B' in {'A': 1, 'B': 2, 'C': 3}, 0 not in [[0]]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##Assignment operators"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "x = 1\n",
    "x <<= 2\n",
    "print(x)\n",
    "x >>= 1\n",
    "print(x)\n",
    "x += 1\n",
    "print(x)\n",
    "x -= 2\n",
    "print(x)\n",
    "x *= 3\n",
    "print(x)\n",
    "x /= 2\n",
    "print(x)\n",
    "x %= 2\n",
    "print(x)\n",
    "x //= -2\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##Precedence and associativity"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "From highest to lowest precedence:\n",
    "\n",
    "- \\*\\*\n",
    "- ~ &emsp; unary + &emsp; unary -\n",
    "- \\* &emsp; / &emsp; // &emsp; %\n",
    "- binary + &emsp; binary -\n",
    "- \\>\\> &emsp; <<\n",
    "- &\n",
    "- ^\n",
    "- |\n",
    "- <= &emsp; < &emsp; > &emsp; >= &emsp; == &emsp; != &emsp; is &emsp; is not &emsp; in  &emsp; not in\n",
    "- = &emsp; (and all augmented assignments)\n",
    "- not\n",
    "- and\n",
    "- or"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "20 / 10 / 2, 20 // (10 // 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "7 % 5 % 3, 7 % (5 % 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "2 ** 3 ** 2, (2 ** 3) ** 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "1 << 2 << 1, 1 << (2 << 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "8 >> 2 >> 1, 8 >> (2 >> 1)"
   ]
  }
 ],
 "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.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}

Resource created Wednesday 05 August 2015, 11:14:14 AM, last modified Wednesday 05 August 2015, 11:14:48 AM.

file: operators.ipynb


Back to top

COMP9021 15s2 (Principles of Programming) is powered by WebCMS3
CRICOS Provider No. 00098G