|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "## Startrek Python - Syntax, Varriables and Comments" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "markdown", |
| 12 | + "metadata": {}, |
| 13 | + "source": [ |
| 14 | + "Python language relies on spacing and will send you an error if spacing of code is incorrect." |
| 15 | + ] |
| 16 | + }, |
| 17 | + { |
| 18 | + "cell_type": "code", |
| 19 | + "execution_count": 1, |
| 20 | + "metadata": {}, |
| 21 | + "outputs": [ |
| 22 | + { |
| 23 | + "name": "stdout", |
| 24 | + "output_type": "stream", |
| 25 | + "text": [ |
| 26 | + " 5 is greater than 2\n" |
| 27 | + ] |
| 28 | + } |
| 29 | + ], |
| 30 | + "source": [ |
| 31 | + "# proper syntax \n", |
| 32 | + "if 5 > 2:\n", |
| 33 | + " print(' 5 is greater than 2')" |
| 34 | + ] |
| 35 | + }, |
| 36 | + { |
| 37 | + "cell_type": "code", |
| 38 | + "execution_count": 2, |
| 39 | + "metadata": {}, |
| 40 | + "outputs": [ |
| 41 | + { |
| 42 | + "ename": "IndentationError", |
| 43 | + "evalue": "expected an indented block (<ipython-input-2-b49e73906c2f>, line 3)", |
| 44 | + "output_type": "error", |
| 45 | + "traceback": [ |
| 46 | + "\u001b[0;36m File \u001b[0;32m\"<ipython-input-2-b49e73906c2f>\"\u001b[0;36m, line \u001b[0;32m3\u001b[0m\n\u001b[0;31m print(' 5 is greater than 2')\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m expected an indented block\n" |
| 47 | + ] |
| 48 | + } |
| 49 | + ], |
| 50 | + "source": [ |
| 51 | + "# incorrect syntax\n", |
| 52 | + "if 5 > 2:\n", |
| 53 | + "print(' 5 is greater than 2')" |
| 54 | + ] |
| 55 | + }, |
| 56 | + { |
| 57 | + "cell_type": "markdown", |
| 58 | + "metadata": {}, |
| 59 | + "source": [ |
| 60 | + "## Python Variables " |
| 61 | + ] |
| 62 | + }, |
| 63 | + { |
| 64 | + "cell_type": "markdown", |
| 65 | + "metadata": {}, |
| 66 | + "source": [ |
| 67 | + "<p> In Python you can name variables mostly however you want with some exceptions, you can't use special words (int, str, bool, etc), 3a, _var. \n", |
| 68 | + "It is advised you use Name_of_Var or camelcase NameOfVar convention. Naming variables relevant to your task at hand is helpful later in understanding what the variable is.</p>\n", |
| 69 | + "\n", |
| 70 | + "- variable name must start with a letter or the underscore character\n", |
| 71 | + "- variable name cannot start with a number\n", |
| 72 | + "- variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )\n", |
| 73 | + "- Variable names are case-sensitive (age, Age and AGE are three different variables)\n" |
| 74 | + ] |
| 75 | + }, |
| 76 | + { |
| 77 | + "cell_type": "code", |
| 78 | + "execution_count": 3, |
| 79 | + "metadata": {}, |
| 80 | + "outputs": [], |
| 81 | + "source": [ |
| 82 | + "# you can use simple variables\n", |
| 83 | + "x = 9\n", |
| 84 | + "v = \"holosuite 3\"\n", |
| 85 | + "\n", |
| 86 | + "latinum_price = 233.65\n", |
| 87 | + "Borg_001 = \"7of9\"\n", |
| 88 | + "TeaEarlGreyHot = True" |
| 89 | + ] |
| 90 | + }, |
| 91 | + { |
| 92 | + "cell_type": "code", |
| 93 | + "execution_count": 14, |
| 94 | + "metadata": {}, |
| 95 | + "outputs": [ |
| 96 | + { |
| 97 | + "name": "stdout", |
| 98 | + "output_type": "stream", |
| 99 | + "text": [ |
| 100 | + "Nog\n", |
| 101 | + "Dax Symbiont\n", |
| 102 | + " Dr Bashir I presume ?\n" |
| 103 | + ] |
| 104 | + }, |
| 105 | + { |
| 106 | + "data": { |
| 107 | + "text/plain": [ |
| 108 | + "3773165756.1099997" |
| 109 | + ] |
| 110 | + }, |
| 111 | + "execution_count": 14, |
| 112 | + "metadata": {}, |
| 113 | + "output_type": "execute_result" |
| 114 | + } |
| 115 | + ], |
| 116 | + "source": [ |
| 117 | + "# Python allows variables assignments of more than 1 variable\n", |
| 118 | + "f1,f2,f3 = 'Quark','Nog','Rom'\n", |
| 119 | + "print(f2)\n", |
| 120 | + "\n", |
| 121 | + "# you can assign the same value to multiple variables in one line:\n", |
| 122 | + "d1 = d2 = d3 = d4 = d5 = d6 = d7 = d8 = \"Dax Symbiont\"\n", |
| 123 | + "print(d4)\n", |
| 124 | + "\n", |
| 125 | + "# you can use the '+' operator to concatenate variables\n", |
| 126 | + "d = \" Dr\"\n", |
| 127 | + "b = ' Bashir'\n", |
| 128 | + "ip = \" I presume ?\"\n", |
| 129 | + "\n", |
| 130 | + "episode = d + b + ip\n", |
| 131 | + "print(episode)\n", |
| 132 | + "\n", |
| 133 | + "x1 = 9398389.89\n", |
| 134 | + "y1 = 3763767366.22\n", |
| 135 | + "calculation = x1 + y1\n", |
| 136 | + "\n", |
| 137 | + "# print out variable in the Notebook\n", |
| 138 | + "calculation" |
| 139 | + ] |
| 140 | + }, |
| 141 | + { |
| 142 | + "cell_type": "markdown", |
| 143 | + "metadata": {}, |
| 144 | + "source": [ |
| 145 | + "## Global Variables" |
| 146 | + ] |
| 147 | + }, |
| 148 | + { |
| 149 | + "cell_type": "markdown", |
| 150 | + "metadata": {}, |
| 151 | + "source": [ |
| 152 | + "<p> Variables that are created outside of a function (as in all of the examples above) are known as global variables.\n", |
| 153 | + "\n", |
| 154 | + "Global variables can be used by everyone, both inside of functions and outside. </p>" |
| 155 | + ] |
| 156 | + }, |
| 157 | + { |
| 158 | + "cell_type": "code", |
| 159 | + "execution_count": 16, |
| 160 | + "metadata": {}, |
| 161 | + "outputs": [ |
| 162 | + { |
| 163 | + "name": "stdout", |
| 164 | + "output_type": "stream", |
| 165 | + "text": [ |
| 166 | + " WARNING: The warpcore is offline\n" |
| 167 | + ] |
| 168 | + } |
| 169 | + ], |
| 170 | + "source": [ |
| 171 | + "# GLOBAL VARIABLES are usually in uppercase \n", |
| 172 | + "# to differentiate from local variables\n", |
| 173 | + "\n", |
| 174 | + "WARP_CORE = \"The warpcore is offline\"\n", |
| 175 | + "\n", |
| 176 | + "# the lines are to show what a function looks like, optional in use\n", |
| 177 | + "#------------------------------- \n", |
| 178 | + "def Engineering_function():\n", |
| 179 | + " print(\" WARNING: \", WARP_CORE )\n", |
| 180 | + "#-------------------------------\n", |
| 181 | + "\n", |
| 182 | + "# need to call the function above in order to run\n", |
| 183 | + "Engineering_function()" |
| 184 | + ] |
| 185 | + }, |
| 186 | + { |
| 187 | + "cell_type": "markdown", |
| 188 | + "metadata": {}, |
| 189 | + "source": [ |
| 190 | + "<p> If you create a variable with the same name inside a function, \n", |
| 191 | + "this variable will be local, and can only be used inside the function. \n", |
| 192 | + "The global variable with the same name will remain as it was, \n", |
| 193 | + "global and with the original value. </p>" |
| 194 | + ] |
| 195 | + }, |
| 196 | + { |
| 197 | + "cell_type": "code", |
| 198 | + "execution_count": 19, |
| 199 | + "metadata": {}, |
| 200 | + "outputs": [ |
| 201 | + { |
| 202 | + "name": "stdout", |
| 203 | + "output_type": "stream", |
| 204 | + "text": [ |
| 205 | + " WARP_CORE inside function_ Is working just fine\n", |
| 206 | + " Global var: The warpcore is offline\n" |
| 207 | + ] |
| 208 | + } |
| 209 | + ], |
| 210 | + "source": [ |
| 211 | + "# GLOBAL VAR\n", |
| 212 | + "WARP_CORE = \"The warpcore is offline\"\n", |
| 213 | + "\n", |
| 214 | + "def function_():\n", |
| 215 | + " WARP_CORE = \" Is working just fine\"\n", |
| 216 | + " print(' WARP_CORE inside function_ ', WARP_CORE)\n", |
| 217 | + " \n", |
| 218 | + "function_()\n", |
| 219 | + "\n", |
| 220 | + "print(' Global var: ', WARP_CORE)" |
| 221 | + ] |
| 222 | + }, |
| 223 | + { |
| 224 | + "cell_type": "markdown", |
| 225 | + "metadata": {}, |
| 226 | + "source": [ |
| 227 | + "<p> when you create a variable inside a function, that variable is local, and can only be used inside that function.\n", |
| 228 | + "\n", |
| 229 | + "To create a global variable inside a function, you can use the global keyword.\n", |
| 230 | + "</p>" |
| 231 | + ] |
| 232 | + }, |
| 233 | + { |
| 234 | + "cell_type": "code", |
| 235 | + "execution_count": 27, |
| 236 | + "metadata": {}, |
| 237 | + "outputs": [ |
| 238 | + { |
| 239 | + "name": "stdout", |
| 240 | + "output_type": "stream", |
| 241 | + "text": [ |
| 242 | + " global WARP_CORE inside function_ : The warpcore is offline\n", |
| 243 | + " GLOBAL var: The warpcore is offline\n" |
| 244 | + ] |
| 245 | + } |
| 246 | + ], |
| 247 | + "source": [ |
| 248 | + "# GLOBAL VAR\n", |
| 249 | + "WARP_CORE = \"The warpcore is offline\"\n", |
| 250 | + "\n", |
| 251 | + "def function_():\n", |
| 252 | + " global WARP_CORE # this global calls the GLOBAL VAR\n", |
| 253 | + " print(' global WARP_CORE inside function_ :', WARP_CORE)\n", |
| 254 | + " \n", |
| 255 | + "function_()\n", |
| 256 | + "\n", |
| 257 | + "print(' GLOBAL var: ', WARP_CORE)" |
| 258 | + ] |
| 259 | + }, |
| 260 | + { |
| 261 | + "cell_type": "markdown", |
| 262 | + "metadata": {}, |
| 263 | + "source": [ |
| 264 | + "## Commenting" |
| 265 | + ] |
| 266 | + }, |
| 267 | + { |
| 268 | + "cell_type": "markdown", |
| 269 | + "metadata": {}, |
| 270 | + "source": [ |
| 271 | + "As shown above using <code> # </code> comments-out the code\n", |
| 272 | + "\n", |
| 273 | + "for multi-line commenting use <code> /* lines of comments */ </code> \n", |
| 274 | + "<br>\n", |
| 275 | + "or can use <code> \"\"\" .... lots of multi-line comments .... \"\"\" </code>" |
| 276 | + ] |
| 277 | + }, |
| 278 | + { |
| 279 | + "cell_type": "markdown", |
| 280 | + "metadata": {}, |
| 281 | + "source": [ |
| 282 | + "<h3> Commenting is important ! </h3>\n", |
| 283 | + "<p> Making comments while you code or even before you start work is a good practice to get into. Why? Because you will most likely forget what your code, variables and functions are and what they are doing. Commenting is helpful when you stop working on a project and then later return to have comment notes telling you what you did and why. </p>\n", |
| 284 | + "\n", |
| 285 | + "Another reason to write comments is that it's helpful for fellow Starfleet Officers that see your source code know and understand what you did and what functions are. Think of these comments as a message through the Space-Time Continuum telling you what the code means without the temporal distortions.\n", |
| 286 | + "\n", |
| 287 | + "- If you wish to read up on Starfleet's Guidelines on comments: [Federation Commenting Guideline](https://en.wikibooks.org/wiki/Python_Programming/Source_Documentation_and_Comments)" |
| 288 | + ] |
| 289 | + } |
| 290 | + ], |
| 291 | + "metadata": { |
| 292 | + "kernelspec": { |
| 293 | + "display_name": "Python 3", |
| 294 | + "language": "python", |
| 295 | + "name": "python3" |
| 296 | + }, |
| 297 | + "language_info": { |
| 298 | + "codemirror_mode": { |
| 299 | + "name": "ipython", |
| 300 | + "version": 3 |
| 301 | + }, |
| 302 | + "file_extension": ".py", |
| 303 | + "mimetype": "text/x-python", |
| 304 | + "name": "python", |
| 305 | + "nbconvert_exporter": "python", |
| 306 | + "pygments_lexer": "ipython3", |
| 307 | + "version": "3.8.3" |
| 308 | + } |
| 309 | + }, |
| 310 | + "nbformat": 4, |
| 311 | + "nbformat_minor": 4 |
| 312 | +} |
0 commit comments