diff --git a/Clase 3.ipynb b/Clase 3.ipynb index c51438d..1018318 100644 --- a/Clase 3.ipynb +++ b/Clase 3.ipynb @@ -51,7 +51,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": { "collapsed": true }, @@ -73,6 +73,29 @@ "\n" ] }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "(1-2j)" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "c = 1 + 2j\n", + "c.conjugate()" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -83,7 +106,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 7, "metadata": { "collapsed": false }, @@ -94,7 +117,52 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 8, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "(2, 4)" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "rectangulo2x4.base, rectangulo2x4.altura" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "8" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "rectangulo2x4.base = 2\n", + "rectangulo2x4.area()" + ] + }, + { + "cell_type": "code", + "execution_count": 23, "metadata": { "collapsed": false }, @@ -105,7 +173,7 @@ "__main__.Rectangulo" ] }, - "execution_count": 5, + "execution_count": 23, "metadata": {}, "output_type": "execute_result" } @@ -123,7 +191,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 17, "metadata": { "collapsed": false }, @@ -134,7 +202,7 @@ "8" ] }, - "execution_count": 7, + "execution_count": 17, "metadata": {}, "output_type": "execute_result" } @@ -145,7 +213,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 18, "metadata": { "collapsed": false }, @@ -156,7 +224,7 @@ "12" ] }, - "execution_count": 9, + "execution_count": 18, "metadata": {}, "output_type": "execute_result" } @@ -174,7 +242,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 24, "metadata": { "collapsed": false }, @@ -183,23 +251,23 @@ "name": "stdout", "output_type": "stream", "text": [ - "<__main__.Rectangulo object at 0x7f61232ffd68>\n" + "<__main__.Rectangulo object at 0x7f0efda4efd0>\n" ] }, { "data": { "text/plain": [ - "<__main__.Rectangulo at 0x7f61232ffd68>" + "<__main__.Rectangulo at 0x7f0efda4efd0>" ] }, - "execution_count": 12, + "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "print(rectangulo2x4)\n", - "rectangulo2x4\n" + "rectangulo2x4" ] }, { @@ -212,7 +280,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 35, "metadata": { "collapsed": true }, @@ -229,14 +297,36 @@ " def perimetro(self):\n", " return (self.base + self.altura) * 2\n", "\n", - "\n", + " def volumen(self, profundidad):\n", + " return self.area() * profundidad\n", + " \n", " def __str__(self):\n", " return \"{} de {} x {}\".format(type(self).__name__, self.base, self.altura)" ] }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 36, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Rectangulo de 3 x 4\n" + ] + } + ], + "source": [ + "r = Rectangulo(3, 4)\n", + "print(r)" + ] + }, + { + "cell_type": "code", + "execution_count": 39, "metadata": { "collapsed": false }, @@ -244,17 +334,16 @@ { "data": { "text/plain": [ - "'Rectangulo de 3x4'" + "18.0" ] }, - "execution_count": 16, + "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "r = Rectangulo(3, 4)\n", - "str(r)" + "r.volumen(1.5)" ] }, { @@ -269,7 +358,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 31, "metadata": { "collapsed": true }, @@ -280,7 +369,9 @@ " def __init__(self, lado):\n", " \n", " # igualito a papá rectángulo\n", - " super(Cuadrado, self).__init__(lado, lado) \n" + " super(Cuadrado, self).__init__(base=lado, altura=lado) \n", + " #self.base = lado\n", + " #self.altura = lado\n" ] }, { @@ -295,7 +386,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 32, "metadata": { "collapsed": false }, @@ -306,7 +397,7 @@ "8" ] }, - "execution_count": 21, + "execution_count": 32, "metadata": {}, "output_type": "execute_result" } @@ -316,6 +407,47 @@ "cuadrado.perimetro()" ] }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "cuadrado.area()" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Cuadrado de 2 x 2\n" + ] + } + ], + "source": [ + "print(cuadrado)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -327,14 +459,14 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 41, "metadata": { "collapsed": true }, "outputs": [], "source": [ "\n", - "class OVNI(object): # acá fue cuando me fumé \n", + "class OVNI: # acá fue cuando me fumé \n", " def volar(self):\n", " print(\"Soy un %s y estoy volando!\" % self)\n", " return 42\n", @@ -347,7 +479,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 42, "metadata": { "collapsed": false }, @@ -358,7 +490,7 @@ "16" ] }, - "execution_count": 23, + "execution_count": 42, "metadata": {}, "output_type": "execute_result" } @@ -370,7 +502,7 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 43, "metadata": { "collapsed": false }, @@ -388,7 +520,7 @@ "42" ] }, - "execution_count": 24, + "execution_count": 43, "metadata": {}, "output_type": "execute_result" } @@ -408,21 +540,11 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": 44, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Soy un CuadradoVoladorSupersonico de 2 x 2 y estoy volando!\n", - "y estoy volando supersónicamente a 42 metros!\n", - "vuelo dando aletazos\n" - ] - } - ], + "outputs": [], "source": [ "class CuadradoVoladorSupersonico(CuadradoVolador):\n", "\n", @@ -439,7 +561,7 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": 45, "metadata": { "collapsed": false }, @@ -459,7 +581,7 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": 46, "metadata": { "collapsed": false }, @@ -495,12 +617,156 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 50, "metadata": { - "collapsed": true + "collapsed": false }, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "13" + ] + }, + "execution_count": 50, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# %load https://gist.githubusercontent.com/mgaitan/68f072b0243e0c85e85a/raw/9830ddc624c752e2ef661ac0db41355b4fb1514a/clases.py\n", + "class Estudiante:\n", + " def __init__(self, nombre, ingreso):\n", + " self.nombre = nombre\n", + " self.ingreso = ingreso\n", + "\n", + " def años_cursado(self):\n", + " return 2015 - self.ingreso\n", + "\n", + " def __len__(self):\n", + " return self.años_cursado()\n", + "\n", + "\n", + "martin = Estudiante('martin', 2002)\n", + "martin.años_cursado()" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "13" + ] + }, + "execution_count": 52, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(martin)" + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "6.5" + ] + }, + "execution_count": 58, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\n", + "\n", + "\n", + "class Lineal:\n", + " def __init__(self, a, b):\n", + " self.a = a\n", + " self.b = b\n", + "\n", + " def calcular(self, x):\n", + " return self.a*x + self.b\n", + "\n", + " def __call__(self, x):\n", + " return self.calcular(x)\n", + "\n", + "\n", + "\n", + "f1 = Lineal(1, 2)\n", + "f1(x=4.5)" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 55, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "f1(1)" + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "7.3890560989306495" + ] + }, + "execution_count": 59, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\n", + "\n", + "\n", + "import math\n", + "class Exponencial(Lineal):\n", + "\n", + " def calcular(self, x):\n", + " return self.a*x**self.b\n", + "\n", + "\n", + "\n", + "e = Exponencial(1, 2)\n", + "e(math.e)" + ] }, { "cell_type": "markdown", @@ -523,11 +789,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 60, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Overwriting cuadratica.py\n" + ] + } + ], "source": [ "%%writefile cuadratica.py \n", "\n", @@ -551,7 +825,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 61, "metadata": { "collapsed": false }, @@ -575,17 +849,28 @@ }, "outputs": [], "source": [ - "cuadratica.raices?" + "cuadratica.raices" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 62, "metadata": { "collapsed": false }, - "outputs": [], - "source": [ + "outputs": [ + { + "data": { + "text/plain": [ + "(0.3333333333333333, -1.0)" + ] + }, + "execution_count": 62, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ "cuadratica.raices(3, 2, -1)" ] }, @@ -611,15 +896,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 63, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "(0.3482423621500228, -0.5982423621500228)" + ] + }, + "execution_count": 63, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "import cuadratica as cuad # igual que la primera forma pero poniendole un alias (mas breve). \n", "\n", - "cuad.raices?" + "cuad.raices(24,6,-5)" ] }, { @@ -631,7 +927,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 64, "metadata": { "collapsed": false }, @@ -643,7 +939,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 65, "metadata": { "collapsed": true }, @@ -652,6 +948,17 @@ "raices?" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "%" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -700,34 +1007,59 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 66, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mkdir: no se puede crear el directorio «paquete»: El archivo ya existe\r\n" + ] + } + ], "source": [ "%mkdir paquete # creamos un directorio \"paquete\"" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 70, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Overwriting paquete/__init__.py\n" + ] + } + ], "source": [ - "%%writefile paquete/__init__.py # el archivo __init__.py vacio\n", + "%%writefile paquete/__init__.py \n", + " \n", " " ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 71, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Overwriting paquete/modulo.py\n" + ] + } + ], "source": [ "%%writefile paquete/modulo.py\n", "\n", @@ -758,20 +1090,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 72, "metadata": { "collapsed": false }, "outputs": [], "source": [ - "from paquete.modulo import funcion_loca\n", - "funcion_loca()\n", + "from paquete import modulo as m # import funcion_loca\n", + "m.funcion_loca()\n", "\n", "# podes ver el resultado creando una celda tipo Markdown con el contenido:\n", "#\n", "# ![](files/M.bmp)" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "![](files/M.bmp)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -819,22 +1158,61 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 75, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Component,Mol fraction\r\n", + "n2,0.46\r\n", + "co2,3.36\r\n", + "c1,62.36\r\n", + "c2,8.9\r\n", + "c3,5.31\r\n", + "c4,3.01\r\n", + "c5,1.5\r\n", + "c6,1.05\r\n", + "c7,2.0\r\n", + "c7+,12.049\r\n" + ] + } + ], "source": [ - "%cat data/near_critical_oil.csv" + "!cat data/near_critical_oil.csv" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 73, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[['Component', 'Mol fraction'],\n", + " ['n2', '0.46'],\n", + " ['co2', '3.36'],\n", + " ['c1', '62.36'],\n", + " ['c2', '8.9'],\n", + " ['c3', '5.31'],\n", + " ['c4', '3.01'],\n", + " ['c5', '1.5'],\n", + " ['c6', '1.05'],\n", + " ['c7', '2.0'],\n", + " ['c7+', '12.049']]" + ] + }, + "execution_count": 73, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "import csv\n", "\n", @@ -854,7 +1232,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 76, "metadata": { "collapsed": true }, @@ -869,11 +1247,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 77, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Nombre,Peso\r", + "\r\n", + "Juan,92\r", + "\r\n", + "\"La \"\"Mole\"\" Moli\",121\r", + "\r\n", + "Martín,5 kilos de más\r", + "\r\n" + ] + } + ], "source": [ "%cat pesos.csv" ] @@ -893,11 +1286,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 78, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Overwriting circuitos.csv\n" + ] + } + ], "source": [ "%%writefile circuitos.csv\n", "long;lat;CODIGO DE ESTABLECIMIENTO;NOMBRE ESTABLECIMIENTO;DOMICILIO;CIRCUITO;DESDE MESA;HASTA MESA;TOTAL MESAS;Dirección Normalizada\n", @@ -910,6 +1311,132 @@ "-58.373438804697628;-34.610990941996334;8716;COLEGIO NACIONAL BUENOS AIRES;BOLIVAR 263;14;372;381;10;BOLIVAR 263" ] }, + { + "cell_type": "code", + "execution_count": 79, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[{'CIRCUITO': '1',\n", + " 'CODIGO DE ESTABLECIMIENTO': '4369',\n", + " 'DESDE MESA': '19',\n", + " 'DOMICILIO': 'BOLIVAR 1235',\n", + " 'Dirección Normalizada': 'BOLIVAR 1235',\n", + " 'HASTA MESA': '28',\n", + " 'NOMBRE ESTABLECIMIENTO': 'ESC N°3 BERNARDINO RIVADAVIA',\n", + " 'TOTAL MESAS': '10',\n", + " 'lat': '-34.622409314430783',\n", + " 'long': '-58.372807778869245'},\n", + " {'CIRCUITO': '6',\n", + " 'CODIGO DE ESTABLECIMIENTO': '4593',\n", + " 'DESDE MESA': '115',\n", + " 'DOMICILIO': 'LIBERTAD 1257',\n", + " 'Dirección Normalizada': 'LIBERTAD 1257',\n", + " 'HASTA MESA': '136',\n", + " 'NOMBRE ESTABLECIMIENTO': 'COL. NAC.Nº2 D F SARMIENTO',\n", + " 'TOTAL MESAS': '22',\n", + " 'lat': '-34.593569986274616',\n", + " 'long': '-58.384400577996836'},\n", + " {'CIRCUITO': '9',\n", + " 'CODIGO DE ESTABLECIMIENTO': '4409',\n", + " 'DESDE MESA': '192',\n", + " 'DOMICILIO': 'SAN MARTIN 458',\n", + " 'Dirección Normalizada': 'SAN MARTIN 458',\n", + " 'HASTA MESA': '206',\n", + " 'NOMBRE ESTABLECIMIENTO': 'ESC N°13 GRAL SAN MARTIN',\n", + " 'TOTAL MESAS': '15',\n", + " 'lat': '-34.602665313935738',\n", + " 'long': '-58.373914555842283'},\n", + " {'CIRCUITO': '10',\n", + " 'CODIGO DE ESTABLECIMIENTO': '5330',\n", + " 'DESDE MESA': '225',\n", + " 'DOMICILIO': 'LIBERTAD 581',\n", + " 'Dirección Normalizada': 'LIBERTAD 581',\n", + " 'HASTA MESA': '234',\n", + " 'NOMBRE ESTABLECIMIENTO': 'ESC N°7 PTE ROCA',\n", + " 'TOTAL MESAS': '10',\n", + " 'lat': '-34.601733848426591',\n", + " 'long': '-58.383791453750931'},\n", + " {'CIRCUITO': '10',\n", + " 'CODIGO DE ESTABLECIMIENTO': '9026',\n", + " 'DESDE MESA': '252',\n", + " 'DOMICILIO': 'SARMIENTO 1565',\n", + " 'Dirección Normalizada': 'SARMIENTO 1565',\n", + " 'HASTA MESA': '259',\n", + " 'NOMBRE ESTABLECIMIENTO': 'UMSA UNIV DE MUSEO SOCIAL ARG',\n", + " 'TOTAL MESAS': '8',\n", + " 'lat': '-34.605387432219594',\n", + " 'long': '-58.388829352224974'},\n", + " {'CIRCUITO': '14',\n", + " 'CODIGO DE ESTABLECIMIENTO': '5118',\n", + " 'DESDE MESA': '367',\n", + " 'DOMICILIO': 'BOLIVAR 346',\n", + " 'Dirección Normalizada': 'BOLIVAR 346',\n", + " 'HASTA MESA': '371',\n", + " 'NOMBRE ESTABLECIMIENTO': 'ESC POLITECNICA Nº5 M BELGRANO',\n", + " 'TOTAL MESAS': '5',\n", + " 'lat': '-34.611859568515861',\n", + " 'long': '-58.373317952774258'},\n", + " {'CIRCUITO': '14',\n", + " 'CODIGO DE ESTABLECIMIENTO': '8716',\n", + " 'DESDE MESA': '372',\n", + " 'DOMICILIO': 'BOLIVAR 263',\n", + " 'Dirección Normalizada': 'BOLIVAR 263',\n", + " 'HASTA MESA': '381',\n", + " 'NOMBRE ESTABLECIMIENTO': 'COLEGIO NACIONAL BUENOS AIRES',\n", + " 'TOTAL MESAS': '10',\n", + " 'lat': '-34.610990941996334',\n", + " 'long': '-58.373438804697628'}]" + ] + }, + "execution_count": 79, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import csv\n", + "import codecs\n", + "\n", + "with codecs.open('circuitos.csv', encoding='utf8') as csv_file:\n", + " reader = csv.DictReader(csv_file, delimiter=';')\n", + " \n", + " escuelas = [line for line in reader] #o list(reader)\n", + "escuelas" + ] + }, + { + "cell_type": "code", + "execution_count": 83, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{'COL. NAC.Nº2 D F SARMIENTO': '22',\n", + " 'COLEGIO NACIONAL BUENOS AIRES': '10',\n", + " 'ESC N°13 GRAL SAN MARTIN': '15',\n", + " 'ESC N°3 BERNARDINO RIVADAVIA': '10',\n", + " 'ESC N°7 PTE ROCA': '10',\n", + " 'ESC POLITECNICA Nº5 M BELGRANO': '5',\n", + " 'UMSA UNIV DE MUSEO SOCIAL ARG': '8'}" + ] + }, + "execution_count": 83, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "{e['NOMBRE ESTABLECIMIENTO']:e['TOTAL MESAS'] for e in escuelas}" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -925,9 +1452,29 @@ "cell_type": "code", "execution_count": null, "metadata": { - "collapsed": false + "collapsed": true }, "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 85, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "0.4039307048869024" + ] + }, + "execution_count": 85, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "import random\n", "\n", @@ -937,77 +1484,276 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 99, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "10" + ] + }, + "execution_count": 99, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "random.randrange(10) # análogo a range() devuelve un numero aleatorio de la serie" + "random.randrange(-10, 11, 2) # análogo a range() devuelve un numero aleatorio de la serie" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 102, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'A'" + ] + }, + "execution_count": 102, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "random.choice([0.3, 10, 'A']) # elige un elemento al azar de una secuencia no vacía" + ] + }, + { + "cell_type": "code", + "execution_count": 106, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[4, 0, 8, 5, 1]" + ] + }, + "execution_count": 106, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "random.sample(l, k=5) # elige k elementos de la poblacion dada " + ] + }, + { + "cell_type": "code", + "execution_count": 107, "metadata": { "collapsed": false }, "outputs": [], "source": [ - "random.choice([0.3, 10, 'A']) # elige un elemento al azar de una secuencia no vacía" + "l = list(range(10))" + ] + }, + { + "cell_type": "code", + "execution_count": 108, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" + ] + }, + "execution_count": 108, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "l" + ] + }, + { + "cell_type": "code", + "execution_count": 109, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[2, 8, 7, 9, 0, 3, 4, 6, 5, 1]" + ] + }, + "execution_count": 109, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "random.shuffle(l) # \"desordena\" una lista (inline)\n", + "l" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "También tiene muchas funciones de probabilidad" + ] + }, + { + "cell_type": "code", + "execution_count": 110, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "['betavariate',\n", + " 'expovariate',\n", + " 'gammavariate',\n", + " 'lognormvariate',\n", + " 'normalvariate',\n", + " 'paretovariate',\n", + " 'vonmisesvariate',\n", + " 'weibullvariate']" + ] + }, + "execution_count": 110, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "[method for method in dir(random) if method.endswith('variate')]" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 114, "metadata": { "collapsed": false }, "outputs": [], "source": [ - "random.sample(l, k=3) # elige k elementos de la poblacion dada " + "random.normalvariate??" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 115, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The Zen of Python, by Tim Peters\n", + "\n", + "Beautiful is better than ugly.\n", + "Explicit is better than implicit.\n", + "Simple is better than complex.\n", + "Complex is better than complicated.\n", + "Flat is better than nested.\n", + "Sparse is better than dense.\n", + "Readability counts.\n", + "Special cases aren't special enough to break the rules.\n", + "Although practicality beats purity.\n", + "Errors should never pass silently.\n", + "Unless explicitly silenced.\n", + "In the face of ambiguity, refuse the temptation to guess.\n", + "There should be one-- and preferably only one --obvious way to do it.\n", + "Although that way may not be obvious at first unless you're Dutch.\n", + "Now is better than never.\n", + "Although never is often better than *right* now.\n", + "If the implementation is hard to explain, it's a bad idea.\n", + "If the implementation is easy to explain, it may be a good idea.\n", + "Namespaces are one honking great idea -- let's do more of those!\n" + ] + } + ], "source": [ - "l = list(range(10))\n", - "random.shuffle(l) # \"desordena\" una lista (inline)\n", - "l " + "import this" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "También tiene muchas funciones de probabilidad" + "#### Ejercicio\n", + "\n", + "1. Crear un generador de 1000 números aleatorios pertecientes a una curva de probabilidad normal con media 1 y variancia 0.25. \n", + "\n", + "2. Verificar que la media y la variancia son cercanas a las esperadas (Tip: investigar las funciones del módulo `statistics`)\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 119, "metadata": { "collapsed": false }, + "outputs": [ + { + "data": { + "text/plain": [ + "0.9982509327831033" + ] + }, + "execution_count": 119, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sum(random.normalvariate(1, 0.25) for i in range(10000)) / 10000" + ] + }, + { + "cell_type": "code", + "execution_count": 120, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ - "[method for method in dir(random) if method.endswith('variate')]" + "import statistics" ] }, { - "cell_type": "markdown", - "metadata": {}, + "cell_type": "code", + "execution_count": 122, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "0.2503674815857074" + ] + }, + "execution_count": 122, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "#### Ejercicio\n", - "\n", - "1. Crear un generador de 1000 números aleatorios pertecientes a una curva de probabilidad normal con media 1 y variancia 0.25. \n", - "\n", - "2. Verificar que la media y la variancia son cercanas a las esperadas (Tip: investigar las funciones del módulo `statistics`)\n" + "statistics.stdev((random.normalvariate(1, 0.25) for i in range(10000)))" ] }, { @@ -1021,7 +1767,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 124, "metadata": { "collapsed": false }, @@ -1031,24 +1777,35 @@ "\n", "import pickle\n", "\n", - "pickle.dumps(información)" + "info_serial = pickle.dumps(información)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 125, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "['puede', 'ser', {'casi': [], 'cosa': ('!',), 'cualquier': 100}]" + ] + }, + "execution_count": 125, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# en sentido contrario\n", - "pickle.loads(_)" + "pickle.loads(info_serial)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 126, "metadata": { "collapsed": false }, @@ -1059,6 +1816,25 @@ "pickle.dump(información, open('datos.pkl', 'wb'))" ] }, + { + "cell_type": "code", + "execution_count": 128, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "�\u0003]q\u0000(X\u0005\u0000\u0000\u0000puedeq\u0001X\u0003\u0000\u0000\u0000serq\u0002}q\u0003(X\u0004\u0000\u0000\u0000cosaq\u0004X\u0001\u0000\u0000\u0000!q\u0005�q\u0006X\t\u0000\u0000\u0000cualquierq\u0007KdX\u0004\u0000\u0000\u0000casiq\b]q\tue." + ] + } + ], + "source": [ + "!cat datos.pkl" + ] + }, { "cell_type": "code", "execution_count": null, @@ -1079,15 +1855,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 135, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "b'\\x80\\x03c__main__\\nA\\nq\\x00)\\x81q\\x01.'" + ] + }, + "execution_count": 135, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "class A:\n", " \"una clase que no hace absolutamente nada\"\n", - " pass\n", + " dato = 45\n", "\n", "a = A()\n", "\n", @@ -1097,24 +1884,57 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 130, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "bytes" + ] + }, + "execution_count": 130, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "type(a_serializado) # la serializados es como datos binarios \"crudos\"" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 136, "metadata": { - "collapsed": true + "collapsed": false }, "outputs": [], "source": [ - "b= pickle.loads(_)" + "b= pickle.loads(a_serializado)" + ] + }, + { + "cell_type": "code", + "execution_count": 138, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "45" + ] + }, + "execution_count": 138, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "b.dato" ] }, { @@ -1126,11 +1946,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 139, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 139, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "b is a" ] @@ -1144,11 +1975,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 140, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "ename": "AttributeError", + "evalue": "Can't get attribute 'A' on ", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;32mdel\u001b[0m \u001b[0mA\u001b[0m \u001b[1;31m# borramos la clase A del espacio de nombres global\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mpickle\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mloads\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma_serializado\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;31mAttributeError\u001b[0m: Can't get attribute 'A' on " + ] + } + ], "source": [ "del A # borramos la clase A del espacio de nombres global\n", "pickle.loads(a_serializado)" @@ -1163,11 +2006,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 141, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'[\"puede\", \"ser\", {\"cosa\": [\"!\"], \"cualquier\": 100, \"casi\": []}]'" + ] + }, + "execution_count": 141, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "import json\n", "\n", @@ -1185,7 +2039,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 142, "metadata": { "collapsed": true }, @@ -1195,6 +2049,15 @@ "!gedit # abre el programa gedit en linux " ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "metadata": {}, @@ -1204,11 +2067,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 143, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "['\"La \"\"Mole\"\" Moli\",121']" + ] + }, + "execution_count": 143, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "mole = !cat pesos.csv | grep \"Mole\" \n", "mole" @@ -1225,11 +2099,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 144, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 144, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "import subprocess\n", "\n", @@ -1238,13 +2123,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 145, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "b'hola python, soy echo\\n'" + ] + }, + "execution_count": 145, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "subprocess.check_output(['echo', 'hola python, soy echo']) # se ejecuta en el \"kernel\" (ver consola)" + "texto_prograa = subprocess.check_output(['echo', 'hola python, soy echo']) # se ejecuta en el \"kernel\" (ver consola)" ] }, { @@ -1450,7 +2346,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 146, "metadata": { "collapsed": false }, @@ -1473,7 +2369,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 147, "metadata": { "collapsed": false }, @@ -1499,7 +2395,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 148, "metadata": { "collapsed": false }, @@ -1529,7 +2425,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 149, "metadata": { "collapsed": false }, @@ -1538,7 +2434,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "Writing ejemplo_script2.py\n" + "Overwriting ejemplo_script2.py\n" ] } ], @@ -1555,7 +2451,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 151, "metadata": { "collapsed": false }, @@ -1566,7 +2462,7 @@ "'Hola'" ] }, - "execution_count": 5, + "execution_count": 151, "metadata": {}, "output_type": "execute_result" } @@ -1578,7 +2474,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 150, "metadata": { "collapsed": false }, @@ -1609,7 +2505,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 152, "metadata": { "collapsed": false }, @@ -1618,7 +2514,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "Writing ejemplo_argv.py\n" + "Overwriting ejemplo_argv.py\n" ] } ], @@ -1632,7 +2528,49 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 157, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Overwriting suma.py\n" + ] + } + ], + "source": [ + "%%writefile suma.py\n", + "\n", + "if __name__ == '__main__':\n", + " import sys\n", + " print(sum([int(numero) for numero in sys.argv[1:]]))" + ] + }, + { + "cell_type": "code", + "execution_count": 161, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "78\r\n" + ] + } + ], + "source": [ + "!python suma.py 10 34 34" + ] + }, + { + "cell_type": "code", + "execution_count": 154, "metadata": { "collapsed": false }, @@ -1641,12 +2579,12 @@ "name": "stdout", "output_type": "stream", "text": [ - "['ejemplo_argv.py', 'parametro', '10', '--allthenight', '--shampein']\r\n" + "['ejemplo_argv.py', '--allthenight', '--shampein', '--myidol']\r\n" ] } ], "source": [ - "!python ejemplo_argv.py --allthenight --shampein" + "!python ejemplo_argv.py --allthenight --shampein --myidol" ] }, { @@ -1667,7 +2605,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 162, "metadata": { "collapsed": false }, @@ -1688,7 +2626,8 @@ "if __name__ == '__main__':\n", "\n", " parser = argparse.ArgumentParser(description='Procesa una lista de enteros')\n", - " parser.add_argument('enteros', metavar='N', type=int, nargs='+', # uno o mas argumentos. se acumulan en una lista\n", + " # uno o mas argumentos. se acumulan en una lista\n", + " parser.add_argument('enteros', metavar='N', type=int, nargs='+', \n", " help='an integer for the accumulator')\n", " parser.add_argument('--sum', dest='operacion', action='store_const', # si se pasa --sum se usará const \n", " const=sum, default=max, # en vez de default\n", @@ -1700,7 +2639,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 163, "metadata": { "collapsed": false }, @@ -1720,7 +2659,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 164, "metadata": { "collapsed": false }, @@ -1748,7 +2687,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 166, "metadata": { "collapsed": false }, @@ -1757,13 +2696,12 @@ "name": "stdout", "output_type": "stream", "text": [ - "usage: prog.py [-h] [--sum] N [N ...]\r\n", - "prog.py: error: unrecognized arguments: 45\r\n" + "163\r\n" ] } ], "source": [ - "!python3 prog.py 10 2 --sum 45 " + "!python3 prog.py 10 2 45 106 --sum" ] }, {