Skip to content
Snippets Groups Projects
Jeg elsker ITGK(ENG).ipynb 13.7 KiB
Newer Older
Swicech's avatar
Swicech committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": false,
    "editable": false,
    "run_control": {
     "frozen": true
    },
    "solution": "hidden"
   },
   "source": [
    "# I love ITGK\n",
    "\n",
    "**Learning goals:**\n",
    "\n",
    "* Print text and numbers to the console\n",
    "\n",
    "* Write a  simple program\n",
    "\n",
    "**Starting Out with Python:**\n",
    "\n",
    "* Chapt. 1.5\n",
    "\n",
    "* Chapt. 2.2-2.4"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": false,
    "editable": false,
    "heading_collapsed": true,
    "run_control": {
     "frozen": true
    }
   },
   "source": [
    "### print() - tutorial part 1:\n",
    "Do not hesitate to read this part before doing the subsequent tasks, especially if you have never programmed before."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": false,
    "editable": false,
    "hidden": true,
    "run_control": {
     "frozen": true
    }
   },
   "source": [
    "Programs that are gonig to be used by humans, often has to display information on the screen. A simple way to accomplish this in Python, is the function print().\n",
    "\n",
    "The code below tries to give a self explaining introduction to the print statement.\n",
    "\n",
    "**Hit control + enter with the cell below activated in order to run the code. You may try to change some of the code and see what happens!**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-06-25T13:22:53.510622Z",
     "start_time": "2019-06-25T13:22:53.493007Z"
    },
    "hidden": true,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "print('The text that is going to be displayed, is inserted within the parentheses following print.')\n",
    "print('The text must be enclosed with single quotes (apostrophes)')\n",
    "print(\"or with double quotes.\")\n",
    "print('Numbers do not need quotes surrounding them:')\n",
    "print(42)\n",
    "print('A blank line can be printed with empty parenthenses:')\n",
    "print()\n",
    "print('You can print several things', ' using commas between: ', 5, 6)\n",
    "print('Decimal numbers must be written with a dot in Python: ', 3.14)\n",
    "print('Using comma is interpreted as to separate numbers:', 3, 14)\n",
    "print('Commas within text are outputted to the screen: ,,,,,,', 'comma', 'between', 'texts do not')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": false,
    "editable": false,
    "hidden": true,
    "run_control": {
     "frozen": true
    }
   },
   "source": [
    "As the explantation states, there are **two alternative signs** that can be used to enclose text strings in Python:\n",
    "\n",
    "* Single quote - On a Norwegian PC keyboard, you can find it between Æ and Enter. On Mac, it is the key to the left of number 1.\n",
    "* Double quotes - Both on PC and Mac, you can find it on the same key as the number 2.\n",
    "\n",
    "Whether you decide to use single or double quotes, is a matter of taste - just make sure you are consistent and use the same sign at the start and end of the text string. In certain situations, one of the options may be preferred over the other, as explained in part 2 of the tutorial.\n",
    "\n",
    "If you on accident input the wrong sign, for instance a bent single quote apostrophe or bent double quotes instead of a right (neutral) apostrophe or neutral double quotes, the code will not work. If you for instance write Python code in Word, Word may automatically convert the neutral sign to a different type (which are different depending upon whether it is at the start or end of the text). This is one of the many reasons why you should not be writing Python code in Word - rather use a dedicated code editor such as Jupyter, Spyder, or Pycharm.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": false,
    "editable": false,
    "run_control": {
     "frozen": true
    },
    "solution": "hidden"
   },
   "source": [
    "### a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": false,
    "editable": false,
    "run_control": {
     "frozen": true
    }
   },
   "source": [
    "**Create a program that prints out the following to the screen:**\n",
    "\n",
    "```\n",
    "I love ITGK!\n",
    "```\n",
    "\n",
    "***Write your code in the box below.***"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": false,
    "editable": false,
    "run_control": {
     "frozen": true
    }
   },
   "source": [
    "### b)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": false,
    "editable": false,
    "run_control": {
     "frozen": true
    }
   },
   "source": [
    "Create a program that uses four print statements in order to print the information below to the screen (line 2 is supposed to be lank). The numbers are supposed to be printed out as numbers, **not** as a part of a text string surrounded by quotes. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-06-25T13:03:36.211485Z",
     "start_time": "2019-06-25T13:03:36.201649Z"
    },
    "deletable": false,
    "editable": false,
    "run_control": {
     "frozen": true
    }
   },
   "source": [
    "```python\n",
    "Norway\n",
    " \n",
    " \n",
    "Area (sq.km): 385180\n",
    " \n",
    "Population (million): 5.3\n",
    "```\n",
    "\n",
    "***Write your code in the box below.***"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": false,
    "editable": false,
    "heading_collapsed": true,
    "run_control": {
     "frozen": true
    },
    "solution": "hidden"
   },
   "source": [
    "### print() - tutorial part 2:\n",
    "Useful information about quotes in strings before task c and d."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": false,
    "editable": false,
    "hidden": true,
    "run_control": {
     "frozen": true
    }
   },
   "source": [
    "The code below illustrates why Python has several options for quotes enclosing text strings rather than standardizing on only one sign for this purpose. **Feel free to change the code and see what happens!**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-06-25T13:37:55.517387Z",
     "start_time": "2019-06-25T13:37:55.490047Z"
    },
    "hidden": true
   },
   "outputs": [],
   "source": [
    "print('Why are both single', \"and double quotes offered?\")\n",
    "print('Well, if there are double \"quotes\" within the text, only single quotes will correctly enclose it,')\n",
    "print(\"and with single 'quotes' within the text, only double quotes will correctly enclose it.\")\n",
    "print(\"With the same quote used both within a text and to enclose it, Python thinks the text stops where it is used within.\")\n",
    "print(\"If you want to use both types of quotes within the text, then you must enclose it with triple quotes:\")\n",
    "print('''Are y'all mad? \"Arne\" asked and laughed hysterically''')\n",
    "print(\"\"\"Triple single quote ' and triple double quote \" both work.\"\"\")\n",
    "print(\"\"\"Triple quotes may also be used\n",
    "for text strings\n",
    "that span multiple lines.\n",
    "\"\"\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": false,
    "editable": false,
    "run_control": {
     "frozen": true
    }
   },
   "source": [
    "### c)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": false,
    "editable": false,
    "run_control": {
     "frozen": true
    }
   },
   "source": [
    "**Create a program that prints the following text to the screen:**\n",
    "```\n",
    "\"I love ITGK\" the student shouted when he got 1c to work.\n",
    "```\n",
    "\n",
    "The quotes enclosing the text is supposed to be a part of the displayed output.\n",
    "\n",
    "***Write the code in the box below.***"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": false,
    "editable": false,
    "run_control": {
     "frozen": true
    }
   },
   "source": [
    "### d)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": false,
    "editable": false,
    "run_control": {
     "frozen": true
    }
   },
   "source": [
    "Create a program that prints out to the screen the text shown below.\n",
    "\n",
    "Hint: Avoid having to write much by copying the text in the box below. Then you only need to write print, parentheses and enclose the text with the appropriate quotes.\n",
    "\n",
    "**Text for task d):**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": false,
    "editable": false,
    "run_control": {
     "frozen": true
    }
   },
   "source": [
    "```\n",
    "Some children were hanging around at the playground.\n",
    "The topic of discussion was somewhat unexpected.\n",
    "\n",
    "- Why is it named Python?\n",
    "- Why shouldn't it be? One of the children responded.\n",
    "- Who's silly enough to give it that name?\n",
    "- Perhaps he was a snake? - No, it's Guido Van Rossum.\n",
    "- Maybe he was into snakes? - Nah, he was into \"Monty Python\".\n",
    "- What's that? A mountain?\n",
    "- No, an English comedian group. Started in '69.\n",
    "- Wow! Were dinosaurs still around then?\n",
    "```\n",
    "***Write the code in the box below.***"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": false,
    "editable": false,
    "heading_collapsed": true,
    "run_control": {
     "frozen": true
    }
   },
   "source": [
    "### Comments in Python - tutorial part 3:\n",
    "Useful information before task e)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": false,
    "editable": false,
    "hidden": true,
    "run_control": {
     "frozen": true
    }
   },
   "source": [
    "Comments is another useful tool we use when we write code. It is mostly used for your own sake, and for others that are going to read through and interpret your code, as it makes it more structured. It is particularly useful for when you or others go back and read through your old code. Comments are ignored by the program when it is executed, i.e. what you write in the comments does not affect the code. Commenrs are made by putting a hash tag (#) in front of text you wish to comment out. If you attempt to run the code below, you will receive an error. **Try to fix the code!**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-06-25T13:30:57.214061Z",
     "start_time": "2019-06-25T13:30:57.200831Z"
    },
    "hidden": true
   },
   "outputs": [],
   "source": [
    "# This is a comment!\n",
    "This is not a comment. Missing #."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": false,
    "editable": false,
    "run_control": {
     "frozen": true
    }
   },
   "source": [
    "### e)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": false,
    "editable": false,
    "run_control": {
     "frozen": true
    }
   },
   "source": [
    "In your code from b) and c), add two comments where you explain what was done in each task. In addition add the comment `print('Hei\")` in one of the the subtasks.\n",
    "\n",
    "Note that if you had written print('Hei\") in the code, it would not have run. This is due to the fact that at the start of the text, there is a single quote, while at the end, there is a double quote. As long as this is written in a comment, such as shown above, it will not affect the code and everything will be okay. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": false,
    "editable": false,
    "run_control": {
     "frozen": true
    }
   },
   "source": [
    "### f)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": false,
    "editable": false,
    "run_control": {
     "frozen": true
    }
   },
   "source": [
    "The code in the code block below will not run because of syntax errors in all of the print statements. Your task is to make the code run by correcting the errors."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"Hellohello, I guess I did not wanna compile :(')\n",
    "print(\"Hello, how \"good\" you are looking today\")\n",
    "print(Hello there)\n",
    "print \"This is fun, right?\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}