├── 3.turtle.py ├── bonus.ipynb ├── challange.ipynb ├── exercise.ipynb ├── main.ipynb ├── readme.md └── references.ipynb /3.turtle.py: -------------------------------------------------------------------------------- 1 | # A program is a set of instructions for a computer. 2 | # A program is made up of lines of code. 3 | # Each line tells the computer a particular detail of those instructions. 4 | 5 | # programs that give instructions to turtles in order to make them create 6 | # different shapes and patterns. 7 | # You can think of turtles as virtual robots that know how to draw lines on the screen, 8 | # following commands such as forward, back, left, and right 9 | 10 | # here the first line is importing (bringing) a module (package) called turtle 11 | # a module is a collection of code (instructions) that gives us all we need 12 | # to create and control turtles 13 | import turtle 14 | 15 | # here we create a turtle "Turtle" from that module 16 | # and assign it to a variable called fred 17 | # get the code of the turtle from the module 18 | # and call it using () to create a turtle and assign it to the variable fred 19 | fred = turtle.Turtle() 20 | 21 | # the turtle can do different things 22 | # and every it can do is called a method 23 | # we use a method by calling the variable name then a dot then the name of the method 24 | 25 | # for example the turtle can change its color with the color method 26 | fred.color("red") 27 | 28 | # here we call the forward method to move the turtle forward by 100 pixels 29 | fred.forward(100) 30 | 31 | # here we call the right method to turn the turtle 135 degrees to the right 32 | fred.right(135) 33 | 34 | # and so on 35 | fred.forward(140) 36 | fred.right(135) 37 | fred.forward(100) -------------------------------------------------------------------------------- /bonus.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Bonus exercise" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "1. Draw a house that has a triangular ceiling, a squared body, two circular windows, and one rectangular door." 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": null, 20 | "metadata": {}, 21 | "outputs": [], 22 | "source": [] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "2. Add shining stars at random locations in the sky around the house" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": null, 34 | "metadata": {}, 35 | "outputs": [], 36 | "source": [] 37 | }, 38 | { 39 | "cell_type": "markdown", 40 | "metadata": {}, 41 | "source": [ 42 | "3. Add comments within your source code for each step of the drawing." 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": null, 48 | "metadata": {}, 49 | "outputs": [ 50 | { 51 | "name": "stdout", 52 | "output_type": "stream", 53 | "text": [ 54 | "Collecting ipyturtle\n", 55 | " Downloading ipyturtle-0.2.4-py2.py3-none-any.whl.metadata (903 bytes)\n", 56 | "Collecting ipywidgets>=7.0.0 (from ipyturtle)\n", 57 | " Downloading ipywidgets-8.1.5-py3-none-any.whl.metadata (2.3 kB)\n", 58 | "Requirement already satisfied: comm>=0.1.3 in /home/codespace/.local/lib/python3.12/site-packages (from ipywidgets>=7.0.0->ipyturtle) (0.2.2)\n", 59 | "Requirement already satisfied: ipython>=6.1.0 in /home/codespace/.local/lib/python3.12/site-packages (from ipywidgets>=7.0.0->ipyturtle) (8.27.0)\n", 60 | "Requirement already satisfied: traitlets>=4.3.1 in /home/codespace/.local/lib/python3.12/site-packages (from ipywidgets>=7.0.0->ipyturtle) (5.14.3)\n", 61 | "Collecting widgetsnbextension~=4.0.12 (from ipywidgets>=7.0.0->ipyturtle)\n", 62 | " Downloading widgetsnbextension-4.0.13-py3-none-any.whl.metadata (1.6 kB)\n", 63 | "Collecting jupyterlab-widgets~=3.0.12 (from ipywidgets>=7.0.0->ipyturtle)\n", 64 | " Downloading jupyterlab_widgets-3.0.13-py3-none-any.whl.metadata (4.1 kB)\n", 65 | "Requirement already satisfied: decorator in /home/codespace/.local/lib/python3.12/site-packages (from ipython>=6.1.0->ipywidgets>=7.0.0->ipyturtle) (5.1.1)\n", 66 | "Requirement already satisfied: jedi>=0.16 in /home/codespace/.local/lib/python3.12/site-packages (from ipython>=6.1.0->ipywidgets>=7.0.0->ipyturtle) (0.19.1)\n", 67 | "Requirement already satisfied: matplotlib-inline in /home/codespace/.local/lib/python3.12/site-packages (from ipython>=6.1.0->ipywidgets>=7.0.0->ipyturtle) (0.1.7)\n", 68 | "Requirement already satisfied: prompt-toolkit<3.1.0,>=3.0.41 in /home/codespace/.local/lib/python3.12/site-packages (from ipython>=6.1.0->ipywidgets>=7.0.0->ipyturtle) (3.0.48)\n", 69 | "Requirement already satisfied: pygments>=2.4.0 in /home/codespace/.local/lib/python3.12/site-packages (from ipython>=6.1.0->ipywidgets>=7.0.0->ipyturtle) (2.18.0)\n", 70 | "Requirement already satisfied: stack-data in /home/codespace/.local/lib/python3.12/site-packages (from ipython>=6.1.0->ipywidgets>=7.0.0->ipyturtle) (0.6.3)\n", 71 | "Requirement already satisfied: pexpect>4.3 in /home/codespace/.local/lib/python3.12/site-packages (from ipython>=6.1.0->ipywidgets>=7.0.0->ipyturtle) (4.9.0)\n", 72 | "Requirement already satisfied: parso<0.9.0,>=0.8.3 in /home/codespace/.local/lib/python3.12/site-packages (from jedi>=0.16->ipython>=6.1.0->ipywidgets>=7.0.0->ipyturtle) (0.8.4)\n", 73 | "Requirement already satisfied: ptyprocess>=0.5 in /home/codespace/.local/lib/python3.12/site-packages (from pexpect>4.3->ipython>=6.1.0->ipywidgets>=7.0.0->ipyturtle) (0.7.0)\n", 74 | "Requirement already satisfied: wcwidth in /home/codespace/.local/lib/python3.12/site-packages (from prompt-toolkit<3.1.0,>=3.0.41->ipython>=6.1.0->ipywidgets>=7.0.0->ipyturtle) (0.2.13)\n", 75 | "Requirement already satisfied: executing>=1.2.0 in /home/codespace/.local/lib/python3.12/site-packages (from stack-data->ipython>=6.1.0->ipywidgets>=7.0.0->ipyturtle) (2.1.0)\n", 76 | "Requirement already satisfied: asttokens>=2.1.0 in /home/codespace/.local/lib/python3.12/site-packages (from stack-data->ipython>=6.1.0->ipywidgets>=7.0.0->ipyturtle) (2.4.1)\n", 77 | "Requirement already satisfied: pure-eval in /home/codespace/.local/lib/python3.12/site-packages (from stack-data->ipython>=6.1.0->ipywidgets>=7.0.0->ipyturtle) (0.2.3)\n", 78 | "Requirement already satisfied: six>=1.12.0 in /home/codespace/.local/lib/python3.12/site-packages (from asttokens>=2.1.0->stack-data->ipython>=6.1.0->ipywidgets>=7.0.0->ipyturtle) (1.16.0)\n", 79 | "Downloading ipyturtle-0.2.4-py2.py3-none-any.whl (427 kB)\n", 80 | "Downloading ipywidgets-8.1.5-py3-none-any.whl (139 kB)\n", 81 | "Downloading jupyterlab_widgets-3.0.13-py3-none-any.whl (214 kB)\n", 82 | "Downloading widgetsnbextension-4.0.13-py3-none-any.whl (2.3 MB)\n", 83 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.3/2.3 MB\u001b[0m \u001b[31m62.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", 84 | "\u001b[?25hInstalling collected packages: widgetsnbextension, jupyterlab-widgets, ipywidgets, ipyturtle\n", 85 | "Successfully installed ipyturtle-0.2.4 ipywidgets-8.1.5 jupyterlab-widgets-3.0.13 widgetsnbextension-4.0.13\n", 86 | "\n", 87 | "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m24.2\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m24.3.1\u001b[0m\n", 88 | "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpython -m pip install --upgrade pip\u001b[0m\n", 89 | "Note: you may need to restart the kernel to use updated packages.\n", 90 | "The ipyturtle module is not an IPython extension.\n" 91 | ] 92 | } 93 | ], 94 | "source": [ 95 | "%pip install ipyturtle" 96 | ] 97 | }, 98 | { 99 | "cell_type": "code", 100 | "execution_count": null, 101 | "metadata": {}, 102 | "outputs": [ 103 | { 104 | "data": { 105 | "application/vnd.jupyter.widget-view+json": { 106 | "model_id": "c7b6764fb39f4e9caf78d05395f63d12", 107 | "version_major": 2, 108 | "version_minor": 0 109 | }, 110 | "text/plain": [ 111 | "Turtle()" 112 | ] 113 | }, 114 | "metadata": {}, 115 | "output_type": "display_data" 116 | } 117 | ], 118 | "source": [ 119 | "%load_ext ipyturtle\n", 120 | "\n", 121 | "# Import the Turtle widget\n", 122 | "from ipyturtle import Turtle\n", 123 | "\n", 124 | "# Create a Turtle object\n", 125 | "t = Turtle()\n", 126 | "\n", 127 | "# Display the Turtle widget\n", 128 | "display(t)\n", 129 | "\n", 130 | "# Draw a square\n", 131 | "t.forward(100) # Move forward by 100 units\n", 132 | "t.left(90) # Turn left by 90 degrees\n", 133 | "t.forward(100)\n", 134 | "t.left(90)\n", 135 | "t.forward(100)\n", 136 | "t.left(90)\n", 137 | "t.forward(100)\n", 138 | "t.left(90)" 139 | ] 140 | } 141 | ], 142 | "metadata": { 143 | "kernelspec": { 144 | "display_name": "Python 3", 145 | "language": "python", 146 | "name": "python3" 147 | }, 148 | "language_info": { 149 | "codemirror_mode": { 150 | "name": "ipython", 151 | "version": 3 152 | }, 153 | "file_extension": ".py", 154 | "mimetype": "text/x-python", 155 | "name": "python", 156 | "nbconvert_exporter": "python", 157 | "pygments_lexer": "ipython3", 158 | "version": "3.12.1" 159 | } 160 | }, 161 | "nbformat": 4, 162 | "nbformat_minor": 2 163 | } 164 | -------------------------------------------------------------------------------- /challange.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Turtle challange" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [] 14 | } 15 | ], 16 | "metadata": { 17 | "language_info": { 18 | "name": "python" 19 | } 20 | }, 21 | "nbformat": 4, 22 | "nbformat_minor": 2 23 | } 24 | -------------------------------------------------------------------------------- /exercise.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Exercise: arithmitic operations in Python" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "Create two numeric variables" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": null, 20 | "metadata": {}, 21 | "outputs": [], 22 | "source": [ 23 | "# your code here:" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": {}, 29 | "source": [ 30 | "Run Addition, subtraction, Division, multiplication" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": null, 36 | "metadata": {}, 37 | "outputs": [], 38 | "source": [ 39 | "# your code here:" 40 | ] 41 | }, 42 | { 43 | "cell_type": "markdown", 44 | "metadata": {}, 45 | "source": [ 46 | "Print the values" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": null, 52 | "metadata": {}, 53 | "outputs": [], 54 | "source": [ 55 | "# your code here:" 56 | ] 57 | }, 58 | { 59 | "cell_type": "markdown", 60 | "metadata": {}, 61 | "source": [ 62 | "Repeat the previous steps with floats" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": null, 68 | "metadata": {}, 69 | "outputs": [], 70 | "source": [ 71 | "# your code here:" 72 | ] 73 | }, 74 | { 75 | "cell_type": "markdown", 76 | "metadata": {}, 77 | "source": [ 78 | "Create two strings" 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": null, 84 | "metadata": {}, 85 | "outputs": [], 86 | "source": [ 87 | "# your code here:" 88 | ] 89 | }, 90 | { 91 | "cell_type": "markdown", 92 | "metadata": {}, 93 | "source": [ 94 | "Print two strings in addition to the integers created in the first example" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": null, 100 | "metadata": {}, 101 | "outputs": [], 102 | "source": [ 103 | "# your code here:" 104 | ] 105 | }, 106 | { 107 | "cell_type": "markdown", 108 | "metadata": {}, 109 | "source": [ 110 | "Add comments to every step explaining what is process done for each variable" 111 | ] 112 | }, 113 | { 114 | "cell_type": "code", 115 | "execution_count": null, 116 | "metadata": {}, 117 | "outputs": [], 118 | "source": [ 119 | "# your code here:" 120 | ] 121 | } 122 | ], 123 | "metadata": { 124 | "kernelspec": { 125 | "display_name": "Python 3", 126 | "language": "python", 127 | "name": "python3" 128 | }, 129 | "language_info": { 130 | "name": "python", 131 | "version": "3.12.1" 132 | } 133 | }, 134 | "nbformat": 4, 135 | "nbformat_minor": 2 136 | } 137 | -------------------------------------------------------------------------------- /main.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Python Variables\n", 8 | "\n", 9 | "variable names\n", 10 | "- are case sensitive\n", 11 | "- must start with a letter or an underscore\n", 12 | "- cannot start with a number\n", 13 | "- can only contain alpha numeric characters and underscores\n", 14 | "- are a way to refer to data values\n", 15 | "\n", 16 | "some words are reserved keywords for the python language and cannot be used as variable names\n", 17 | "meaning they are reserved for special use by the python language.\n", 18 | "\n", 19 | "A variable is a connection between a name in the code and some data in memory." 20 | ] 21 | }, 22 | { 23 | "cell_type": "markdown", 24 | "metadata": {}, 25 | "source": [ 26 | "The reason we call it a variable is because it is able to vary. \n", 27 | "We can first say that this name refers to some data in one place in memory, \n", 28 | "but then later in our program we could change it so that the same name refers to some other data in a different location." 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 2, 34 | "metadata": {}, 35 | "outputs": [ 36 | { 37 | "name": "stdout", 38 | "output_type": "stream", 39 | "text": [ 40 | "10\n" 41 | ] 42 | } 43 | ], 44 | "source": [ 45 | "x = 10\n", 46 | "print(x)" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": 3, 52 | "metadata": {}, 53 | "outputs": [ 54 | { 55 | "name": "stdout", 56 | "output_type": "stream", 57 | "text": [ 58 | "hello\n" 59 | ] 60 | } 61 | ], 62 | "source": [ 63 | "x = \"hello\"\n", 64 | "print(x)" 65 | ] 66 | }, 67 | { 68 | "cell_type": "markdown", 69 | "metadata": {}, 70 | "source": [ 71 | "that make python what is called a dynamically typed language\n", 72 | "this means the type of the variable can be changed during the execution of the program (runtime)\n", 73 | "the type of the variable is determined by the value assigned to it" 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "execution_count": 4, 79 | "metadata": {}, 80 | "outputs": [ 81 | { 82 | "name": "stdout", 83 | "output_type": "stream", 84 | "text": [ 85 | "hello\n" 86 | ] 87 | } 88 | ], 89 | "source": [ 90 | "x = 10\n", 91 | "x = \"hello\"\n", 92 | "print(x)" 93 | ] 94 | }, 95 | { 96 | "cell_type": "markdown", 97 | "metadata": {}, 98 | "source": [ 99 | "there are other data types in python\n", 100 | "for example\n", 101 | "- `x = 10` int data type\n", 102 | "- `x = \"hello\"` str data type\n", 103 | "- `x = 10.5` float data type\n", 104 | "- `x = True` bool data type\n", 105 | "- `x = None` None data type - no value\n", 106 | "\n", 107 | "any other type of value not listed in these previous examples is a user defined data type (class)\n", 108 | "meaning that it is a custom data type that we can define and use, and each value of that type is called an instance (object) of that class" 109 | ] 110 | }, 111 | { 112 | "cell_type": "markdown", 113 | "metadata": {}, 114 | "source": [ 115 | "spacing in python is important and is used to separate code into different blocks of code (groups of statements) as we will see in loops and if statements" 116 | ] 117 | }, 118 | { 119 | "cell_type": "markdown", 120 | "metadata": {}, 121 | "source": [ 122 | "## Integer Values - int data type" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": 6, 128 | "metadata": {}, 129 | "outputs": [ 130 | { 131 | "name": "stdout", 132 | "output_type": "stream", 133 | "text": [ 134 | "6\n" 135 | ] 136 | } 137 | ], 138 | "source": [ 139 | "number = 3\n", 140 | "double_the_number = number * 2\n", 141 | "print(double_the_number)" 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "execution_count": 7, 147 | "metadata": {}, 148 | "outputs": [ 149 | { 150 | "name": "stdout", 151 | "output_type": "stream", 152 | "text": [ 153 | "18\n" 154 | ] 155 | } 156 | ], 157 | "source": [ 158 | "number = number + 15\n", 159 | "print(number)" 160 | ] 161 | }, 162 | { 163 | "cell_type": "code", 164 | "execution_count": 8, 165 | "metadata": {}, 166 | "outputs": [ 167 | { 168 | "name": "stdout", 169 | "output_type": "stream", 170 | "text": [ 171 | "19\n" 172 | ] 173 | } 174 | ], 175 | "source": [ 176 | "\n", 177 | "number += 1\n", 178 | "print(number)" 179 | ] 180 | }, 181 | { 182 | "cell_type": "code", 183 | "execution_count": 9, 184 | "metadata": {}, 185 | "outputs": [ 186 | { 187 | "name": "stdout", 188 | "output_type": "stream", 189 | "text": [ 190 | "38\n" 191 | ] 192 | } 193 | ], 194 | "source": [ 195 | "number *= 2\n", 196 | "print(number)" 197 | ] 198 | }, 199 | { 200 | "cell_type": "markdown", 201 | "metadata": {}, 202 | "source": [ 203 | "## Strings (str)\n", 204 | "- are sequences of characters\n", 205 | "- written in double quotes or single quotes\n", 206 | "- immutable (cannot be changed)\n", 207 | "- used to represent text" 208 | ] 209 | }, 210 | { 211 | "cell_type": "code", 212 | "execution_count": 10, 213 | "metadata": {}, 214 | "outputs": [ 215 | { 216 | "name": "stdout", 217 | "output_type": "stream", 218 | "text": [ 219 | "john\n" 220 | ] 221 | } 222 | ], 223 | "source": [ 224 | "name = \"john\"\n", 225 | "print(name)" 226 | ] 227 | }, 228 | { 229 | "cell_type": "code", 230 | "execution_count": 12, 231 | "metadata": {}, 232 | "outputs": [ 233 | { 234 | "name": "stdout", 235 | "output_type": "stream", 236 | "text": [ 237 | "john Doe Doe\n" 238 | ] 239 | } 240 | ], 241 | "source": [ 242 | "# add (concatenate) \" Doe\" to \"john\"\n", 243 | "name += \" Doe\"\n", 244 | "print(name)" 245 | ] 246 | }, 247 | { 248 | "cell_type": "code", 249 | "execution_count": 13, 250 | "metadata": {}, 251 | "outputs": [ 252 | { 253 | "name": "stdout", 254 | "output_type": "stream", 255 | "text": [ 256 | "Hi Hi Hi \n" 257 | ] 258 | } 259 | ], 260 | "source": [ 261 | "# print \"Hi \" 3 times as a single string\n", 262 | "print(\"Hi \" * 3)" 263 | ] 264 | }, 265 | { 266 | "cell_type": "code", 267 | "execution_count": null, 268 | "metadata": {}, 269 | "outputs": [ 270 | { 271 | "ename": "TypeError", 272 | "evalue": "unsupported operand type(s) for +: 'int' and 'str'", 273 | "output_type": "error", 274 | "traceback": [ 275 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 276 | "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", 277 | "Cell \u001b[0;32mIn[14], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m# generates and error\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;241;43m3\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mhello\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m)\n", 278 | "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'int' and 'str'" 279 | ] 280 | } 281 | ], 282 | "source": [ 283 | "# generates an error\n", 284 | "print(3 + \"hello\")" 285 | ] 286 | }, 287 | { 288 | "cell_type": "code", 289 | "execution_count": null, 290 | "metadata": {}, 291 | "outputs": [], 292 | "source": [ 293 | "# generates an error\n", 294 | "print(\"hi\" * 'hi')" 295 | ] 296 | }, 297 | { 298 | "cell_type": "markdown", 299 | "metadata": {}, 300 | "source": [ 301 | "error message does not mean you are a bad programmer\n", 302 | "instead it means the computer can't understand your code\n", 303 | "use errors to help yourself learn and grow" 304 | ] 305 | }, 306 | { 307 | "cell_type": "markdown", 308 | "metadata": {}, 309 | "source": [ 310 | "strings can be enclosed between single `'name'`or double `\"name\"`or triple `\"\"\"name\"\"\"`quotes to denote that is a string\n", 311 | "is the string we are trying to print is a multiline string or has `'` or `\"` in it then we need to use triple quotes" 312 | ] 313 | }, 314 | { 315 | "cell_type": "code", 316 | "execution_count": null, 317 | "metadata": {}, 318 | "outputs": [], 319 | "source": [ 320 | "person = 'amy'\n", 321 | "para = \"\"\"amy's car \"bmw\" is new\"\"\"\n", 322 | "para" 323 | ] 324 | }, 325 | { 326 | "cell_type": "markdown", 327 | "metadata": {}, 328 | "source": [ 329 | "triple quotes can be used as a way to denote documenations. you can find them in the beginning of the file or in the beginning of a function.\n", 330 | "for example, the numpy library uses triple quotes to document functions and files (modules). see [here](https://github.com/numpy/numpy/blob/main/numpy/__init__.py)." 331 | ] 332 | }, 333 | { 334 | "cell_type": "code", 335 | "execution_count": 25, 336 | "metadata": {}, 337 | "outputs": [ 338 | { 339 | "data": { 340 | "text/plain": [ 341 | "\"\\n This is the doc string of this module (file/package)\\n It's used to describe the functionality (usage) of the module,\\n and provide detailed descriptions of code for users\\n\\n this doc string is often processed by developers tools like Sphinx to generate documentation\\n for more information see: https://www.datacamp.com/tutorial/docstrings-python\\n and https://www.python.org/dev/peps/pep-0257/\\n\\n while comments are used in general to explain context and provide background\\n for a specific block of code, docstrings are used to document the entire module\\n function, or class (user-defined data type)\\n\\n documentation includes use cases, parameters, and return values\\n\\n comments can be used also as a way to disable sections of code\\n so that they are not executed by the interpreter when the program is run\\n\\n the interpreter is a program that reads your python code and executes it\\n by translating it into machine code (zeros and ones '001001110') and running it\\n line by line in real-time.\\n\"" 342 | ] 343 | }, 344 | "execution_count": 25, 345 | "metadata": {}, 346 | "output_type": "execute_result" 347 | } 348 | ], 349 | "source": [ 350 | "\"\"\"\n", 351 | " This is the doc string of this module (file/package)\n", 352 | " It's used to describe the functionality (usage) of the module,\n", 353 | " and provide detailed descriptions of code for users\n", 354 | "\n", 355 | " this doc string is often processed by developers tools like Sphinx to generate documentation\n", 356 | " for more information see: https://www.datacamp.com/tutorial/docstrings-python\n", 357 | " and https://www.python.org/dev/peps/pep-0257/\n", 358 | "\n", 359 | " while comments are used in general to explain context and provide background\n", 360 | " for a specific block of code, docstrings are used to document the entire module\n", 361 | " function, or class (user-defined data type)\n", 362 | "\n", 363 | " documentation includes use cases, parameters, and return values\n", 364 | "\n", 365 | " comments can be used also as a way to disable sections of code\n", 366 | " so that they are not executed by the interpreter when the program is run\n", 367 | "\n", 368 | " the interpreter is a program that reads your python code and executes it\n", 369 | " by translating it into machine code (zeros and ones '001001110') and running it\n", 370 | " line by line in real-time.\n", 371 | "\"\"\"" 372 | ] 373 | }, 374 | { 375 | "cell_type": "markdown", 376 | "metadata": {}, 377 | "source": [ 378 | "## Floating point numbers (float)\n", 379 | "\n", 380 | "- are real numbers\n", 381 | "- they are written with decimal points\n", 382 | "- they are used to represent numbers with decimal places\n", 383 | "- float (floating point) values" 384 | ] 385 | }, 386 | { 387 | "cell_type": "code", 388 | "execution_count": 15, 389 | "metadata": {}, 390 | "outputs": [ 391 | { 392 | "name": "stdout", 393 | "output_type": "stream", 394 | "text": [ 395 | "3.14159\n" 396 | ] 397 | } 398 | ], 399 | "source": [ 400 | "pi = 3.14159\n", 401 | "print(pi)" 402 | ] 403 | }, 404 | { 405 | "cell_type": "code", 406 | "execution_count": null, 407 | "metadata": {}, 408 | "outputs": [], 409 | "source": [ 410 | "r = 5\n", 411 | "cim = 2 * pi * r\n", 412 | "area = pi * (r**2)\n", 413 | "print(cim, area)" 414 | ] 415 | }, 416 | { 417 | "cell_type": "markdown", 418 | "metadata": {}, 419 | "source": [ 420 | "# Complex numbers\n", 421 | "\n", 422 | "- written with the j symbol\n", 423 | "- used to represent complex numbers" 424 | ] 425 | }, 426 | { 427 | "cell_type": "code", 428 | "execution_count": 17, 429 | "metadata": {}, 430 | "outputs": [ 431 | { 432 | "name": "stdout", 433 | "output_type": "stream", 434 | "text": [ 435 | "(2+3j)\n" 436 | ] 437 | } 438 | ], 439 | "source": [ 440 | "complex_number = 2 + 3j\n", 441 | "print(complex_number)" 442 | ] 443 | }, 444 | { 445 | "cell_type": "code", 446 | "execution_count": null, 447 | "metadata": {}, 448 | "outputs": [], 449 | "source": [ 450 | "complex_number = 2 + 3j\n", 451 | "other_complex = 1 + 4j\n", 452 | "print(complex_number + other_complex)" 453 | ] 454 | }, 455 | { 456 | "cell_type": "markdown", 457 | "metadata": {}, 458 | "source": [ 459 | "# Boolean Types\n", 460 | "\n", 461 | "- used primarily for conditional statements (if, elif, else statements)\n", 462 | "- values are either True or False" 463 | ] 464 | }, 465 | { 466 | "cell_type": "code", 467 | "execution_count": 18, 468 | "metadata": {}, 469 | "outputs": [ 470 | { 471 | "name": "stdout", 472 | "output_type": "stream", 473 | "text": [ 474 | "It's raining outside\n" 475 | ] 476 | } 477 | ], 478 | "source": [ 479 | "is_raining = True\n", 480 | "if is_raining:\n", 481 | " print(\"It's raining outside\")\n", 482 | "else:\n", 483 | " print(\"It's not raining outside\")" 484 | ] 485 | }, 486 | { 487 | "cell_type": "markdown", 488 | "metadata": {}, 489 | "source": [ 490 | "as there is binary operators for int and float data types like +, -, *, /, //, %, ** used to do arithmitic operations\n", 491 | "\n", 492 | "there is aslo conditional operators like ==, !=, >, <, >=, <=, and, or, not used to do boolean operations on values of boolean data types" 493 | ] 494 | }, 495 | { 496 | "cell_type": "code", 497 | "execution_count": 19, 498 | "metadata": {}, 499 | "outputs": [ 500 | { 501 | "name": "stdout", 502 | "output_type": "stream", 503 | "text": [ 504 | "It's not cloudy or raining outside\n" 505 | ] 506 | } 507 | ], 508 | "source": [ 509 | "is_cloudy = False\n", 510 | "if is_cloudy and is_raining:\n", 511 | " print(\"It's cloudy and raining outside\")\n", 512 | "else:\n", 513 | " print(\"It's not cloudy or raining outside\")" 514 | ] 515 | }, 516 | { 517 | "cell_type": "code", 518 | "execution_count": 20, 519 | "metadata": {}, 520 | "outputs": [ 521 | { 522 | "name": "stdout", 523 | "output_type": "stream", 524 | "text": [ 525 | "True\n", 526 | "False\n", 527 | "False\n" 528 | ] 529 | } 530 | ], 531 | "source": [ 532 | "print(15 == 15)\n", 533 | "print(10 < 3)\n", 534 | "print(10 <= 3)" 535 | ] 536 | }, 537 | { 538 | "cell_type": "markdown", 539 | "metadata": {}, 540 | "source": [ 541 | "### Boolean short circuting\n", 542 | "\n", 543 | "when we are using the `or` operator in a conditional statement\n", 544 | "- if the first expression evaluates to true, the second expression is not evaluated and `True` is returned\n", 545 | "- if the first expression evaluates to false, the second expression is evaluated and the result is returned\n", 546 | "\n", 547 | "this is known as short circuting. the opposite behaviour happens with `and` operator\n", 548 | "- if the first expression evaluates to true, the second expression is evaluated and the result is returned\n", 549 | "- if the first expression evaluates to false, the second expression is not evaluated and `False` is returned" 550 | ] 551 | }, 552 | { 553 | "cell_type": "code", 554 | "execution_count": null, 555 | "metadata": {}, 556 | "outputs": [], 557 | "source": [ 558 | "print(True or 5)\n", 559 | "print(False or 5)" 560 | ] 561 | }, 562 | { 563 | "cell_type": "code", 564 | "execution_count": null, 565 | "metadata": {}, 566 | "outputs": [], 567 | "source": [ 568 | "print(True and 5)\n", 569 | "print(False and 5)" 570 | ] 571 | }, 572 | { 573 | "cell_type": "markdown", 574 | "metadata": {}, 575 | "source": [ 576 | "# None Type\n", 577 | "\n", 578 | "- used to represent the absence of a value\n", 579 | "- used to represent the absence of a variable" 580 | ] 581 | }, 582 | { 583 | "cell_type": "code", 584 | "execution_count": 22, 585 | "metadata": {}, 586 | "outputs": [ 587 | { 588 | "name": "stdout", 589 | "output_type": "stream", 590 | "text": [ 591 | "None\n" 592 | ] 593 | } 594 | ], 595 | "source": [ 596 | "x = None\n", 597 | "print(x)" 598 | ] 599 | }, 600 | { 601 | "cell_type": "markdown", 602 | "metadata": {}, 603 | "source": [ 604 | "setting a variable to `None` can be used to dereference a variable. meaning that the variable is no longer pointing to any value" 605 | ] 606 | }, 607 | { 608 | "cell_type": "code", 609 | "execution_count": null, 610 | "metadata": {}, 611 | "outputs": [], 612 | "source": [ 613 | "first_list = [1, 2, 3]\n", 614 | "second_list = first_list\n", 615 | "first_list = None\n", 616 | "\n", 617 | "print(first_list)\n", 618 | "print(second_list)" 619 | ] 620 | }, 621 | { 622 | "cell_type": "markdown", 623 | "metadata": {}, 624 | "source": [ 625 | "## Type annotations\n", 626 | "\n", 627 | "in python, we can still indicate the type of the value inside our variable using type annotations\n" 628 | ] 629 | }, 630 | { 631 | "cell_type": "code", 632 | "execution_count": 23, 633 | "metadata": {}, 634 | "outputs": [ 635 | { 636 | "name": "stdout", 637 | "output_type": "stream", 638 | "text": [ 639 | "5\n" 640 | ] 641 | } 642 | ], 643 | "source": [ 644 | "x: int = 5\n", 645 | "print(x)" 646 | ] 647 | }, 648 | { 649 | "cell_type": "markdown", 650 | "metadata": {}, 651 | "source": [ 652 | "but it will not generate errors when we assigned the variable a different type" 653 | ] 654 | }, 655 | { 656 | "cell_type": "code", 657 | "execution_count": 24, 658 | "metadata": {}, 659 | "outputs": [ 660 | { 661 | "name": "stdout", 662 | "output_type": "stream", 663 | "text": [ 664 | "hi\n" 665 | ] 666 | } 667 | ], 668 | "source": [ 669 | "x = \"hi\"\n", 670 | "print(x)" 671 | ] 672 | }, 673 | { 674 | "cell_type": "code", 675 | "execution_count": null, 676 | "metadata": {}, 677 | "outputs": [], 678 | "source": [] 679 | } 680 | ], 681 | "metadata": { 682 | "kernelspec": { 683 | "display_name": "Python 3", 684 | "language": "python", 685 | "name": "python3" 686 | }, 687 | "language_info": { 688 | "codemirror_mode": { 689 | "name": "ipython", 690 | "version": 3 691 | }, 692 | "file_extension": ".py", 693 | "mimetype": "text/x-python", 694 | "name": "python", 695 | "nbconvert_exporter": "python", 696 | "pygments_lexer": "ipython3", 697 | "version": "3.12.1" 698 | } 699 | }, 700 | "nbformat": 4, 701 | "nbformat_minor": 2 702 | } 703 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Session-1 - Python 2 | 3 | ## Concepts 4 | 5 | - variables 6 | - data types 7 | - memory and references 8 | - turtule graphics 9 | 10 | ## Extras 11 | 12 | ## Materials 13 | 14 | - [Python docstrings](https://www.datacamp.com/tutorial/docstrings-python) 15 | - [Python Docstring Conventions](https://www.python.org/dev/peps/pep-0257/) 16 | 17 | ## Note 18 | 19 | - solutions to the bonus exercises will be posted one week after 20 | - solutions to the challange exercises will be posted two week after -------------------------------------------------------------------------------- /references.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "python string are immutable (don't change)\n", 8 | "so they are written in the memory only once" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 2, 14 | "metadata": {}, 15 | "outputs": [ 16 | { 17 | "name": "stdout", 18 | "output_type": "stream", 19 | "text": [ 20 | "False\n" 21 | ] 22 | } 23 | ], 24 | "source": [ 25 | "# in the first case name1 and name2 point to different strings\n", 26 | "name1 = 'john'\n", 27 | "name2 = 'joudy'\n", 28 | "print(id(name1) == id(name2))" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 3, 34 | "metadata": {}, 35 | "outputs": [ 36 | { 37 | "name": "stdout", 38 | "output_type": "stream", 39 | "text": [ 40 | "False\n" 41 | ] 42 | } 43 | ], 44 | "source": [ 45 | "# is is an operator that is the same as doing id(name1) == id(name2)\n", 46 | "print(name1 is name2) " 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": 4, 52 | "metadata": {}, 53 | "outputs": [ 54 | { 55 | "name": "stdout", 56 | "output_type": "stream", 57 | "text": [ 58 | "True\n" 59 | ] 60 | } 61 | ], 62 | "source": [ 63 | "# now since the strings are the same no need to write it twice\n", 64 | "# so both will point to the same string\n", 65 | "name1 = 'john'\n", 66 | "name2 = 'john'\n", 67 | "print(name1 is name2)" 68 | ] 69 | }, 70 | { 71 | "cell_type": "markdown", 72 | "metadata": {}, 73 | "source": [ 74 | "here since list are mutable (can be changed) each variables will point to a different copy in memory" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": null, 80 | "metadata": {}, 81 | "outputs": [ 82 | { 83 | "name": "stdout", 84 | "output_type": "stream", 85 | "text": [ 86 | "False\n" 87 | ] 88 | } 89 | ], 90 | "source": [ 91 | "numbers1 = [1, 2, 3]\n", 92 | "numbers2 = [1, 2, 3]\n", 93 | "print(numbers1 is numbers2)" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": null, 99 | "metadata": {}, 100 | "outputs": [], 101 | "source": [] 102 | } 103 | ], 104 | "metadata": { 105 | "kernelspec": { 106 | "display_name": "Python 3", 107 | "language": "python", 108 | "name": "python3" 109 | }, 110 | "language_info": { 111 | "codemirror_mode": { 112 | "name": "ipython", 113 | "version": 3 114 | }, 115 | "file_extension": ".py", 116 | "mimetype": "text/x-python", 117 | "name": "python", 118 | "nbconvert_exporter": "python", 119 | "pygments_lexer": "ipython3", 120 | "version": "3.12.1" 121 | } 122 | }, 123 | "nbformat": 4, 124 | "nbformat_minor": 2 125 | } 126 | --------------------------------------------------------------------------------