Created
February 2, 2020 02:49
-
-
Save diogorusso/78913f74fbbaf42cdadb48c97267f969 to your computer and use it in GitHub Desktop.
Jupyter Notebook Basics.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "Jupyter Notebook Basics.ipynb", | |
"provenance": [], | |
"authorship_tag": "ABX9TyM/olUYfvJED1HI1jj4+Rms", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/diogorusso/78913f74fbbaf42cdadb48c97267f969/jupyter-notebook-basics.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "d-xJpnK-y_tn", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"from: https://github.com/iitmcvg/Python-Exercises/blob/master/Exercise%201%20-%20Python.ipynb" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "3xSHmd6bi-oz", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "4180d684-cd84-4298-f496-39d8349be152" | |
}, | |
"source": [ | |
"# As well as code, and see their output in real time\n", | |
"print('Hello World')" | |
], | |
"execution_count": 1, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"Hello World\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "c6wsmnsUjIxP", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "ab214e78-9c8d-486f-c106-ea006249706a" | |
}, | |
"source": [ | |
"# This line configures matplotlib(plotting library) to show figures embedded in the notebook, \n", | |
"# instead of opening a new window for each figure. It also imports numpy \n", | |
"# and matplotlib.pyplot in their common namespaces, np and plt\n", | |
"%pylab inline" | |
], | |
"execution_count": 2, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"Populating the interactive namespace from numpy and matplotlib\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "TExlXlhkjQ8s", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"x = np.linspace(0, 5, 10)\n", | |
"y = x ** 2" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "KPPdZ6MUjZ9z", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 295 | |
}, | |
"outputId": "1a80fac8-6349-48f8-8f32-2144e06f0101" | |
}, | |
"source": [ | |
"plt.figure()\n", | |
"plt.plot(x, y, 'r')\n", | |
"plt.xlabel('x')\n", | |
"plt.ylabel('y')\n", | |
"plt.title('This be the quadratic function')\n", | |
"plt.show()" | |
], | |
"execution_count": 4, | |
"outputs": [ | |
{ | |
"output_type": "display_data", | |
"data": { | |
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAX4AAAEWCAYAAABhffzLAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjIsIGh0\ndHA6Ly9tYXRwbG90bGliLm9yZy8li6FKAAAgAElEQVR4nO3de7xVc/7H8dcHJSqK0i+UhnEfU8zp\nYjCDXJrkfo2E0YTfMHIZgxlkMiP6MW6DSSKVSIqkaSQ1alCKzES5jCmVriq6qU7n8/v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8zm25mU7O672Lt448Wdf+IjEXdgc6xL+qeIDP7CbAKeMrdf5B0PXEz\ns6ZAU3d/x8zqA9OA04r8d2xAXXdfZWa1gEnA1e7+VsKlxc7MrgVKgJ3cvVPS9cTNzGYDJe6e9QvW\nirnF/99F3d19PVC+qHvRcvfXgWVJ15Er7r7A3d+J7q8EZhLWdC5aHqyKvq0VfRVn6y2Dme0JnAT0\nS7qWYlDMwV/Rou5FHQppZlz2ObcAAAIRSURBVGYtgEOByclWEr+oy2M6sBgY6+5F/56B+4AbgLKk\nC8khB14xs2lm1j2bOy7m4JeUMLN6wPNAD3f/Kul64ubuG929FWG96jZmVtTdembWCVjs7tOSriXH\njnT3w4CfAb+MunKzopiDX4u6p0DUz/08MNjdhyddTy65+wpgPNAh6VpidgRwStTn/QxwrJkNSrak\n+Ln7/Oh2MTCC0H2dFcUc/FrUvchFJzofB2a6+71J15MLZtbYzBpE93cgDF6YlWxV8XL3m9x9T3dv\nQfh//Jq7d0m4rFiZWd1owAJmVhc4AcjaaL2iDX53LwXKF3WfCQwt9kXdzWwI8Cawv5nNM7NLk64p\nZkcAFxJagNOjr45JFxWzpsB4M/snoXEz1t1TMbwxZZoAk8zsPWAK8LK7j8nWzot2OKeIiFSsaFv8\nIiJSMQW/iEjKKPhFRFJGwS8ikjIKfhGRlFHwi4ikjIJfRCRlFPwiNWBmrc3sn9H8+HWjufGLes4c\nKR66gEukhszsDqAOsAMwz93vTLgkkWpR8IvUUDQH1NvA18CP3X1jwiWJVIu6ekRqblegHlCf0PIX\nKQhq8YvUkJmNJEwT/D3CEpBXJlySSLVsl3QBIoXIzLoCG9z96Wh95zfM7Fh3fy3p2kSqoha/iEjK\nqI9fRCRlFPwiIimj4BcRSRkFv4hIyij4RURSRsEvIpIyCn4RkZT5f7fjec9j5+f+AAAAAElFTkSu\nQmCC\n", | |
"text/plain": [ | |
"<Figure size 432x288 with 1 Axes>" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
} | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "_QBg1QEnm08B", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "adf4c5cc-a9c1-41b7-bd45-a301cb45d6ab" | |
}, | |
"source": [ | |
"ls" | |
], | |
"execution_count": 5, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"\u001b[0m\u001b[01;34msample_data\u001b[0m/\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "TBsSPm9_nJY-", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "12cd4589-de3e-4726-a776-41631de6c56d" | |
}, | |
"source": [ | |
"import math\n", | |
"\n", | |
"x = math.cos(2 * math.pi)\n", | |
"\n", | |
"print(x)" | |
], | |
"execution_count": 6, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"1.0\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "Y8QRXrlqnuY1", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"# variable assignments\n", | |
"x = 1.0\n", | |
"my_variable = 12.2" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "Ypmg22blnyc2", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "86d09c3d-8a6f-4d44-f97f-f8f5c58ea333" | |
}, | |
"source": [ | |
"type(x)" | |
], | |
"execution_count": 9, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"float" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 9 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "VjGkQTyVoqI_", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"x = 1" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "HdPII5Obor6w", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "c1eebd7a-452f-4d64-8e8b-1e58133f3e2b" | |
}, | |
"source": [ | |
"type(x)" | |
], | |
"execution_count": 11, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"int" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 11 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "_w5LTiMGo7HJ", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "7ee5203b-45ba-4d09-d94a-ce000cf12c0a" | |
}, | |
"source": [ | |
"# boolean\n", | |
"b1 = True\n", | |
"b2 = False\n", | |
"\n", | |
"type(b1)" | |
], | |
"execution_count": 12, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"bool" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 12 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "Fklk703hpwB7", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "610d61b2-bc0f-43ff-fae2-e691259cea63" | |
}, | |
"source": [ | |
"# complex numbers: note the use of `j` to specify the imaginary part\n", | |
"x = 1.0 - 1.0j\n", | |
"type(x)" | |
], | |
"execution_count": 13, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"complex" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 13 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "AbP-TuPkqHnN", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "8338615e-270f-409c-9cee-835953f02817" | |
}, | |
"source": [ | |
"\n", | |
"print(x)" | |
], | |
"execution_count": 14, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"(1-1j)\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "tvSk_BvlqJcf", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "76d6a07c-bccf-4e17-c5d8-4db59c63de7d" | |
}, | |
"source": [ | |
"print(x.real, x.imag)" | |
], | |
"execution_count": 15, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"1.0 -1.0\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "okyMV04JpzB5", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "7afd754f-f1d2-41f5-d669-37c34dddcd1b" | |
}, | |
"source": [ | |
"x = 1.0\n", | |
"\n", | |
"# check if the variable x is a float\n", | |
"type(x) is float" | |
], | |
"execution_count": 16, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"True" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 16 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "Nb4mT6dQqeP-", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "757955b3-84fc-4ca7-e8e6-525733c80c3a" | |
}, | |
"source": [ | |
"# check if the variable x is an int\n", | |
"type(x) is int" | |
], | |
"execution_count": 17, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"False" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 17 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "8UaRM2EKqiDr", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "6212972d-c38a-4752-eed0-1a3445b969df" | |
}, | |
"source": [ | |
"isinstance(x, float)" | |
], | |
"execution_count": 18, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"True" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 18 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "miGkni-0qmgH", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "74ba89e2-f873-436e-abc2-f94dee80ac4f" | |
}, | |
"source": [ | |
"x = 1.5\n", | |
"\n", | |
"print(x, type(x))" | |
], | |
"execution_count": 19, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"1.5 <class 'float'>\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "up_3cHborHrA", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "1a899289-d78b-4967-89c7-6de1aac8b23b" | |
}, | |
"source": [ | |
"x = int(x)\n", | |
"\n", | |
"print(x, type(x))" | |
], | |
"execution_count": 20, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"1 <class 'int'>\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "JfR9cRbQrO-b", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "b9866638-d095-42c8-ca50-6eb94ca16cc4" | |
}, | |
"source": [ | |
"z = complex(x)\n", | |
"\n", | |
"print(z, type(z))" | |
], | |
"execution_count": 21, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"(1+0j) <class 'complex'>\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "iXH2PAEyrTyb", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 172 | |
}, | |
"outputId": "f00bc2dc-6e85-4809-b85c-6c21693b1cc3" | |
}, | |
"source": [ | |
"x = float(z)" | |
], | |
"execution_count": 22, | |
"outputs": [ | |
{ | |
"output_type": "error", | |
"ename": "TypeError", | |
"evalue": "ignored", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-22-19c840f40bd8>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mfloat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mz\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[0;31mTypeError\u001b[0m: can't convert complex to float" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "jOUk_5UjrYxn", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 53 | |
}, | |
"outputId": "d190bde3-0318-41d6-aef2-4c4df1511f4a" | |
}, | |
"source": [ | |
"y = bool(z.real)\n", | |
"\n", | |
"print(z.real, \" -> \", y, type(y))\n", | |
"\n", | |
"y = bool(z.imag)\n", | |
"\n", | |
"print(z.imag, \" -> \", y, type(y))" | |
], | |
"execution_count": 23, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"1.0 -> True <class 'bool'>\n", | |
"0.0 -> False <class 'bool'>\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "ojoRRLKQreg1", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "16a19308-420f-409d-96e0-876eed81ad8b" | |
}, | |
"source": [ | |
"1 + 2, 1 - 2, 1 * 2, 1 / 2" | |
], | |
"execution_count": 24, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"(3, -1, 2, 0.5)" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 24 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "UwXj2H1nrjx1", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "00d58017-8b68-4047-83f4-eba97b8ec36b" | |
}, | |
"source": [ | |
"1.0 + 2.0, 1.0 - 2.0, 1.0 * 2.0, 1.0 / 2.0" | |
], | |
"execution_count": 25, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"(3.0, -1.0, 2.0, 0.5)" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 25 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "EyqpeDWarnNt", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "8e0c3367-9558-482c-d310-33b6a020617c" | |
}, | |
"source": [ | |
"# Integer division of float numbers\n", | |
"3.0 // 2.0" | |
], | |
"execution_count": 26, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"1.0" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 26 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "CSeZI5L1rrEo", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "8a540fb7-3a5e-4052-e32d-d8218881e206" | |
}, | |
"source": [ | |
"True and False" | |
], | |
"execution_count": 27, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"False" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 27 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "AXlDeYvqrvQR", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "ed3268f6-1fae-4e31-ab3c-76ee90d8f0ec" | |
}, | |
"source": [ | |
"not False" | |
], | |
"execution_count": 28, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"True" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 28 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "JRrHP4IBryMf", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "bb2caf99-9f34-465f-97f2-a088986b12a6" | |
}, | |
"source": [ | |
"True or False" | |
], | |
"execution_count": 29, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"True" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 29 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "ziGu6Thrr0RC", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "9102f2e5-8ce7-4e19-92a4-0bcb3c53a29c" | |
}, | |
"source": [ | |
"2 > 1, 2 < 1" | |
], | |
"execution_count": 30, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"(True, False)" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 30 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "5f1dSczqr25Q", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "288bd848-bacc-46f5-ff5b-44f316adbd38" | |
}, | |
"source": [ | |
"2 > 2, 2 < 2" | |
], | |
"execution_count": 31, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"(False, False)" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 31 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "CKLJYBMnr5US", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "c6d9afce-fa3a-4240-dbb6-22e839160ecb" | |
}, | |
"source": [ | |
"2 >= 2, 2 <= 2" | |
], | |
"execution_count": 32, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"(True, True)" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 32 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "2NPJm0nDr9lg", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "6506b780-c74e-47ef-88cd-60e1bbe129c7" | |
}, | |
"source": [ | |
"# equality\n", | |
"[1,2] == [1,2]" | |
], | |
"execution_count": 33, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"True" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 33 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "lPvWh5dksAeD", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "936df747-8301-4c94-e8f9-8525b399badd" | |
}, | |
"source": [ | |
"# objects identical?\n", | |
"l1 = l2 = [1,2]\n", | |
"\n", | |
"l1 is l2" | |
], | |
"execution_count": 34, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"True" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 34 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "Ijct5eKksD2H", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "48c96d4f-4f5e-4da3-afa6-52f30def9933" | |
}, | |
"source": [ | |
"s = \"Hello world\"\n", | |
"type(s)" | |
], | |
"execution_count": 35, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"str" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 35 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "NyFUwXwesHU1", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "a759d0e9-df1f-46fc-8288-aa6bd0c393e0" | |
}, | |
"source": [ | |
"# length of the string: the number of characters\n", | |
"len(s)" | |
], | |
"execution_count": 36, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"11" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 36 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "qAcorIDZsNNt", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "1860e473-f483-44ed-de34-2d6d66ff0e57" | |
}, | |
"source": [ | |
"# replace a substring in a string with something else\n", | |
"s2 = s.replace(\"world\", \"test\")\n", | |
"print(s2)" | |
], | |
"execution_count": 37, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"Hello test\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "GLb6DagcsRbA", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "65e6b520-312c-4cdd-8337-631c614dfbac" | |
}, | |
"source": [ | |
"s[0]" | |
], | |
"execution_count": 38, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"'H'" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 38 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "kXYkslOIsVFe", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "51a84168-2942-46e7-e231-7bcc6d858f42" | |
}, | |
"source": [ | |
"s[0:5]" | |
], | |
"execution_count": 39, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"'Hello'" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 39 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "4Sv4C8OxsZlb", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "fd7acead-d7ed-4cff-a722-cb8e4f2ea8ab" | |
}, | |
"source": [ | |
"s[4:5]" | |
], | |
"execution_count": 40, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"'o'" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 40 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "ospZX2-Hsdc8", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "cb764ae2-f5c2-4896-aef7-abea2878772e" | |
}, | |
"source": [ | |
"s[:5]" | |
], | |
"execution_count": 41, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"'Hello'" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 41 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "8-3SS9oWsg08", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "e4e2de06-0821-4cb2-a9b9-a08bdb7f4381" | |
}, | |
"source": [ | |
"s[6:]" | |
], | |
"execution_count": 42, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"'world'" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 42 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "b369lJrdsjiQ", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "8b05d154-07d4-413c-d402-06ff233c1247" | |
}, | |
"source": [ | |
"s[:]" | |
], | |
"execution_count": 43, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"'Hello world'" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 43 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "Qd2CnFmAsm2J", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "a9935dfe-d066-4a40-fa5d-3ca7e53b93a4" | |
}, | |
"source": [ | |
"s[::1]" | |
], | |
"execution_count": 44, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"'Hello world'" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 44 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "6_i3HsL9srd7", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "85c4b499-cb97-4845-c807-6ac37eb74501" | |
}, | |
"source": [ | |
"s[::2]" | |
], | |
"execution_count": 45, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"'Hlowrd'" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 45 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "u9z27AFtsuWL", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "dfe7d63f-f989-42f6-cc72-230ba72cb87d" | |
}, | |
"source": [ | |
"print(\"str1\", \"str2\", \"str3\") # The print statement concatenates strings with a space" | |
], | |
"execution_count": 46, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"str1 str2 str3\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "oosSZhSRs97a", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "978d9156-cd20-429c-c1c6-ea7336f02852" | |
}, | |
"source": [ | |
"print(\"str1\", 1.0, False, -1j) # The print statements converts all arguments to strings" | |
], | |
"execution_count": 47, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"str1 1.0 False (-0-1j)\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "MyQHJ-V5tBGx", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "68cfb276-34b9-44c1-d691-2fee91b7f7de" | |
}, | |
"source": [ | |
"print(\"str1\" + \"str2\" + \"str3\") # strings added with + are concatenated without space" | |
], | |
"execution_count": 48, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"str1str2str3\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "g08K8dGOtEQE", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "d0e828ab-3ea1-4e6b-d0e6-b84d8d0753e8" | |
}, | |
"source": [ | |
"print(\"value = %f\" % 1.0) # we can use C-style string formatting" | |
], | |
"execution_count": 49, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"value = 1.000000\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "cj0TU5-ttHv-", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "b93fa203-d817-412a-dc05-f13122bdeefe" | |
}, | |
"source": [ | |
"# this formatting creates a string\n", | |
"s2 = \"value1 = %.2f. value2 = %d\" % (3.1415, 1.5)\n", | |
"\n", | |
"print(s2)" | |
], | |
"execution_count": 50, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"value1 = 3.14. value2 = 1\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "rzAXW1J_tNGl", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "453b2a71-00c9-4a5c-c5f6-83c7cb6dd6bc" | |
}, | |
"source": [ | |
"# alternative, more intuitive way of formatting a string \n", | |
"s3 = 'value1 = {0}, value2 = {1}'.format(3.1415, 1.5)\n", | |
"\n", | |
"print(s3)" | |
], | |
"execution_count": 51, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"value1 = 3.1415, value2 = 1.5\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "_3mKgrIytQoH", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 53 | |
}, | |
"outputId": "f48baf58-c453-4f84-e0e4-8fa525a3a049" | |
}, | |
"source": [ | |
"l = [1,2,3,4]\n", | |
"\n", | |
"print(type(l))\n", | |
"print(l)" | |
], | |
"execution_count": 53, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"<class 'list'>\n", | |
"[1, 2, 3, 4]\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "fNT7N1oztU_S", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 71 | |
}, | |
"outputId": "78e91da9-243f-451c-8a1b-b21c2cbe9454" | |
}, | |
"source": [ | |
"print(l)\n", | |
"\n", | |
"print(l[1:3])\n", | |
"\n", | |
"print(l[::2])" | |
], | |
"execution_count": 54, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"[1, 2, 3, 4]\n", | |
"[2, 3]\n", | |
"[1, 3]\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "Q51RIIaDtel2", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "cee5bfec-6deb-4d6f-a4a6-9088a8a8ec16" | |
}, | |
"source": [ | |
"l[0]" | |
], | |
"execution_count": 55, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"1" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 55 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "S1PxIpTRti-L", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "a187dde9-51fe-4e91-9e42-ca6fb2a41b37" | |
}, | |
"source": [ | |
"l = [1, 'a', 1.0, 1-1j]\n", | |
"\n", | |
"print(l)" | |
], | |
"execution_count": 56, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"[1, 'a', 1.0, (1-1j)]\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "VWO-NuENtnmb", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "9606e82a-4c86-4e26-d355-a846977481f7" | |
}, | |
"source": [ | |
"nested_list = [1, [2, [3, [4, [5]]]]]\n", | |
"\n", | |
"nested_list" | |
], | |
"execution_count": 57, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"[1, [2, [3, [4, [5]]]]]" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 57 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "FP_mL4a_tuxY", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "291b2b6b-0687-4fab-d4d0-b8c0b32636b1" | |
}, | |
"source": [ | |
"start = 10\n", | |
"stop = 30\n", | |
"step = 2\n", | |
"\n", | |
"range(start, stop, step)" | |
], | |
"execution_count": 58, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"range(10, 30, 2)" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 58 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "_lBWbiTKtymS", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "db677780-c7e4-4aba-811f-a9c1f47d8072" | |
}, | |
"source": [ | |
"# in python 3 range generates an interator, which can be converted to a list using 'list(...)'.\n", | |
"# It has no effect in python 2\n", | |
"list(range(start, stop, step))" | |
], | |
"execution_count": 59, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"[10, 12, 14, 16, 18, 20, 22, 24, 26, 28]" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 59 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "zTmOvOhBt4cv", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "6a46b257-37e9-4eee-ae7b-e77f10eb5a18" | |
}, | |
"source": [ | |
"list(range(-10, 10))" | |
], | |
"execution_count": 60, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"[-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 60 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "Yy2Z36z_t75q", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "26640550-bde9-43a0-86ca-560eeea04303" | |
}, | |
"source": [ | |
"s" | |
], | |
"execution_count": 61, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"'Hello world'" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 61 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "sfwTHKwht_ps", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "3776d55a-2a3c-43bf-fabb-c9b5dd77acdb" | |
}, | |
"source": [ | |
"# convert a string to a list by type casting:\n", | |
"s2 = list(s)\n", | |
"\n", | |
"s2" | |
], | |
"execution_count": 62, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"['H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 62 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "rPXHEpUnuDOy", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "29cfd96a-96ad-4d18-c6d5-0fd52ee36729" | |
}, | |
"source": [ | |
"# sorting lists\n", | |
"s2.sort()\n", | |
"\n", | |
"print(s2)" | |
], | |
"execution_count": 63, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"[' ', 'H', 'd', 'e', 'l', 'l', 'l', 'o', 'o', 'r', 'w']\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "PQykTj98uGhh", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "029fe686-9280-489a-df85-efe10f3fa3a2" | |
}, | |
"source": [ | |
"# create a new empty list\n", | |
"l = []\n", | |
"\n", | |
"# add an elements using `append`\n", | |
"l.append(\"A\")\n", | |
"l.append(\"d\")\n", | |
"l.append(\"d\")\n", | |
"\n", | |
"print(l)" | |
], | |
"execution_count": 64, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"['A', 'd', 'd']\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "gPEFGeJDuJXV", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "f8270e12-2d51-465c-e226-2812a57f7bac" | |
}, | |
"source": [ | |
"l[1] = \"p\"\n", | |
"l[2] = \"p\"\n", | |
"\n", | |
"print(l)" | |
], | |
"execution_count": 65, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"['A', 'p', 'p']\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "TTHLbO0suMYI", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "6804d912-49ce-4dbb-87ea-a6a293c51f5a" | |
}, | |
"source": [ | |
"l[1:3] = [\"d\", \"d\"]\n", | |
"\n", | |
"print(l)" | |
], | |
"execution_count": 66, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"['A', 'd', 'd']\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "eBUEvuD8uRlD", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "3203e0ac-75e2-4614-e07e-8a62c90e623d" | |
}, | |
"source": [ | |
"l.insert(0, \"i\")\n", | |
"l.insert(1, \"n\")\n", | |
"l.insert(2, \"s\")\n", | |
"l.insert(3, \"e\")\n", | |
"l.insert(4, \"r\")\n", | |
"l.insert(5, \"t\")\n", | |
"\n", | |
"print(l)" | |
], | |
"execution_count": 67, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"['i', 'n', 's', 'e', 'r', 't', 'A', 'd', 'd']\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "gkCx5KiKuW7I", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "9a285e2a-0978-4bdc-b4a9-9c7d5de9621c" | |
}, | |
"source": [ | |
"l.remove(\"A\")\n", | |
"\n", | |
"print(l)" | |
], | |
"execution_count": 68, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"['i', 'n', 's', 'e', 'r', 't', 'd', 'd']\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "dELFv4_7uc1D", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "c6ee163b-2e4e-42b8-facb-47146e56c4cd" | |
}, | |
"source": [ | |
"del l[7]\n", | |
"del l[6]\n", | |
"\n", | |
"print(l)" | |
], | |
"execution_count": 69, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"['i', 'n', 's', 'e', 'r', 't']\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "cR9e9wDrufx4", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "cb32f3c9-4bc1-4292-9b57-e9dc2dc37b49" | |
}, | |
"source": [ | |
"point = (10, 20)\n", | |
"\n", | |
"print(point, type(point))" | |
], | |
"execution_count": 70, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"(10, 20) <class 'tuple'>\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "otD-F7lUujam", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 53 | |
}, | |
"outputId": "0175f3aa-3812-4b43-d8ed-a603402e31be" | |
}, | |
"source": [ | |
"x, y = point\n", | |
"\n", | |
"print(\"x =\", x)\n", | |
"print(\"y =\", y)" | |
], | |
"execution_count": 71, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"x = 10\n", | |
"y = 20\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "Ai0_HrqSupAf", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "A6QI_cvGutsL", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"Dictionaries\n", | |
"Dictionaries are also like lists, except that each element is a key-value pair. The syntax for dictionaries is {key1 : value1, ...}:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "cY4dychYuvdm", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 53 | |
}, | |
"outputId": "fdb2b9fb-4447-4823-a166-3b34bb1eef8e" | |
}, | |
"source": [ | |
"params = {\"parameter1\" : 1.0,\n", | |
" \"parameter2\" : 2.0,\n", | |
" \"parameter3\" : 3.0,}\n", | |
"\n", | |
"print(type(params))\n", | |
"print(params)" | |
], | |
"execution_count": 72, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"<class 'dict'>\n", | |
"{'parameter1': 1.0, 'parameter2': 2.0, 'parameter3': 3.0}\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "ZxF5jxGYvCKC", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 71 | |
}, | |
"outputId": "24275940-93c6-4bad-f576-47b3dfad7503" | |
}, | |
"source": [ | |
"print(\"parameter1 = \" + str(params[\"parameter1\"]))\n", | |
"print(\"parameter2 = \" + str(params[\"parameter2\"]))\n", | |
"print(\"parameter3 = \" + str(params[\"parameter3\"]))" | |
], | |
"execution_count": 73, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"parameter1 = 1.0\n", | |
"parameter2 = 2.0\n", | |
"parameter3 = 3.0\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "IoA8lvaVvGBm", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 89 | |
}, | |
"outputId": "9e9961fb-8c86-43d4-d6d0-89b5d4fa968b" | |
}, | |
"source": [ | |
"params[\"parameter1\"] = \"A\"\n", | |
"params[\"parameter2\"] = \"B\"\n", | |
"\n", | |
"# add a new entry\n", | |
"params[\"parameter4\"] = \"D\"\n", | |
"\n", | |
"print(\"parameter1 = \" + str(params[\"parameter1\"]))\n", | |
"print(\"parameter2 = \" + str(params[\"parameter2\"]))\n", | |
"print(\"parameter3 = \" + str(params[\"parameter3\"]))\n", | |
"print(\"parameter4 = \" + str(params[\"parameter4\"]))" | |
], | |
"execution_count": 74, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"parameter1 = A\n", | |
"parameter2 = B\n", | |
"parameter3 = 3.0\n", | |
"parameter4 = D\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "U5Ywvn6bvJW4", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "784ea262-64db-4ba8-cb76-44321e533cfd" | |
}, | |
"source": [ | |
"statement1 = False\n", | |
"statement2 = False\n", | |
"\n", | |
"if statement1:\n", | |
" print(\"statement1 is True\")\n", | |
" \n", | |
"elif statement2:\n", | |
" print(\"statement2 is True\")\n", | |
" \n", | |
"else:\n", | |
" print(\"statement1 and statement2 are False\")" | |
], | |
"execution_count": 75, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"statement1 and statement2 are False\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "aaCQcRGhvNYv", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "3a04f501-510c-4375-ad2f-eec514725aa2" | |
}, | |
"source": [ | |
"statement1 = statement2 = True\n", | |
"\n", | |
"if statement1:\n", | |
" if statement2:\n", | |
" print(\"both statement1 and statement2 are True\")" | |
], | |
"execution_count": 76, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"both statement1 and statement2 are True\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "TSSNwTXAvRZY", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 71 | |
}, | |
"outputId": "46d63fc4-81b6-4d77-bd98-ee4cf0464b95" | |
}, | |
"source": [ | |
"for x in [1,2,3]:\n", | |
" print(x)" | |
], | |
"execution_count": 77, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"1\n", | |
"2\n", | |
"3\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "phTG42myvVLD", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 89 | |
}, | |
"outputId": "13a8faac-8968-47b8-db6b-f196653c48ee" | |
}, | |
"source": [ | |
"for x in range(4): # by default range start at 0\n", | |
" print(x)" | |
], | |
"execution_count": 78, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"0\n", | |
"1\n", | |
"2\n", | |
"3\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "R0CCxfClvX1x", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 125 | |
}, | |
"outputId": "53a6f973-5f80-4ec1-bd8c-0ee4eef0b8a7" | |
}, | |
"source": [ | |
"for x in range(-3,3):\n", | |
" print(x)" | |
], | |
"execution_count": 79, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"-3\n", | |
"-2\n", | |
"-1\n", | |
"0\n", | |
"1\n", | |
"2\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "mZipOH5evbKe", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 89 | |
}, | |
"outputId": "1553b44a-95bf-468b-c83c-fc0928d281b4" | |
}, | |
"source": [ | |
"for word in [\"scientific\", \"computing\", \"with\", \"python\"]:\n", | |
" print(word)" | |
], | |
"execution_count": 80, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"scientific\n", | |
"computing\n", | |
"with\n", | |
"python\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "VHrld9L8vegj", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 89 | |
}, | |
"outputId": "fa5c283e-fe99-44ca-fd35-6466de64d2f8" | |
}, | |
"source": [ | |
"for key, value in params.items():\n", | |
" print(key + \" = \" + str(value))" | |
], | |
"execution_count": 81, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"parameter1 = A\n", | |
"parameter2 = B\n", | |
"parameter3 = 3.0\n", | |
"parameter4 = D\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "5UWRV6y_viB4", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "76a2bf92-25eb-4e61-a9ab-f50ef3ab30a6" | |
}, | |
"source": [ | |
"l1 = [x**2 for x in range(0,5)]\n", | |
"\n", | |
"print(l1)" | |
], | |
"execution_count": 82, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"[0, 1, 4, 9, 16]\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "fAoFyMvtvkm9", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 125 | |
}, | |
"outputId": "72afa2c4-3216-4846-c171-826fe1183a84" | |
}, | |
"source": [ | |
"i = 0\n", | |
"\n", | |
"while i < 5:\n", | |
" print(i)\n", | |
" \n", | |
" i = i + 1\n", | |
" \n", | |
"print(\"done\")" | |
], | |
"execution_count": 83, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"0\n", | |
"1\n", | |
"2\n", | |
"3\n", | |
"4\n", | |
"done\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "Mcv-pJmSvoVL", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"def func0(): \n", | |
" print(\"test\")" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "fhyewgDTvrnm", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "17c53401-0d9e-4af3-c9ea-1db5832f902f" | |
}, | |
"source": [ | |
"func0()" | |
], | |
"execution_count": 85, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"test\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "TKuBG1Zwvw3t", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"def func1(s):\n", | |
" \"\"\"\n", | |
" Print a string 's' and tell how many characters it has \n", | |
" \"\"\"\n", | |
" \n", | |
" print(s + \" has \" + str(len(s)) + \" characters\")" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "pgQyQrJlvzmm", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 107 | |
}, | |
"outputId": "5246a6c2-a7cb-4af5-b353-d4abc738f630" | |
}, | |
"source": [ | |
"help(func1)" | |
], | |
"execution_count": 87, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"Help on function func1 in module __main__:\n", | |
"\n", | |
"func1(s)\n", | |
" Print a string 's' and tell how many characters it has\n", | |
"\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "uzEfgRPkv4qN", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "1a3635c7-9c67-47b4-ce70-9e938945ab97" | |
}, | |
"source": [ | |
"func1(\"test\")" | |
], | |
"execution_count": 88, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"test has 4 characters\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "RVjmUB8ev8Yl", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"def square(x):\n", | |
" \"\"\"\n", | |
" Return the square of x.\n", | |
" \"\"\"\n", | |
" return x ** 2" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "u9u0K4xGwCLw", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "bf426eda-6ca3-4883-b5f8-278a82a3ea34" | |
}, | |
"source": [ | |
"square(4)" | |
], | |
"execution_count": 90, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"16" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 90 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "TCU61P2cwFyH", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"def powers(x):\n", | |
" \"\"\"\n", | |
" Return a few powers of x.\n", | |
" \"\"\"\n", | |
" return x ** 2, x ** 3, x ** 4" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "3XN9rMSDwI0D", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "e05606bb-0334-4663-ed1e-d1b38a7bdd1a" | |
}, | |
"source": [ | |
"powers(3)" | |
], | |
"execution_count": 92, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"(9, 27, 81)" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 92 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "truu-K72wMdn", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "1d7c1e6d-6804-42df-af56-ca33cd7d0c1e" | |
}, | |
"source": [ | |
"x2, x3, x4 = powers(3)\n", | |
"\n", | |
"print(x3)" | |
], | |
"execution_count": 93, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"27\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "P8BNA0GewP_A", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"def myfunc(x, p=2, debug=False):\n", | |
" if debug:\n", | |
" print(\"evaluating myfunc for x = \" + str(x) + \" using exponent p = \" + str(p))\n", | |
" return x**p" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "16S6SGwqwT1a", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "0be91b12-fae8-480c-9765-b35677e0eb6c" | |
}, | |
"source": [ | |
"\n", | |
"myfunc(5)" | |
], | |
"execution_count": 95, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"25" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 95 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "9PhWe7INwXGc", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 53 | |
}, | |
"outputId": "fe9342a0-7eb9-4609-9cc5-c926ecaa21cd" | |
}, | |
"source": [ | |
"myfunc(5, debug=True)" | |
], | |
"execution_count": 96, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"evaluating myfunc for x = 5 using exponent p = 2\n" | |
], | |
"name": "stdout" | |
}, | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"25" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 96 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "A_IDfn5nwbMB", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 53 | |
}, | |
"outputId": "520f8179-e6d8-4775-d868-564d719d95ed" | |
}, | |
"source": [ | |
"myfunc(p=3, debug=True, x=7)" | |
], | |
"execution_count": 97, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"evaluating myfunc for x = 7 using exponent p = 3\n" | |
], | |
"name": "stdout" | |
}, | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"343" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 97 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "k_kmFdwqwfDI", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"f1 = lambda x: x**2\n", | |
" \n", | |
"# is equivalent to \n", | |
"\n", | |
"def f2(x):\n", | |
" return x**2" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "bF8vcztUwiIL", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "eeedac16-4fe9-4cc9-a821-60752585b5cd" | |
}, | |
"source": [ | |
"f1(2), f2(2)" | |
], | |
"execution_count": 99, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"(4, 4)" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 99 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "VYwuRp1bwomh", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "f5c251ea-dccc-40e8-cb09-328eb06609b1" | |
}, | |
"source": [ | |
"# map is a built-in python function\n", | |
"map(lambda x: x**2, range(-3,4))" | |
], | |
"execution_count": 101, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"<map at 0x7f400254c048>" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 101 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "f1AbFOVKw2w2", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "91bc3d7f-252f-4209-9681-cb71dd7d7ca4" | |
}, | |
"source": [ | |
"# in python 3 we can use `list(...)` to convert the iterator to an explicit list\n", | |
"list(map(lambda x: x**2, range(-3,4)))" | |
], | |
"execution_count": 102, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"[9, 4, 1, 0, 1, 4, 9]" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 102 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "p_NX238zw3ZG", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "rtfM4uBuw_UE", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"Classes are the key features of object-oriented programming. A class is a structure for representing an object and the operations that can be performed on the object.\n", | |
"\n", | |
"In Python a class can contain attributes (variables) and methods (functions).\n", | |
"\n", | |
"A class is defined almost like a function, but using the class keyword, and the class definition usually contains a number of class method definitions (a function in a class).\n", | |
"\n", | |
"Each class method should have an argument self as its first argument. This object is a self-reference.\n", | |
"\n", | |
"Some class method names have special meaning, for example:\n", | |
"\n", | |
"__init__: The name of the method that is invoked when the object is first created.\n", | |
"__str__ : A method that is invoked when a simple string representation of the class is needed, as for example when printed.\n", | |
"There are many more, see http://docs.python.org/2/reference/datamodel.html#special-method-names" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "5GkihJyxxX07", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"class Point:\n", | |
" \"\"\"\n", | |
" Simple class for representing a point in a Cartesian coordinate system.\n", | |
" \"\"\"\n", | |
" \n", | |
" def __init__(self, x, y):\n", | |
" \"\"\"\n", | |
" Create a new Point at x, y.\n", | |
" \"\"\"\n", | |
" self.x = x\n", | |
" self.y = y\n", | |
" \n", | |
" def translate(self, dx, dy):\n", | |
" \"\"\"\n", | |
" Translate the point by dx and dy in the x and y direction.\n", | |
" \"\"\"\n", | |
" self.x += dx\n", | |
" self.y += dy\n", | |
" \n", | |
" def __str__(self):\n", | |
" return(\"Point at [%f, %f]\" % (self.x, self.y))" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "JO6Vki-AxbYH", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "d8848b14-c830-45e5-c943-3fd76aadb511" | |
}, | |
"source": [ | |
"p1 = Point(0, 0) # this will invoke the __init__ method in the Point class\n", | |
"\n", | |
"print(p1) # this will invoke the __str__ method" | |
], | |
"execution_count": 109, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"Point at [0.000000, 0.000000]\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "NVhJ8441xeks", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 53 | |
}, | |
"outputId": "cf6cf0a4-1d50-43c6-fa9d-e98d90a0a153" | |
}, | |
"source": [ | |
"p2 = Point(1, 1)\n", | |
"\n", | |
"p1.translate(0.25, 1.5)\n", | |
"\n", | |
"print(p1)\n", | |
"print(p2)" | |
], | |
"execution_count": 110, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"Point at [0.250000, 1.500000]\n", | |
"Point at [1.000000, 1.000000]\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "HYiz_0pwxqxM", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "5YjR5grixvfu", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"Modules\n", | |
"One of the most important concepts in good programming is to reuse code and avoid repetitions.\n", | |
"\n", | |
"The idea is to write functions and classes with a well-defined purpose and scope, and reuse these instead of repeating similar code in different part of a program (modular programming). The result is usually that readability and maintainability of a program is greatly improved. What this means in practice is that our programs have fewer bugs, are easier to extend and debug/troubleshoot.\n", | |
"\n", | |
"Python supports modular programming at different levels. Functions and classes are examples of tools for low-level modular programming. Python modules are a higher-level modular programming construct, where we can collect related variables, functions and classes in a module. A python module is defined in a python file (with file-ending .py), and it can be made accessible to other Python modules and programs using the import statement." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "BdiLOBtDxwJ4", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "fa08bab0-554d-4268-a1c7-3e6c71e9de5b" | |
}, | |
"source": [ | |
"%%file mymodule.py\n", | |
"\"\"\"\n", | |
"Example of a python module. Contains a variable called my_variable,\n", | |
"a function called my_function, and a class called MyClass.\n", | |
"\"\"\"\n", | |
"\n", | |
"my_variable = 0\n", | |
"\n", | |
"def my_function():\n", | |
" \"\"\"\n", | |
" Example function\n", | |
" \"\"\"\n", | |
" return my_variable\n", | |
" \n", | |
"class MyClass:\n", | |
" \"\"\"\n", | |
" Example class.\n", | |
" \"\"\"\n", | |
"\n", | |
" def __init__(self):\n", | |
" self.variable = my_variable\n", | |
" \n", | |
" def set_variable(self, new_value):\n", | |
" \"\"\"\n", | |
" Set self.variable to a new value\n", | |
" \"\"\"\n", | |
" self.variable = new_value\n", | |
" \n", | |
" def get_variable(self):\n", | |
" return self.variable" | |
], | |
"execution_count": 111, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"Writing mymodule.py\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "ZFGBd6f1x3Cu", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"import mymodule" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "a8hWbP-qx6Yh", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 845 | |
}, | |
"outputId": "f407505a-50ec-4e49-91a3-ee70614b1311" | |
}, | |
"source": [ | |
"\n", | |
"help(mymodule)" | |
], | |
"execution_count": 113, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"Help on module mymodule:\n", | |
"\n", | |
"NAME\n", | |
" mymodule\n", | |
"\n", | |
"DESCRIPTION\n", | |
" Example of a python module. Contains a variable called my_variable,\n", | |
" a function called my_function, and a class called MyClass.\n", | |
"\n", | |
"CLASSES\n", | |
" builtins.object\n", | |
" MyClass\n", | |
" \n", | |
" class MyClass(builtins.object)\n", | |
" | Example class.\n", | |
" | \n", | |
" | Methods defined here:\n", | |
" | \n", | |
" | __init__(self)\n", | |
" | Initialize self. See help(type(self)) for accurate signature.\n", | |
" | \n", | |
" | get_variable(self)\n", | |
" | \n", | |
" | set_variable(self, new_value)\n", | |
" | Set self.variable to a new value\n", | |
" | \n", | |
" | ----------------------------------------------------------------------\n", | |
" | Data descriptors defined here:\n", | |
" | \n", | |
" | __dict__\n", | |
" | dictionary for instance variables (if defined)\n", | |
" | \n", | |
" | __weakref__\n", | |
" | list of weak references to the object (if defined)\n", | |
"\n", | |
"FUNCTIONS\n", | |
" my_function()\n", | |
" Example function\n", | |
"\n", | |
"DATA\n", | |
" my_variable = 0\n", | |
"\n", | |
"FILE\n", | |
" /content/mymodule.py\n", | |
"\n", | |
"\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "6OksIfmox9aV", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "c3d0c2f8-e80b-4bad-9962-c83f439382b3" | |
}, | |
"source": [ | |
"mymodule.my_variable" | |
], | |
"execution_count": 114, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"0" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 114 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "rpCZmjjByBsb", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "0423f9b7-e22e-4f03-f5b1-99090562cc49" | |
}, | |
"source": [ | |
"mymodule.my_function()" | |
], | |
"execution_count": 115, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"0" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 115 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "KM7uLtOtyFSS", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "181db29f-ef28-4dea-9a4b-11ebb941e684" | |
}, | |
"source": [ | |
"my_class = mymodule.MyClass() \n", | |
"my_class.set_variable(10)\n", | |
"my_class.get_variable()" | |
], | |
"execution_count": 116, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"10" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 116 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "fKDAUiToyIB6", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 172 | |
}, | |
"outputId": "2a5ae0f8-25f5-4e74-9dc1-faa00dd9d5ce" | |
}, | |
"source": [ | |
"reload(mymodule) # works only in python 2" | |
], | |
"execution_count": 117, | |
"outputs": [ | |
{ | |
"output_type": "error", | |
"ename": "NameError", | |
"evalue": "ignored", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-117-1a6cfac4c71b>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mreload\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmymodule\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# works only in python 2\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[0;31mNameError\u001b[0m: name 'reload' is not defined" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "7w3kS1tLyMG8", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "D2jvrJKvyUv1", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"Exceptions\n", | |
"In Python errors are managed with a special language construct called \"Exceptions\". When errors occur exceptions can be raised, which interrupts the normal program flow and fallback to somewhere else in the code where the closest try-except statement is defined.\n", | |
"\n", | |
"To generate an exception we can use the raise statement, which takes an argument that must be an instance of the class BaseException or a class derived from it." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "GUpgND0ZyVNK", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 172 | |
}, | |
"outputId": "40f4805c-56ed-4555-fee4-954110818c5c" | |
}, | |
"source": [ | |
"raise Exception(\"description of the error\")" | |
], | |
"execution_count": 118, | |
"outputs": [ | |
{ | |
"output_type": "error", | |
"ename": "Exception", | |
"evalue": "ignored", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[0;31mException\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-118-c32f93e4dfa0>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mException\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"description of the error\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[0;31mException\u001b[0m: description of the error" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "-1s1ZHbFycre", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 53 | |
}, | |
"outputId": "2a97d3eb-efc5-4306-d10c-af70183908d0" | |
}, | |
"source": [ | |
"try:\n", | |
" print(\"test\")\n", | |
" # generate an error: the variable test is not defined\n", | |
" print(test)\n", | |
"except:\n", | |
" print(\"Caught an exception\")" | |
], | |
"execution_count": 119, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"test\n", | |
"<numpy._pytesttester.PytestTester object at 0x7f401a7d0048>\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "dUFbz3o5yh-Y", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 53 | |
}, | |
"outputId": "d6d40da4-b6e0-4c44-c041-f13d2bc7d322" | |
}, | |
"source": [ | |
"try:\n", | |
" print(\"test\")\n", | |
" # generate an error: the variable test is not defined\n", | |
" print(test)\n", | |
"except Exception as e:\n", | |
" print(\"Caught an exception:\" + str(e))" | |
], | |
"execution_count": 120, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"test\n", | |
"<numpy._pytesttester.PytestTester object at 0x7f401a7d0048>\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "ypRvcjTAyncD", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment