{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h1 align=\"center\">Lists Tuples</h1>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##Lists"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "L1 = ['A', 'B', 'C']\n",
    "L2 = list(['A', 'B', 'C'])\n",
    "L3 = list(('A', 'B', 'C'))\n",
    "L4 = sorted(list({'A', 'B', 'C'}))\n",
    "L5 = sorted(list({'A': 1, 'B': 2, 'C': 3}))\n",
    "L6 = list('ABC')\n",
    "L7 = L1.copy()\n",
    "L1 == L2 == L3 == L4 == L5 == L6 == L7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "L = [1, 3, 5, 7, 3, 5, 5]\n",
    "print(len(L))\n",
    "print(0 not in L)\n",
    "print(5 in L)\n",
    "print(L.index(5))\n",
    "print(L.count(5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "L = []\n",
    "L.append(3)\n",
    "print(L)\n",
    "L.append(9)\n",
    "print(L)\n",
    "L.insert(1, 7)\n",
    "print(L)\n",
    "L.insert(-2, 5)\n",
    "print(L)\n",
    "# 0 can be replaced by any negative integer\n",
    "# at most equal to minus the length of L\n",
    "L.insert(0, 1)\n",
    "print(L)\n",
    "# 5 can be replaced by any larger integer\n",
    "L.insert(5, 11)\n",
    "print(L)\n",
    "L.append(13)\n",
    "print(L)\n",
    "L.extend([15])\n",
    "print(L)\n",
    "L.extend([17, 19])\n",
    "print(L)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "L = [0, 2, 4, 6, 8, 0, 2, 4, 6, 8]\n",
    "L.pop()\n",
    "print(L)\n",
    "L.pop(8)\n",
    "print(L)\n",
    "L.pop(-8)\n",
    "print(L)\n",
    "L.pop(3)\n",
    "print(L)\n",
    "L.remove(4)\n",
    "print(L)\n",
    "L.clear()\n",
    "print(L)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "L = [2, 0, 5, 1, 4, 3]\n",
    "L.sort()\n",
    "print(L)\n",
    "L.sort(reverse = True)\n",
    "print(L)\n",
    "L.reverse()\n",
    "print(L)\n",
    "print()\n",
    "\n",
    "L = ['A', 'd', 'C', 'b']\n",
    "L.sort()\n",
    "print(L)\n",
    "L.sort(key = str.upper)\n",
    "print(L)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "L = [1] + [2, 3]\n",
    "print(L)\n",
    "L += [4, 5, 6]\n",
    "print(L)\n",
    "L = [1, 2] * 3\n",
    "print(L)\n",
    "L *= 2\n",
    "print(L)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "L1 = [1]\n",
    "L2 = L1\n",
    "print(L2)\n",
    "L2 += [2, 3]\n",
    "L2 = L2 + [4, 5]\n",
    "print(L2)\n",
    "print(L1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "L = [0, 2, 4, 6]\n",
    "\n",
    "print(L[1])\n",
    "L[1] = 1\n",
    "print(L)\n",
    "print(L[-2])\n",
    "L[-2] = 3\n",
    "print(L)\n",
    "print()\n",
    "\n",
    "print(L[2:])\n",
    "L[2:] = []\n",
    "print(L)\n",
    "L[2:] = [4, 5, 6]\n",
    "print(L)\n",
    "print(L[-2:])\n",
    "L[-2:] = [1, 2, 3]\n",
    "print(L)\n",
    "print()\n",
    "\n",
    "print(L[: 4])\n",
    "L[: 4] = [3]\n",
    "print(L)\n",
    "print(L[1: -1])\n",
    "L[1: -1] = [2, 4, 2]\n",
    "print(L)\n",
    "# The second occurrence of 3 can be replaced by any smaller integer\n",
    "print(L[3: 3])\n",
    "L[3: 3] = [1]\n",
    "print(L)\n",
    "print()\n",
    "\n",
    "print(L[: : 2])\n",
    "print(L[1: : 2])\n",
    "L[1: : 2] = [-2, -1, -3]\n",
    "print(L)\n",
    "print(L[: 4: 2])\n",
    "print(L[1: 5: 2])\n",
    "print(L[: : -2])\n",
    "print(L[: 1: -2])\n",
    "print(L[4: : -2])\n",
    "print(L[4: 1: -2])\n",
    "L[4: 1: -2] = [-2, -4]\n",
    "print(L)\n",
    "del L[0]\n",
    "print(L)\n",
    "del L[-3: 0: -1]\n",
    "print(L)\n",
    "del L[: ]\n",
    "print(L)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "L = [[[1, 2, 3, 4], [5], [6, 7], [8, 9, 10]],\n",
    "     [[11, 12], [13, 14, 15, 16, 18], [19, 20]],\n",
    "     [[21], [22], [23, 24, 25], [26, 27], [28, 29, 30]]]\n",
    "print(L[1])\n",
    "print(L[1][1])\n",
    "print(L[1][1][3])\n",
    "print()\n",
    "\n",
    "L1 = L\n",
    "L[1][1][3] = [0]\n",
    "print(L)\n",
    "L[1][1] = 0\n",
    "print(L)\n",
    "L[1] = []\n",
    "print(L)\n",
    "print(L1 == L)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from copy import deepcopy\n",
    "\n",
    "L = [list(range(1, 4)), list(range(4, 7)), list(range(7, 10))]\n",
    "print(L)\n",
    "L1 = L.copy()\n",
    "L2 = deepcopy(L)\n",
    "L[0] = [0]\n",
    "L[1][1] = -5\n",
    "print(L)\n",
    "print(L1)\n",
    "print(L2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##Tuples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "T1 = 'A', 'B', 'C'\n",
    "T2 = tuple(['A', 'B', 'C'])\n",
    "T3 = tuple(('A', 'B', 'C'))\n",
    "T4 = tuple({'A', 'B', 'C'})\n",
    "T5 = tuple({'A': 1, 'B': 2, 'C': 3})\n",
    "T6 = tuple('ABC')\n",
    "print(T1, T2, T3, T4, T5, T6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "T = 1, 3, 5, 7, 3, 5, 5\n",
    "print(len(T))\n",
    "print(0 not in T)\n",
    "print(5 in T)\n",
    "print(T.index(5))\n",
    "print(T.count(5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "T = (1,) + (2, 3)\n",
    "print(T)\n",
    "T += (4, 5, 6)\n",
    "print(T)\n",
    "T = (1, 2) * 3\n",
    "print(T)\n",
    "T *= 2\n",
    "print(T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "T1 = 1,\n",
    "T2 = T1\n",
    "print(T2)\n",
    "T2 += (2, 3)\n",
    "T2 = T2 + (4, 5)\n",
    "print(T2)\n",
    "print(T1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "T = (0, 2, 4, 6)\n",
    "\n",
    "print(T[1])\n",
    "print(T[-2])\n",
    "print()\n",
    "\n",
    "print(T[2:])\n",
    "print(T[-2:])\n",
    "print()\n",
    "\n",
    "print(T[: 3])\n",
    "print(T[1: -1])\n",
    "print(T[3: 3])\n",
    "print()\n",
    "\n",
    "print(T[: : 2])\n",
    "print(T[: 2: 2])\n",
    "print(T[: : -2])\n",
    "print(T[: 1: -2])\n",
    "print(T[2: : -2])\n",
    "print(T[4: 0: -2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "T = ([[1, 2, 3, 4], [5], [6, 7], [8, 9, 10]],\n",
    "     [[11, 12], [13, 14, 15, 16, 18], [19, 20]],\n",
    "     [[21], [22], [23, 24, 25], [26, 27], [28, 29, 30]])\n",
    "print(T[1])\n",
    "print(T[1][1])\n",
    "print(T[1][1][3])\n",
    "print()\n",
    "\n",
    "T1 = T\n",
    "T[1][1][3] = [0]\n",
    "print(T)\n",
    "T[1][1] = 0\n",
    "print(T)\n",
    "print(T1 == T)"
   ]
  }
 ],
 "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 12 August 2015, 09:52:36 AM.

file: lists_tuples.ipynb


Back to top

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