{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "markdown", "source": [ "# **Welcome to the Coding in Python workshop!**" ], "metadata": { "id": "CbxeM7ywWrfD" } }, { "cell_type": "markdown", "source": [ "## Task 1: The \"Hello, World!\" example\n", "\"Hello, World\" is the classic first program.\n", "\n", "Run the code cell below to print \"Hello, World\"" ], "metadata": { "id": "QpHxho9lXHe-" } }, { "cell_type": "code", "execution_count": 1, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "SjstCzKaWU1b", "outputId": "b6b9dac8-c831-4e31-c7c8-10987a5d5aa1" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Hello, World\n" ] } ], "source": [ "print(\"Hello, World\")" ] }, { "cell_type": "markdown", "source": [ "## Task 2: Simple Maths\n", "run the code cell below to finish the math calculation." ], "metadata": { "id": "fs9NWIdHYCFW" } }, { "cell_type": "code", "source": [ "12+8" ], "metadata": { "collapsed": true, "id": "2o11H_c6Ye58" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "edit the calculaiton and run again, check if you got the correct answer" ], "metadata": { "id": "m39jr2c0Y1ZH" } }, { "cell_type": "markdown", "source": [ "## Task 3: Drawing with Turtle\n", "Use turtle to draw shapes" ], "metadata": { "id": "g6vLRdGHZssU" } }, { "cell_type": "code", "source": [ "# install the ColabTurtle package\n", "!pip install ColabTurtle\n", "\n", "# import the Turtle library\n", "from ColabTurtle.Turtle import *\n", "\n", "# initilize a turtle, which will be used to draw shapes\n", "initializeTurtle()\n", "\n", "# move forward 100 units\n", "forward(100)\n", "\n", "# turn right 60 degree\n", "#right(60)\n", "\n", "# move backward 100 units\n", "#backward(100)\n", "\n", "# turn right 120 degree\n", "#right(120)\n", "\n", "# move forward 100 units\n", "#forward(100)\n" ], "metadata": { "id": "qhIqGqrKd5T0" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "## Task 4: Plotting Graphs\n", "Use \"matplotlib\" to plot simple graphs" ], "metadata": { "id": "aEO3qdsqjErX" } }, { "cell_type": "code", "source": [ "# import the pyplot library in matplotlib, and rename the library as 'plt'\n", "import matplotlib.pyplot as plt\n", "\n", "# specify the coordinates of points\n", "x = [1, 2, 3, 4]\n", "y = [1, 3, 7, 16]\n", "\n", "plt.plot(x, y)\n", "plt.show()\n" ], "metadata": { "id": "XjsveMVEjUsj" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "## Task 5: Drawing with ellipses\n", "\n", "use \"matplotlib\" library to draw with ellipses" ], "metadata": { "id": "YVyYAzNrj8-W" } }, { "cell_type": "code", "source": [ "# import necessary libraries\n", "import matplotlib.pyplot as plt\n", "import matplotlib.patches as patches\n", "\n", "# Set up the figure and axis\n", "fig, ax = plt.subplots()\n", "\n", "# Set the aspect of the plot to be equal\n", "ax.set_aspect('equal')\n", "\n", "# Set the limits of the plot\n", "ax.set_xlim(-1.5, 1.5)\n", "ax.set_ylim(-1.5, 1.5)\n", "\n", "# Remove the axes\n", "ax.axis('off')\n", "\n", "# Draw the face\n", "face = patches.Ellipse((0, 0), width=2, height=2, edgecolor='black', facecolor='yellow')\n", "ax.add_patch(face)\n", "\n", "# Draw the left eye\n", "left_eye = patches.Ellipse((-0.5, 0.5), width=0.3, height=0.5, edgecolor='black', facecolor='white')\n", "ax.add_patch(left_eye)\n", "\n", "# Draw the right eye\n", "right_eye = patches.Ellipse((0.5, 0.5), width=0.3, height=0.5, edgecolor='black', facecolor='white')\n", "ax.add_patch(right_eye)\n", "\n", "# Draw the pupils\n", "left_pupil = patches.Ellipse((-0.5, 0.5), width=0.1, height=0.1, edgecolor='black', facecolor='black')\n", "ax.add_patch(left_pupil)\n", "\n", "right_pupil = patches.Ellipse((0.5, 0.5), width=0.15, height=0.01, edgecolor='black', facecolor='black')\n", "ax.add_patch(right_pupil)\n", "\n", "# Draw the mouth\n", "mouth = patches.Arc((0, -0.3), width=1, height=0.6, angle=0, theta1=0, theta2=180, edgecolor='black', facecolor='none')\n", "ax.add_patch(mouth)\n", "\n", "# Show the plot\n", "plt.show()\n", "\n" ], "metadata": { "id": "x7irXPJfkVNf" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "## Challenge 1: Complete the code cell below to draw a shape \"8\" with Turtle" ], "metadata": { "id": "tKNenpSimNd9" } }, { "cell_type": "code", "source": [ "# install the ColabTurtle package\n", "!pip install ColabTurtle\n", "\n", "# import the Turtle library\n", "from ColabTurtle.Turtle import *\n", "\n", "# initilize a turtle, which will be used to draw shapes\n", "initializeTurtle()\n", "\n", "# add you code below...\n", "\n", "\n", "\n", "\n", "\n", "\n" ], "metadata": { "id": "zvxgNbUMmpC1" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "## Challenge 2: Complet the code cell below to draw a smily face with ellipses" ], "metadata": { "id": "IFmOzSClmqqn" } }, { "cell_type": "code", "source": [ "# import necessary libraries\n", "import matplotlib.pyplot as plt\n", "import matplotlib.patches as patches\n", "\n", "# Set up the figure and axis\n", "fig, ax = plt.subplots()\n", "\n", "# Set the aspect of the plot to be equal\n", "ax.set_aspect('equal')\n", "\n", "# Set the limits of the plot\n", "ax.set_xlim(-1.5, 1.5)\n", "ax.set_ylim(-1.5, 1.5)\n", "\n", "# Remove the axes\n", "ax.axis('off')" ], "metadata": { "id": "AHf2uDOvnKy2" }, "execution_count": null, "outputs": [] } ] }