{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Foundations of Computational Economics #4\n", "\n", "by Fedor Iskhakov, ANU\n", "\n", "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "## Python essentials: data types\n", "\n", "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "\n", "\n", "[https://youtu.be/V4LjT3qjMcs](https://youtu.be/V4LjT3qjMcs)\n", "\n", "Description: Variables and memory, binary operations, logical expressions, composite variables types." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Plan for the video\n", "\n", "1. Names and comments \n", "1. Variables and memory \n", "1. Binary operations \n", "1. Logical expressions \n", "1. Composite variables types \n", "\n", "\n", "📖 Kevin Sheppard “Introduction to Python for Econometrics, Statistics\n", "and Data Analysis.” *Chapters: 3, 5, 10, 11, 13, 18, 22*" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Intro\n", "\n", "\n", "\n", "- General–purpose programming language \n", "- Open source \n", "- High level language: slower in general but easier to write code and\n", " develop software \n", "- With special tools and for particular problems is fast (approaching low level\n", " languages) " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Native Python and the Scientific Stack (Python modules)\n", "\n", "- *NumPy* implements fast array processing — vectorization \n", "- *SciPy* is a collection of functions of common scientific\n", " operations (optimization, root finding, linear algebra, interpolation,\n", " numerical integration, etc.) \n", "- *Pandas* is data manipulation package with special data types and\n", " methods \n", "- *Matplotlib* is package for making figures and plots \n", "\n", "\n", "**Today: talking about core Python**" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Coding standard: PEP8\n", "\n", "Python Enhancement Proposal 0008\n", "\n", "[https://www.python.org/dev/peps/pep-0008/](https://www.python.org/dev/peps/pep-0008/)\n", "\n", "- Indentation: 4 spaces per level \n", "- Max line length: 79 characters \n", "- White space: around binary operations + according to precedence of operations \n", "- Comments and docstrings \n", "- Naming conventions " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Names and comments\n", "\n", "**Names of variables** $ \\exists $ certain rules: can only contain\n", "numbers, letters (both upper and lower) and underscores (_)\n", "\n", "**Reserved words**: *and as assert break class continue def del elif else\n", "except exec finally for from global if import in is lambda not or pass\n", "print raise return try while with yield*\n", "\n", "**Comments** made with # at any location of a line" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "hide-output": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# This is a comment" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Basic variable types\n", "\n", "- Boolean \n", "- Integer \n", "- Floating point numbers \n", "- Complex numbers \n", "- Strings " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Boolean\n", "\n", "Record logical **True** or **False**\n", "\n", "Logical opertations + arithmetics" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "hide-output": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = False\n", "y = True\n", "x and y" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "hide-output": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y = 1 < 5\n", "y = y and (4 < 8)\n", "y" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Check that at least one condition is satisfied" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "hide-output": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Modify the code appropriately\n", "z = (1 < 0)\n", "z = (2 >= 4)\n", "z = (5 <= 5)\n", "z = (2 > 4)\n", "z" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Integer" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "hide-output": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x is 1\n", "Type of x is \n", "y is 11\n", "Type of y is \n", "y takes 28 bytes in memory\n" ] } ], "source": [ "import sys\n", "x = 1\n", "print(\"x is %r\" % x)\n", "print(\"Type of x is %s\" % type(x))\n", "\n", "y = 0b1011 #binary (base=2)\n", "# y = 0x10f #hex (base=16)\n", "\n", "print(\"y is %r\" % y)\n", "print(\"Type of y is %s\" % type(y))\n", "print(\"y takes %d bytes in memory\" % sys.getsizeof(y))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Biggest integer that can be stored in memory" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "hide-output": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "y is 6910\n", "Type of y is \n", "y takes 28 bytes in memory\n" ] } ], "source": [ "y = 0b1111111111111111111111111111110\n", "# 1234567890123456789012345678901234567890\n", "y = 0x1afe\n", "print(\"y is %r\" % y)\n", "print(\"Type of y is %s\" % type(y))\n", "print(\"y takes %d bytes in memory\" % sys.getsizeof(y))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Python adjusts the memory for integers as to fit large numbers!" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Arithmetics with integers" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "hide-output": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "155 + 7 = 162 ()\n" ] } ], "source": [ "a = 155\n", "b = 7\n", "c=a+b\n", "print(\"%d + %d = %r (%s)\" % (a,b,c,type(c)))" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "hide-output": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "155 - 50*7 = -195 ()\n" ] } ], "source": [ "c=a-50*b\n", "print(\"%d - 50*%d = %r (%s)\" % (a,b,c,type(c)))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Automatic casting" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "hide-output": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "155 ^ 7 = 2149422977421875 ()\n", "155 / 7 = 22.142857142857142 ()\n" ] } ], "source": [ "a = 155\n", "b = 7\n", "c = a**b\n", "print(\"%d ^ %d = %r (%s)\" % (a,b,c,type(c)))\n", "c = a/b\n", "print(\"%d / %d = %r (%s)\" % (a,b,c,type(c)))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Python adjusts the *type* of integers as to fit the result of arithmetic operation!" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Integer division and remainder" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "hide-output": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "155 // 7 = 22 ()\n", "155 % 7 = 1 ()\n" ] } ], "source": [ "a = 155\n", "b = 7\n", "c = a//b\n", "print(\"%d // %d = %r (%s)\" % (a,b,c,type(c)))\n", "c = a%b\n", "print(\"%d %% %d = %r (%s)\" % (a,b,c,type(c)))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Booleans and integers" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "hide-output": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x is True\n" ] } ], "source": [ "import sys\n", "x = 15 > 10\n", "print(\"x is %r\" % x)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "hide-output": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Type of x is \n", "x takes 28 bytes in memory\n" ] } ], "source": [ "print(\"Type of x is %s\" % type(x))\n", "print(\"x takes %d bytes in memory\" % sys.getsizeof(x))" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "hide-output": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Type of y is \n", "y takes 28 bytes in memory\n" ] } ], "source": [ "y = 36\n", "print(\"Type of y is %s\" % type(y))\n", "print(\"y takes %d bytes in memory\" % sys.getsizeof(y))" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "hide-output": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(,)\n" ] } ], "source": [ "print(bool.__bases__) #see that boolean is a subclass of integer" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Arithmetics with booleans" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "hide-output": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "-4" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "z = (1 < 5) - 5\n", "z" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "hide-output": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "15" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 15\n", "z = x * (x > 10) + x**2 * (x < 10)\n", "z" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "What if $ x=10 $ in the last example?" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Precedence of binary operators\n", "\n", "1. Power (**) \n", "1. Multiple/devide \n", "1. Plus/minus \n", "1. Comparison operators (< >) \n", "1. Equality operators (== !=) \n", "1. Logical operators (and not or) \n", "\n", "\n", "[Full table here](https://docs.python.org/3/reference/expressions.html#operator-precedence)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "hide-output": false, "slideshow": { "slide_type": "slide" } }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y = 5 - 4 > True\n", "y" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "hide-output": false, "slideshow": { "slide_type": "slide" } }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y = 6 < 10 <= 15\n", "y" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "hide-output": false, "slideshow": { "slide_type": "slide" } }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y = 5 / 2**4 < 10 or 15\n", "y" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Good practice\n", "\n", "Use brackets to avoid ambiguity!" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Float (floating point number)\n", "\n", "Representation for **real numbers**" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "hide-output": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x is 183.0\n", "Type of x is \n" ] } ], "source": [ "x = 183.0\n", "print(\"x is %r\" % x)\n", "print(\"Type of x is %s\" % type(x))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Floats have min and max limits" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "hide-output": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x takes 24 bytes in memory\n" ] }, { "data": { "text/plain": [ "sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "print(\"x takes %d bytes in memory\" % sys.getsizeof(x))\n", "\n", "sys.float_info" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Special values: Inf, -Inf, NaN" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "hide-output": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x is inf\n", "Type of x is \n", "x takes 24 bytes in memory\n" ] } ], "source": [ "x = 1.79769319e+308\n", "print(\"x is %r\" % x)\n", "print(\"Type of x is %s\" % type(x))\n", "print(\"x takes %d bytes in memory\" % sys.getsizeof(x))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide-output": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "import math as m\n", "# import numpy as m\n", "x = m.log(0) # implementations may vary in different packages/libraries!\n", "print(\"x is %r\" % x)\n", "print(\"Type of x is %s\" % type(x))\n", "print(\"x takes %d bytes in memory\" % sys.getsizeof(x))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Complex numbers\n", "\n", "Representation for **imaginary numbers**" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "hide-output": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x is (5+1j)\n", "Type of x is \n", "x takes 32 bytes in memory\n" ] } ], "source": [ "x = 1j+5\n", "print(\"x is %r\" % x)\n", "print(\"Type of x is %s\" % type(x))\n", "print(\"x takes %d bytes in memory\" % sys.getsizeof(x))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Euler formula\n", "\n", "$$\n", "e^{i \\pi}+1 = 0\n", "$$" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "hide-output": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x is 1.2246467991473532e-16j\n", "|x| is 0.00000000000000012246\n", "Type of x is \n", "x takes 32 bytes in memory\n" ] } ], "source": [ "from cmath import exp, pi\n", "x=1j\n", "x = exp(x*pi)+1\n", "print(\"x is %r\" % x)\n", "print(\"|x| is %1.20f\" % abs(x))\n", "print(\"Type of x is %s\" % type(x))\n", "print(\"x takes %d bytes in memory\" % sys.getsizeof(x))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Strings" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "hide-output": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "s is 'Hellow world'\n", "Type of s is \n", "Length of \"Hellow world\" is 12\n", "s takes 61 bytes in memory\n" ] } ], "source": [ "s='Hellow world'\n", "print(\"s is %r\" % s)\n", "print(\"Type of s is %s\" % type(s))\n", "print(\"Length of \\\"%s\\\" is %d\" %(s,len(s)))\n", "print(\"s takes %d bytes in memory\" % sys.getsizeof(s))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Slicing strings" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "hide-output": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "s[0:9] is 'Australia'\n", "s[:9] is 'Australia'\n", "s[11:] is 'National University'\n", "s[-10:] is 'University'\n", "s[::3] is 'Atlnaolnei'\n", "s[0:0:-1] is 'ytisrevinU lanoitaN nailartsuA'\n" ] } ], "source": [ "s='Australian National University'\n", "# 012345678901234567890123456789\n", "\n", "print(\"s[0:9] is %r\" % s[0:9]) # from 1st up to and excluing 9th\n", "print(\"s[:9] is %r\" % s[:9]) # the same\n", "print(\"s[11:] is %r\" % s[11:]) # from 11th till the end\n", "print(\"s[-10:] is %r\" % s[-10:]) # 10th last till the end\n", "print(\"s[::3] is %r\" % s[::3]) # from beginning to end with step 3\n", "print(\"s[0:0:-1] is %r\" % s[::-1]) # from end till beginning with step -1 (reverse order)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Slicing strings puzzle" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "hide-output": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "s[0:9] is 'jde4jecc doij naajo rdmp hin0icbdrs1cgdhttuif 7gjxm'\n" ] } ], "source": [ "s='jde4jecc doij naajo rdmp hin0icbdrs1cgdhttuif 7gjxm'\n", "# 012345678901234567890123456789012345678901234567890\n", "\n", "print(\"s[0:9] is %r\" % s[::]) # output \"economics\"" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Strings in the memory" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "hide-output": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Memory(\"a\", 1 symbols) = 50 bytes\n", "Memory(\"aa\", 2 symbols) = 51 bytes\n", "Memory(\"aaa\", 3 symbols) = 52 bytes\n", "Memory(\"aaaa\", 4 symbols) = 53 bytes\n", "Memory(\"aaaaa\", 5 symbols) = 54 bytes\n", "Memory(\"aaaaaa\", 6 symbols) = 55 bytes\n", "Memory(\"aaaaaaa\", 7 symbols) = 56 bytes\n", "Memory(\"aaaaaaaa\", 8 symbols) = 57 bytes\n", "Memory(\"aaaaaaaaa\", 9 symbols) = 58 bytes\n", "Memory(\"aaaaaaaaaa\", 10 symbols) = 59 bytes\n" ] } ], "source": [ "s=''\n", "for i in range(10):\n", " s=s + 'a'\n", " print(\"Memory(\\\"%s\\\", %d symbols) = %d bytes\"%(s,len(s),sys.getsizeof(s)))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Integers in the memory" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "hide-output": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Memory(\"4\") = 28 bytes\n", "Memory(\"16\") = 28 bytes\n", "Memory(\"256\") = 28 bytes\n", "Memory(\"65536\") = 28 bytes\n", "Memory(\"4294967296\") = 32 bytes\n", "Memory(\"18446744073709551616\") = 36 bytes\n", "Memory(\"340282366920938463463374607431768211456\") = 44 bytes\n", "Memory(\"115792089237316195423570985008687907853269984665640564039457584007913129639936\") = 60 bytes\n", "Memory(\"13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084096\") = 96 bytes\n", "Memory(\"179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216\") = 164 bytes\n" ] } ], "source": [ "x=2\n", "for i in range(10):\n", " x**=2\n", " print(\"Memory(\\\"%d\\\") = %d bytes\"%(x,sys.getsizeof(x)))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Floats in the memory" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "hide-output": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Memory(\"4.000000e+00\") = 24 bytes\n", "Memory(\"1.600000e+01\") = 24 bytes\n", "Memory(\"2.560000e+02\") = 24 bytes\n", "Memory(\"6.553600e+04\") = 24 bytes\n", "Memory(\"4.294967e+09\") = 24 bytes\n", "Memory(\"1.844674e+19\") = 24 bytes\n", "Memory(\"3.402824e+38\") = 24 bytes\n", "Memory(\"1.157921e+77\") = 24 bytes\n", "Memory(\"1.340781e+154\") = 24 bytes\n" ] }, { "ename": "OverflowError", "evalue": "(34, 'Result too large')", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mOverflowError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m2.0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mx\u001b[0m\u001b[0;34m**=\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Memory(\\\"%e\\\") = %d bytes\"\u001b[0m\u001b[0;34m%\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0msys\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgetsizeof\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mOverflowError\u001b[0m: (34, 'Result too large')" ] } ], "source": [ "x=2.0\n", "for i in range(10):\n", " x**=2\n", " print(\"Memory(\\\"%e\\\") = %d bytes\"%(x,sys.getsizeof(x)))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Assignment operator" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "hide-output": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Line 1 - Value of b is 100\n", "Line 2 - Value of c is 42\n", "Line 3 - Value of c is 21\n" ] } ], "source": [ "a = 21\n", "b = 10\n", "c = 2\n", "\n", "b **= c\n", "print (\"Line 1 - Value of b is %d\" % b)\n", "\n", "c *= a\n", "print (\"Line 2 - Value of c is %d\" % c)\n", "\n", "c -= a\n", "print (\"Line 3 - Value of c is %d\" % c)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Id(.) function returns unique int for a variable (reference)\n", "\n", "Python implements complex memory management system to avoid unnecessary memory allocation" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "hide-output": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Initial id(x) is 4469753248\n", " id(y) is 4469753248\n", " Now id(y) is 4469753568\n" ] } ], "source": [ "x = 10\n", "print(\"Initial id(x) is %s\" % id(x))\n", "\n", "y = x\n", "print(\" id(y) is %s\" % id(y))\n", "\n", "y +=x\n", "print(\" Now id(y) is %s\" % id(y))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Composite variable types\n", "\n", "- **List** Collection of variables of any types, can be sliced like\n", " strings \n", "- **Tuple** Same as list but immutable (can not be edited) \n", "- **Dictionary** Pairs of keys and values \n", "- **Set** Unique elements of a collection (also has immutable\n", " counterpart) \n", "- **Range** Sequence of integers, useful for loops in the code! " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Lists and tuples" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "hide-output": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x is [True, 5, 5.2, 'string']\n", "Type of x is \n", "x takes 104 bytes in memory\n", "\n", "y is (True,5,5.2,'string')\n", "Type of y is \n", "Y takes 88 bytes in memory\n" ] } ], "source": [ "x = [True, 5, 5.2, 'string',] # trailing comma is ignored\n", "y = (True, 5, 5.2, 'string',)\n", "\n", "print(\"x is %r\" % x)\n", "print(\"Type of x is %s\" % type(x))\n", "print(\"x takes %d bytes in memory\" % sys.getsizeof(x))\n", "print()\n", "print(\"y is (%r,%r,%r,%r)\" % y)\n", "print(\"Type of y is %s\" % type(y))\n", "print(\"Y takes %d bytes in memory\" % sys.getsizeof(y))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Lists vs tuples" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "hide-output": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "ename": "TypeError", "evalue": "'tuple' object does not support item assignment", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m567\u001b[0m\u001b[0;34m;\u001b[0m \u001b[0;31m# lists are mutable\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0my\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m567\u001b[0m\u001b[0;34m;\u001b[0m \u001b[0;31m# tuples are immutable -> ERROR\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" ] } ], "source": [ "x = [True, 5, 5.2, 'string',] # last comma is ignored\n", "y = (True, 5, 5.2, 'string',)\n", "\n", "x[0] = 567; # lists are mutable\n", "y[0] = 567; # tuples are immutable -> ERROR" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Typical list operations" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "hide-output": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n", "[5, 5.2, 'string', 4, (4+2j)]\n", "7\n", "[True, 5, 5.2, 'string', 4, (4+2j), 'again', 586]\n", "[True, 5, 5.2, 4, (4+2j), 'again', 586]\n" ] } ], "source": [ "x = [True, 5, 5.2, 'string', 4, 4+2j, 'again']\n", "print( x[0] ) # first element\n", "print( x[1:-1] ) # slicing as with strings\n", "print( len(x) ) # length of the list\n", "x.append(586) # add a value at the end\n", "print(x)\n", "x.pop(3) # remove fourth element\n", "print(x)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Further learning resources\n", "\n", "- Python book: Kevin Sheppard “Introduction to Python for Econometrics,\n", " Statistics and Data Analysis.” 3rd Edition University of Oxford\n", " Thursday 1st February, 2018. \n", "- Precedence of binary operations\n", " [https://www.tutorialspoint.com/python/operators_precedence_example.htm](https://www.tutorialspoint.com/python/operators_precedence_example.htm) \n", "- Euler formula [https://www.youtube.com/watch?v=F_0yfvm0UoU](https://www.youtube.com/watch?v=F_0yfvm0UoU) \n", "- Euler formula [https://www.youtube.com/watch?v=-dhHrg-KbJ0](https://www.youtube.com/watch?v=-dhHrg-KbJ0) \n", "- Documenting your code\n", " [https://realpython.com/documenting-python-code/](https://realpython.com/documenting-python-code/) " ] } ], "metadata": { "celltoolbar": "Slideshow", "date": 1612589584.414906, "download_nb": false, "filename": "04_python_types.rst", "filename_with_path": "04_python_types", "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.7.6" }, "title": "Foundations of Computational Economics #4" }, "nbformat": 4, "nbformat_minor": 4 }