diff --git a/Introduction_to_Colab_and_Python.ipynb b/Introduction_to_Colab_and_Python.ipynb new file mode 100644 index 0000000..27d192f --- /dev/null +++ b/Introduction_to_Colab_and_Python.ipynb @@ -0,0 +1,434 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "accelerator": "GPU", + "colab": { + "name": "Introduction to Colab and Python", + "provenance": [], + "private_outputs": true, + "collapsed_sections": [], + "toc_visible": true, + "include_colab_link": true + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "code", + "metadata": { + "cellView": "form", + "colab_type": "code", + "id": "Eq10uEbw0E4l", + "colab": {} + }, + "source": [ + "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n", + "# you may not use this file except in compliance with the License.\n", + "# You may obtain a copy of the License at\n", + "#\n", + "# https://www.apache.org/licenses/LICENSE-2.0\n", + "#\n", + "# Unless required by applicable law or agreed to in writing, software\n", + "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", + "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", + "# See the License for the specific language governing permissions and\n", + "# limitations under the License." + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "YHI3vyhv5p85" + }, + "source": [ + "## **Introduction to Colab and Python**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "OVi775ZJ2bsy" + }, + "source": [ + "\n", + " \n", + " \n", + "
\n", + " Run in Google Colab\n", + " \n", + " View source on GitHub\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "F8YVA_634OFk" + }, + "source": [ + "Welcome to this Colab where you will get a quick introduction to the Python programming language and the environment used for the course's exercises: Colab.\n", + "\n", + "Colab is a Python development environment that runs in the browser using Google Cloud.\n", + "\n", + "For example, to print \"Hello World\", just hover the mouse over [ ] and press the play button to the upper left. Or press shift-enter to execute." + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "bdLopQtuAJi4", + "colab": {} + }, + "source": [ + "# Never mind this statement, for compatibility reasons\n", + "from __future__ import absolute_import, division, print_function, unicode_literals" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "X9uIpOS2zx7k", + "colab": {} + }, + "source": [ + "print(\"Hello World\")" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "wwJGmDrQ0EoB" + }, + "source": [ + "## Functions, Conditionals, and Iteration\n", + "Let's create a Python function, and call it from a loop." + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "pRllo2HLfXiu", + "colab": {} + }, + "source": [ + "def HelloWorldXY(x, y):\n", + " if (x < 10):\n", + " print(\"Hello World, x was < 10\")\n", + " elif (x < 20):\n", + " print(\"Hello World, x was >= 10 but < 20\")\n", + " else:\n", + " print(\"Hello World, x was >= 20\")\n", + " return x + y\n", + "\n", + "for i in range(8, 25, 5): # i=8, 13, 18, 23 (start, stop, step)\n", + " print(\"--- Now running with i: {}\".format(i))\n", + " r = HelloWorldXY(i,i)\n", + " print(\"Result from HelloWorld: {}\".format(r))" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "lHNmDCh0JpVP", + "colab": {} + }, + "source": [ + "print(HelloWorldXY(1,2))" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "kiZG7uhm8qCF" + }, + "source": [ + "Easy, right?\n", + "\n", + "If you want a loop starting at 0 to 2 (exclusive) you could do any of the following" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "m8YQN1H41L-Y", + "colab": {} + }, + "source": [ + "print(\"Iterate over the items. `range(2)` is like a list [0,1].\")\n", + "for i in range(2):\n", + " print(i)\n", + "\n", + "print(\"Iterate over an actual list.\")\n", + "for i in [0,1]:\n", + " print(i)\n", + "\n", + "print(\"While works\")\n", + "i = 0\n", + "while i < 2:\n", + " print(i)\n", + " i += 1" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "vIgmFZq4zszl", + "colab": {} + }, + "source": [ + "print(\"Python supports standard key words like continue and break\")\n", + "while True:\n", + " print(\"Entered while\")\n", + " break" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "5QyOUhFw1OUX" + }, + "source": [ + "## Numpy and lists\n", + "Python has lists built into the language.\n", + "However, we will use a library called numpy for this.\n", + "Numpy gives you lots of support functions that are useful when doing Machine Learning.\n", + "\n", + "Here, you will also see an import statement. This statement makes the entire numpy package available and we can access those symbols using the abbreviated 'np' syntax." + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "4Dxk4q-jzEy4", + "colab": {} + }, + "source": [ + "import numpy as np # Make numpy available using np.\n", + "\n", + "# Create a numpy array, and append an element\n", + "a = np.array([\"Hello\", \"World\"])\n", + "a = np.append(a, \"!\")\n", + "print(\"Current array: {}\".format(a))\n", + "print(\"Printing each element\")\n", + "for i in a:\n", + " print(i)\n", + "\n", + "print(\"\\nPrinting each element and their index\")\n", + "for i,e in enumerate(a):\n", + " print(\"Index: {}, was: {}\".format(i, e))" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "RTa8_9G3LV03", + "colab": {} + }, + "source": [ + "print(\"\\nShowing some basic math on arrays\")\n", + "b = np.array([0,1,4,3,2])\n", + "print(\"Max: {}\".format(np.max(b)))\n", + "print(\"Average: {}\".format(np.average(b)))\n", + "print(\"Max index: {}\".format(np.argmax(b)))" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "9YaGj5n4LW7P", + "colab": {} + }, + "source": [ + "print(\"\\nYou can print the type of anything\")\n", + "print(\"Type of b: {}, type of b[0]: {}\".format(type(b), type(b[0])))" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "V6ilVhi9LXn_", + "colab": {} + }, + "source": [ + "print(\"\\nUse numpy to create a [3,3] dimension array with random number\")\n", + "c = np.random.rand(3, 3)\n", + "print(c)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "W_Q-DkFCLYGA", + "colab": {} + }, + "source": [ + "print(\"\\nYou can print the dimensions of arrays\")\n", + "print(\"Shape of a: {}\".format(a.shape))\n", + "print(\"Shape of b: {}\".format(b.shape))\n", + "print(\"Shape of c: {}\".format(c.shape))\n", + "print(\"...Observe, Python uses both [0,1,2] and (0,1,2) to specify lists\")" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "c-Jk4dG91dvD" + }, + "source": [ + "## Colab Specifics" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "G0cGd8sHEmKi" + }, + "source": [ + "Colab is a virtual machine you can access directly. To run commands at the VM's terminal, prefix the line with an exclamation point (!).\n", + "\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "cLkfhyzq0W2y", + "colab": {} + }, + "source": [ + "print(\"\\nDoing $ls on filesystem\")\n", + "!ls -l\n", + "!pwd" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "gR2WTN1cOZ1n", + "colab": {} + }, + "source": [ + "print(\"Install numpy\") # Just for test, numpy is actually preinstalled in all Colab instances\n", + "!pip install numpy" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "QuWRpQdatAIU" + }, + "source": [ + "**Exercise**\n", + "\n", + "Create a code cell underneath this text cell and add code to:\n", + "\n", + "\n", + "* List the path of the current directory (pwd)\n", + "* Go to / (cd) and list the content (ls -l)" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "xU-cJbMCR61P", + "colab": {} + }, + "source": [ + "!pwd\n", + "!cd /\n", + "!ls -l\n", + "print(\"Hello\")" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "7b5jv0ouFREV" + }, + "source": [ + "All usage of Colab in this course is completely free or charge. Even GPU usage is provided free of charge for some hours of usage every day.\n", + "\n", + "**Using GPUs**\n", + "* Many of the exercises in the course executes more quickly by using GPU runtime: Runtime | Change runtime type | Hardware accelerator | GPU\n", + "\n", + "**Some final words on Colab**\n", + "* You execute each cell in order, you can edit & re-execute cells if you want\n", + "* Sometimes, this could have unintended consequences. For example, if you add a dimension to an array and execute the cell multiple times, then the cells after may not work. If you encounter problem reset your environment:\n", + " * Runtime -> Restart runtime... Resets your Python shell\n", + " * Runtime -> Restart all runtimes... Will reset the Colab image, and get you back to a 100% clean environment\n", + "* You can also clear the output in the Colab by doing: Edit -> Clear all outputs\n", + "* Colabs in this course are loaded from GitHub. Save to your Google Drive if you want a copy with your code/output: File -> Save a copy in Drive...\n", + "\n", + "**Learn More**\n", + "* Check out [this](https://www.youtube.com/watch?v=inN8seMm7UI&list=PLQY2H8rRoyvwLbzbnKJ59NkZvQAW9wLbx&index=3) episode of #CodingTensorFlow, and don't forget to subscribe to the YouTube channel ;)\n" + ] + } + ] +} \ No newline at end of file