{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h1 align=\"center\">Built-ins</h1>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##Types and classes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "type(True), type(1), type (1.), type('1'), type([]), type(()), type({}), type({1})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "x = 1.\n",
    "type(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "type(bool), type(int), type(float), type(str), type(list), type(type)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "isinstance(1, int), isinstance(1, float), isinstance({2}, set), isinstance({}, set)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "issubclass(bool, int), issubclass(int, float)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##Literal representations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "bool(True), bool(-17.8), bool('17'), bool([]), bool({}), bool(None), bool('')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "int('17'), int('10001', 2), int('21', 8), int('15', 12), int('11', 16), int('h', 22)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "int(True), int(17), int(-17), int(0b10001), int(0o21), int(0x11), int(17.8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "bin(True), bin(17), bin(-17), bin(0b10001), bin(0o21), bin(0x11)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "oct(True), oct(17), oct(-17), oct(0b10001), oct(0o21), oct(0x11)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "hex(True), hex(17), hex(-17), hex(0b10001), hex(0o21), hex(0x11)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "float(False), float(17), float(17.), float(17e-1), float(0.17E2), float('170E-1') "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "print(complex(0), complex(1.5), complex(4.5, -7.5))\n",
    "print(complex(0).conjugate(), complex(1.5).conjugate(), complex(4.5, -7.5).conjugate())\n",
    "print(complex(0).real, complex(1.5).real, complex(4.5, -7.5).real)\n",
    "print(complex(0).imag, complex(1.5).imag, complex(4.5, -7.5).imag)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##Operations on numbers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "abs(-3.8), abs(-2), abs(3.8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "print(round(-3.6), round(-3.5), round(-3.4), round(3.4), round(3.5), round(3.6))\n",
    "print(round(-3.1235, 3), round(-3.123456, 4), round(3.123456, 4), round(3.1235, 3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "divmod(13, 5), divmod(-13., 5), divmod(13., -5.), divmod(-13, -5.), divmod(3.5, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "print(pow(2, 3), pow(-2., -1), pow(4, -0.5), pow(-1, 0.5), pow(-3.8, 0), pow(1j, 1j))\n",
    "print(pow(-2, 3, 3), pow(-2, 3, 5), pow(2, 3, 3), pow(2, 3, 5))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##Strings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "print(ord('c'), ord('\\xf7'), ord('•'), ord('\\u2603'))\n",
    "print(chr(99), chr(247), chr(8226), chr(9731))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "ascii('Ça me tient ∞ment à cœur\\t')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "repr('A string'), str('A string')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "a = 2; b = 4.5\n",
    "eval('(a + 3.5) * (b + 5.)')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "message = input('Input your message: ')\n",
    "print('Your message is:', message)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##Creating and processing iteratables"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "print(tuple(range(4)), tuple(range(4, 8)), tuple(range(4, 16, 3)))\n",
    "print(tuple(range(-8)), tuple(range(-8, -4)), tuple(range(-16, -8, -3)))\n",
    "print()\n",
    "\n",
    "print(range(2, 8, 2).count(4), range(2, 8, 2).count(5), range(2, 8, 2).index(4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "print(list(enumerate({10, 15, 25, 40})))\n",
    "print(dict(enumerate((10, 15, 25, 40), 3)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "print(list(zip([1, 2, 3, 4], [11, 12, 13], [21, 22, 23, 24, 25], [31, 32, 33])))\n",
    "print(list(zip(*zip([1, 2, 3, 4], [11, 12, 13], [21, 22, 23, 24, 25], [31, 32, 33]))))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "print(list(map(sorted,\n",
    "               [(1, 2, 3), (7, 5, 4, 6, 8), (10, 9), (11,)])))\n",
    "print(set(map(int.__add__,\n",
    "              [1, 2, 3, 4, 5], [11, 12, 13])))\n",
    "print(dict(map(lambda x: (x, 2 * x),\n",
    "               (0, 1, 2, 3, 4, 5))))\n",
    "print(tuple(map(lambda x, y, z: len({x, y, z}) == 2,\n",
    "                [1, 20, 30, -4, 5, 60], [-1, 20, 31, 4, 5], [1, 20, 32, 4, -5, 61, 70])))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "print(list(filter(str.isupper,\n",
    "                 {'A': 1, 'b': 2, 'c': 3, 'D': 4, 'E': 5})))\n",
    "print(tuple(filter(lambda x: x < 10,\n",
    "                [-1, 20, -3, -4, 50, 60])))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "print(sum([3, 1, 7, 5]))\n",
    "print(sum({3: 1, 1: 1, 7: 2, 5: 3}))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "print(min((3, 1, 7, 5)))\n",
    "# min() accepts also an arbitrary number of arguments\n",
    "print(min(3, 1, 7, 5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "print(max({3 : 10, 1: 10, 7: 10, 5: 10}))\n",
    "# max() accepts also an arbitrary number of arguments\n",
    "print(max(3, 1, 7, 5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "sorted([2, 1, 3, 4, 0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "list(reversed((2, 1, 3, 4, 0)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "print(any([0, 0, 0, 0, 0]))\n",
    "print(any((1, 0, 0, 1, 0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "print(all({5, 2, 3, 1, 4}))\n",
    "print(all({5: 1, 2: 1, 3: 1, 0: 1, 4: 4}))"
   ]
  }
 ],
 "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, 10:56:00 AM, last modified Wednesday 19 August 2015, 10:12:25 AM.

file: built_ins.ipynb


Back to top

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