{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### Python types are Dynamic typed, Strong typed" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Mutable and Immutable types\n", "- Immutable types: `number`, `string`, `tuple`\n", "- Mutable types: `list`, `dict`, `set`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Whats' the different?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Mutable" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a: [1, 2, 3]\n", "b: [1, 2, 3]\n", "a == b True\n", "a is b False\n" ] } ], "source": [ "a = [1, 2, 3] # list\n", "b = [1, 2, 3] # list\n", "\n", "print(\"a: \", a)\n", "print(\"b: \", b)\n", "print(\"a == b \", a == b)\n", "print(\"a is b \", a is b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Immutable" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "c: abc\n", "d: abc\n", "c == d True\n", "c is d True\n" ] } ], "source": [ "c = \"abc\"\n", "d = \"abc\"\n", "\n", "print(\"c: \", c)\n", "print(\"d: \", d)\n", "print(\"c == d \", c == d)\n", "print(\"c is d \", c is d)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The default `str` for python3 is unicode while it's byte code for python2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Dict\n", "### Assign a dict" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# method 1\n", "Tom = {\"name\": \"Tom\", \"age\": 40, \"gender\": \"male\"}\n", "\n", "# method 2\n", "Tom = dict(name=\"Tom\", age=40, gender=\"male\")\n", "\n", "# method 3\n", "Tom = dict(zip([\"Tom\", \"age\", \"gender\"], [\"Tom\", 40, \"male\"]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Key checking" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "We don't know Tom's habit.\n" ] } ], "source": [ "if not \"habit\" in Tom:\n", " print(\"We don't know Tom's habit.\")" ] } ], "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 }