{ "cells": [ { "cell_type": "markdown", "id": "d1e21d00-0d0c-41d1-9b54-32b9aa8cfa34", "metadata": { "tags": [] }, "source": [ "Creación de funciones de usuario\n", "===\n", "\n", "* Duración de la lección: 9 min" ] }, { "cell_type": "markdown", "id": "b5321cd9-d06e-48e0-be60-b3647c7d2b9d", "metadata": {}, "source": [ "## Operación de las funciones internas" ] }, { "cell_type": "code", "execution_count": 1, "id": "3ca53bb0-b3a0-4750-9865-2564248732d0", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hola mundo cruel!\n" ] } ], "source": [ "#\n", "# Ejemplo de funciones internas del lenguaje\n", "# =============================================================================\n", "# La función print() internamente genera\n", "# la salida en pantalla de sus argumentos\n", "#\n", "print(\"Hola mundo cruel!\")" ] }, { "cell_type": "code", "execution_count": 2, "id": "51ead632-eef5-4aa9-ad4d-f643e3b0ef6e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "21" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#\n", "# La función sum() toma una lista y retorna su suma.\n", "#\n", "sum([1, 2, 3, 4, 5, 6])" ] }, { "cell_type": "code", "execution_count": 3, "id": "507122da-3355-4bb3-a678-66cd5c53f211", "metadata": {}, "outputs": [], "source": [ "#\n", "# Abstracción del concepto f(x) = x ** 2\n", "# =============================================================================\n", "#\n", "# x -> argumento de la función\n", "# return -> indica que retorna\n", "#\n", "def square(x):\n", " x_squared = x ** 2\n", " return x_squared" ] }, { "cell_type": "code", "execution_count": 4, "id": "d0efd3b0-c902-457f-8686-76cf87ed2b3a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "4" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "#\n", "# Diferencia argumento posicional y nombrado\n", "# =============================================================================\n", "#\n", "display(\n", " square(2),\n", " square(x=2),\n", ")" ] }, { "cell_type": "code", "execution_count": 5, "id": "e7767d56-9cd8-476a-8914-d6ab81340e0b", "metadata": {}, "outputs": [], "source": [ "#\n", "# Las funciones puden ser llamadas dentro de otras\n", "# =============================================================================\n", "#\n", "def sum_of_squares(x, y):\n", " return square(x) + square(y)" ] }, { "cell_type": "code", "execution_count": 6, "id": "ebd33e39-6684-4829-b77e-7729dacc27b1", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum_of_squares(1, 2)" ] }, { "cell_type": "code", "execution_count": 7, "id": "03671794-1bfe-4a6b-b563-ee08e500e385", "metadata": {}, "outputs": [], "source": [ "#\n", "# Error causado cuando el argumento no existe\n", "# =============================================================================\n", "#" ] }, { "cell_type": "code", "execution_count": 8, "id": "2f29d87f-967b-4bb3-80a0-1c772fd905f2", "metadata": {}, "outputs": [], "source": [ "#\n", "# Error causado al usar una variable inexistente\n", "# =============================================================================\n", "#" ] }, { "cell_type": "code", "execution_count": 9, "id": "b1d15a97-8e79-42e3-bc17-2afdf6c74bd6", "metadata": {}, "outputs": [], "source": [ "#\n", "# Error causado al llamar la función con un número equivocado de argumentos.\n", "# =============================================================================\n", "#" ] }, { "cell_type": "code", "execution_count": 10, "id": "68efb090-0aef-4d50-921b-8b19465a30b7", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a: 1\n", "b: 2\n", "c: 3\n" ] } ], "source": [ "#\n", "# Asignación de los valores de las llamadas a los argumentos de la función.\n", "# =============================================================================\n", "#\n", "def my_function(a, b, c):\n", " print(\"a:\", a)\n", " print(\"b:\", b)\n", " print(\"c:\", c)\n", "\n", "\n", "my_function(1, 2, 3)" ] }, { "cell_type": "code", "execution_count": 11, "id": "4ac10222-2ab7-4882-8730-9f2bc27fb8fe", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "None" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "NoneType" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "#\n", "# Asignación del resultado de una función cuando la función no retorna nada\n", "# =============================================================================\n", "#\n", "def function_returning_nothing(x):\n", " pass\n", "\n", "\n", "y = function_returning_nothing(1)\n", "\n", "display(\n", " y,\n", " type(y),\n", ")" ] }, { "cell_type": "code", "execution_count": 12, "id": "608c6158-5f89-49f8-b269-9395e7e42f75", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 2)" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#\n", "# Retorno de varios valores\n", "# =============================================================================\n", "#\n", "def function_returning_a_tuple(x, y):\n", " return x, y\n", "\n", "\n", "function_returning_a_tuple(1, 2)" ] }, { "cell_type": "code", "execution_count": 13, "id": "31651e26-3fad-444d-b688-f0b660b2969a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#\n", "# Se debe verificar que la función siempre retorne un valor\n", "# =============================================================================\n", "#\n", "def comparing_function(x, y):\n", " if x > y:\n", " return True\n", "\n", "\n", "comparing_function(2, 1)" ] }, { "cell_type": "code", "execution_count": 14, "id": "5b577c05-ff4d-431d-aad1-e76fec201652", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "None" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "NoneType" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "#\n", "# Esta llamada no retorna un valor\n", "# =============================================================================\n", "#\n", "display(\n", " comparing_function(1, 2),\n", " type(comparing_function(1, 2)),\n", ")" ] }, { "cell_type": "code", "execution_count": 15, "id": "3fa28868-4943-4353-a493-d2249ee454ec", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#\n", "# Corrección\n", "#\n", "def comparing_function(x, y):\n", " if x > y:\n", " return True\n", " return False\n", "\n", "\n", "comparing_function(1, 2)" ] }, { "cell_type": "code", "execution_count": 16, "id": "1dbe6894-5607-4609-b135-14bc9235f52c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'No hace nada'" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#\n", "# Define una función sin cuerpo pero documentada\n", "#\n", "def my_function():\n", " \"\"\"No hace nada\"\"\"\n", "\n", "\n", "my_function.__doc__" ] }, { "cell_type": "code", "execution_count": 17, "id": "c6a332cf-5e31-4d3d-80d6-9c23297f348e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on function my_function in module __main__:\n", "\n", "my_function()\n", " No hace nada\n", "\n" ] } ], "source": [ "help(my_function)" ] }, { "cell_type": "code", "execution_count": 24, "id": "ae573d67-bcd7-4ad3-9229-d58d0f76e70f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on built-in function sum in module builtins:\n", "\n", "sum(iterable, start=0, /)\n", " Return the sum of a 'start' value (default: 0) plus an iterable of numbers\n", " \n", " When the iterable is empty, return the start value.\n", " This function is intended specifically for use with numeric values and may\n", " reject non-numeric types.\n", "\n" ] } ], "source": [ "help(sum)" ] }, { "cell_type": "code", "execution_count": null, "id": "2d342f46-8101-4d7f-9f27-9de357359473", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "bab90268-4313-4247-8f78-ba9d66065792", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "86b99676-3aff-4f01-9403-c17ff0be1bcd", "metadata": {}, "source": [ "## Ejemplo" ] }, { "cell_type": "markdown", "id": "66c51814-164a-492f-a298-b43394aa40ee", "metadata": {}, "source": [ "Se desea construir una función que cuente las ocurrencias de cada elemento en una columna de una tabla." ] }, { "cell_type": "code", "execution_count": 18, "id": "d1deeb77-3c7e-460b-b2fd-ff971bdb42e3", "metadata": {}, "outputs": [], "source": [ "#\n", "# Se descargan los datos de la tabla\n", "#\n", "tweets_url = (\n", " \"https://raw.githubusercontent.com/jdvelasq/datalabs/master/datasets/tweets.csv\"\n", ")\n", "!wget --quiet {tweets_url} -P /tmp/" ] }, { "cell_type": "code", "execution_count": 19, "id": "5b6fe6a7-8460-4601-8665-9a94649f94a3", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "contributors\n", "coordinates\n", "created_at\n", "entities\n", "extended_entities\n", "favorite_count\n", "favorited\n", "filter_level\n", "geo\n", "id\n", "id_str\n", "in_reply_to_screen_name\n", "in_reply_to_status_id\n", "in_reply_to_status_id_str\n", "in_reply_to_user_id\n", "in_reply_to_user_id_str\n", "is_quote_status\n", "lang\n", "place\n", "possibly_sensitive\n", "quoted_status\n", "quoted_status_id\n", "quoted_status_id_str\n", "retweet_count\n", "retweeted\n", "retweeted_status\n", "source\n", "text\n", "timestamp_ms\n", "truncated\n", "user\n" ] } ], "source": [ "#\n", "# Se cargan los datos en una tabla\n", "#\n", "import pandas as pd\n", "\n", "tweets_df = pd.read_csv(\"/tmp/tweets.csv\")\n", "\n", "for column in sorted(tweets_df.columns):\n", " print(column)" ] }, { "cell_type": "code", "execution_count": 20, "id": "a3f33031-05bd-4080-942a-0f054d0ff95c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | contributors | \n", "coordinates | \n", "created_at | \n", "entities | \n", "extended_entities | \n", "favorite_count | \n", "favorited | \n", "filter_level | \n", "geo | \n", "id | \n", "... | \n", "quoted_status_id | \n", "quoted_status_id_str | \n", "retweet_count | \n", "retweeted | \n", "retweeted_status | \n", "source | \n", "text | \n", "timestamp_ms | \n", "truncated | \n", "user | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | \n", "NaN | \n", "NaN | \n", "Tue Mar 29 23:40:17 +0000 2016 | \n", "{'hashtags': [], 'user_mentions': [{'screen_na... | \n", "{'media': [{'sizes': {'large': {'w': 1024, 'h'... | \n", "0 | \n", "False | \n", "low | \n", "NaN | \n", "714960401759387648 | \n", "... | \n", "NaN | \n", "NaN | \n", "0 | \n", "False | \n", "{'retweeted': False, 'text': \".@krollbondratin... | \n", "<a href=\"http://twitter.com\" rel=\"nofollow\">Tw... | \n", "RT @bpolitics: .@krollbondrating's Christopher... | \n", "1459294817758 | \n", "False | \n", "{'utc_offset': 3600, 'profile_image_url_https'... | \n", "
1 | \n", "NaN | \n", "NaN | \n", "Tue Mar 29 23:40:17 +0000 2016 | \n", "{'hashtags': [{'text': 'cruzsexscandal', 'indi... | \n", "{'media': [{'sizes': {'large': {'w': 500, 'h':... | \n", "0 | \n", "False | \n", "low | \n", "NaN | \n", "714960401977319424 | \n", "... | \n", "NaN | \n", "NaN | \n", "0 | \n", "False | \n", "{'retweeted': False, 'text': '@dmartosko Cruz ... | \n", "<a href=\"http://twitter.com\" rel=\"nofollow\">Tw... | \n", "RT @HeidiAlpine: @dmartosko Cruz video found..... | \n", "1459294817810 | \n", "False | \n", "{'utc_offset': None, 'profile_image_url_https'... | \n", "
2 | \n", "NaN | \n", "NaN | \n", "Tue Mar 29 23:40:17 +0000 2016 | \n", "{'hashtags': [], 'user_mentions': [], 'symbols... | \n", "NaN | \n", "0 | \n", "False | \n", "low | \n", "NaN | \n", "714960402426236928 | \n", "... | \n", "NaN | \n", "NaN | \n", "0 | \n", "False | \n", "NaN | \n", "<a href=\"http://www.facebook.com/twitter\" rel=... | \n", "Njihuni me Zonjën Trump !!! | Ekskluzive https... | \n", "1459294817917 | \n", "False | \n", "{'utc_offset': 7200, 'profile_image_url_https'... | \n", "
3 | \n", "NaN | \n", "NaN | \n", "Tue Mar 29 23:40:17 +0000 2016 | \n", "{'hashtags': [], 'user_mentions': [], 'symbols... | \n", "NaN | \n", "0 | \n", "False | \n", "low | \n", "NaN | \n", "714960402367561730 | \n", "... | \n", "7.149239e+17 | \n", "7.149239e+17 | \n", "0 | \n", "False | \n", "NaN | \n", "<a href=\"http://twitter.com/download/android\" ... | \n", "Your an idiot she shouldn't have tried to grab... | \n", "1459294817903 | \n", "False | \n", "{'utc_offset': None, 'profile_image_url_https'... | \n", "
4 | \n", "NaN | \n", "NaN | \n", "Tue Mar 29 23:40:17 +0000 2016 | \n", "{'hashtags': [], 'user_mentions': [{'screen_na... | \n", "NaN | \n", "0 | \n", "False | \n", "low | \n", "NaN | \n", "714960402149416960 | \n", "... | \n", "NaN | \n", "NaN | \n", "0 | \n", "False | \n", "{'retweeted': False, 'text': 'The anti-America... | \n", "<a href=\"http://twitter.com/download/iphone\" r... | \n", "RT @AlanLohner: The anti-American D.C. elites ... | \n", "1459294817851 | \n", "False | \n", "{'utc_offset': -18000, 'profile_image_url_http... | \n", "
5 rows × 31 columns
\n", "