diff --git a/IntroProgrammingInPython.ipynb b/IntroProgrammingInPython.ipynb new file mode 100644 index 0000000000000000000000000000000000000000..270aefc560ea36056318d05e8654ccb9d9a2a9a9 --- /dev/null +++ b/IntroProgrammingInPython.ipynb @@ -0,0 +1,380 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "a1b0abfd-330c-4c9a-81e0-8c996948cf16", + "metadata": { + "slideshow": { + "slide_type": "slide" + }, + "tags": [] + }, + "source": [ + "# Introduction to programming in Python\n", + "\n", + "Author: Auke Klazema (klazema@astron.nl)\n", + "Length: 2.5 days" + ] + }, + { + "cell_type": "markdown", + "id": "785876cb-ef7d-46cf-aa9e-20950b6e8574", + "metadata": { + "slideshow": { + "slide_type": "slide" + }, + "tags": [] + }, + "source": [ + "## What is programming?" + ] + }, + { + "cell_type": "markdown", + "id": "f8081ed1-9c6b-4c0b-8f10-0cc8fad37a9a", + "metadata": { + "slideshow": { + "slide_type": "slide" + }, + "tags": [] + }, + "source": [ + "## What is a programming language?" + ] + }, + { + "cell_type": "markdown", + "id": "ecf1f80a-3312-4f3c-85c8-6199daad071d", + "metadata": {}, + "source": [ + "## Python 3 introduction" + ] + }, + { + "cell_type": "markdown", + "id": "cf433491-7990-443f-ae1a-3157a09a3c37", + "metadata": {}, + "source": [ + "\n", + "\n", + "## First working program\n", + "\n", + "Lets start out with a working simple program." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f3b3fb00-a25e-4050-8d41-ad9a10678bf9", + "metadata": {}, + "outputs": [], + "source": [ + "#!/bin/env python3\n", + "\n", + "print(\"Hello World\")" + ] + }, + { + "cell_type": "markdown", + "id": "0fbd93a6-fa1c-4f38-a6e5-99c2ec222c91", + "metadata": {}, + "source": [ + "This is a small script / program that outputs the string plus a newline on the standar output.\n", + "\n", + "The first line is the shebang that tells my terminal how to run the program. This way I only have to execute my script and the terminal feeds it into python. An alternative is to run it directly with python like so: python helloworld.py\n", + "\n", + "Then we have a empty line that gets ignored by Python. The last line contains a built-in function called \"print\" and this function accepts a couple of arguments. In this case I gave it the string \"Hello World\".\n", + "\n", + "Lets look up the documentation for print." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "6bf448b7-7e4f-46ff-81d8-0b94d97104fe", + "metadata": { + "slideshow": { + "slide_type": "subslide" + }, + "tags": [] + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Help on built-in function print in module builtins:\n", + "\n", + "print(...)\n", + " print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)\n", + " \n", + " Prints the values to a stream, or to sys.stdout by default.\n", + " Optional keyword arguments:\n", + " file: a file-like object (stream); defaults to the current sys.stdout.\n", + " sep: string inserted between values, default a space.\n", + " end: string appended after the last value, default a newline.\n", + " flush: whether to forcibly flush the stream.\n", + "\n" + ] + } + ], + "source": [ + "#!/bin/env python3\n", + "\n", + "help(print)" + ] + }, + { + "cell_type": "markdown", + "id": "6ec87341-5d37-4c12-a849-0ed856c55e67", + "metadata": {}, + "source": [ + "The print documentation can also be found on the online documentation at python.org at https://docs.python.org/3/library/functions.html#print\n", + "\n", + "```\n", + "print(*objects, sep=' ', end='\\n', file=sys.stdout, flush=False)\n", + "\n", + " Print objects to the text stream file, separated by sep and followed by end. sep, end, file and flush, if present, must be given as keyword arguments.\n", + "\n", + " All non-keyword arguments are converted to strings like str() does and written to the stream, separated by sep and followed by end. Both sep and end must be strings; they can also be None, which means to use the default values. If no objects are given, print() will just write end.\n", + "\n", + " The file argument must be an object with a write(string) method; if it is not present or None, sys.stdout will be used. Since printed arguments are converted to text strings, print() cannot be used with binary mode file objects. For these, use file.write(...) instead.\n", + "\n", + " Whether output is buffered is usually determined by file, but if the flush keyword argument is true, the stream is forcibly flushed.\n", + "\n", + " Changed in version 3.3: Added the flush keyword argument.\n", + "```" + ] + }, + { + "cell_type": "markdown", + "id": "701d1e4e-fc0a-4164-abac-cc2f49f5c88b", + "metadata": {}, + "source": [ + "So based on the information given in this first section I could spend days talking. But this is a beginners course on programming. We happen to use Python as our tool of learning so lets get some basics out of the way first. Its fine if not everything is understood from the start because I will revisit them later.\n", + "\n", + "The help text of print talks about values, streams, sys.stdout. It mentions functions, objects and modules. It also mentions optional keywords and strings. We only have 2.5 days so I will need to leave out a lot of things that Python offers. Some will be covered in other courses Astron will give but others will need to be learned through other means." + ] + }, + { + "cell_type": "markdown", + "id": "2babb3aa-8be9-4fe2-8dbc-f3fa8aff2574", + "metadata": {}, + "source": [ + "## First broken programs\n", + "\n", + "Your programs will be broken alot, especially in the beginning, so lets see how to deal with that." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0c656ee3-870f-4f51-aad4-4fd2dbf160b6", + "metadata": {}, + "outputs": [], + "source": [ + "#!/bin/env python3\n", + "\n", + " print(\"Hello World\")" + ] + }, + { + "cell_type": "markdown", + "id": "95082acd-7c8b-4138-9189-84af128a0a93", + "metadata": {}, + "source": [ + "This program will give a NameError." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6d99111d-b279-4f22-8862-8c59df632237", + "metadata": {}, + "outputs": [], + "source": [ + "#!/bin/env python3\n", + "\n", + "printer(\"Hello World\")" + ] + }, + { + "cell_type": "markdown", + "id": "93564fff-c8c9-4059-89f2-82d29453f28e", + "metadata": {}, + "source": [ + "This program will give an IndentationError. Indentation is very important in a Python program." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9fc26361-3d9c-4160-a245-6ef36ff8317f", + "metadata": {}, + "outputs": [], + "source": [ + "#!/bin/env python3\n", + "\n", + "print(\"Hello World)" + ] + }, + { + "cell_type": "markdown", + "id": "7b84478e-b8c8-48ad-b6b5-b29d4b21ad3d", + "metadata": {}, + "source": [ + "This program will give a SyntaxError" + ] + }, + { + "cell_type": "markdown", + "id": "4df3af9b-23e2-45c5-82ed-e7deb2b376aa", + "metadata": {}, + "source": [ + "### Exercise\n", + "\n", + "Try to break your program in more ways." + ] + }, + { + "cell_type": "markdown", + "id": "0823895a-f753-44b2-8f0b-e24b63c3545f", + "metadata": {}, + "source": [ + " Programming introduction\n", + " Programming languages\n", + " Installing Python 3\n", + " Python 3 introduction\n", + " Finding documentation\n", + " Your first program\n", + " Your first bug\n", + " Functions\n", + " Variables\n", + " Numbers and strings\n", + " Basic math\n", + " Data types (lists, dictionaries, sets, tuples, etc)\n", + " Flow control\n", + " Abstraction\n", + " How to solve problems with programming" + ] + }, + { + "cell_type": "markdown", + "id": "2d5d509d-2735-4ff0-9417-d2969f991958", + "metadata": {}, + "source": [ + "## Functions" + ] + }, + { + "cell_type": "markdown", + "id": "41925997-1b78-44a5-a336-ae9a2c87960c", + "metadata": {}, + "source": [ + "## Variables" + ] + }, + { + "cell_type": "markdown", + "id": "bb1d80f2-640b-4d80-8fd4-ef310008b3b0", + "metadata": {}, + "source": [ + "## Numbers and Strings" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "c979d150-9c12-4327-b565-15abe1d1c386", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Hello world'" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#!/bin/env python3\n", + "\n", + "\"Hello world\"" + ] + }, + { + "cell_type": "markdown", + "id": "32d8f383-db80-479a-9aab-20cc430e9739", + "metadata": {}, + "source": [ + "## Basic math" + ] + }, + { + "cell_type": "markdown", + "id": "7c011488-f5dc-4cea-85db-1fa202c2c240", + "metadata": {}, + "source": [ + "## Data types\n", + "### lists\n", + "### dictionaries\n", + "### sets\n", + "### tuples" + ] + }, + { + "cell_type": "markdown", + "id": "13e86fa7-c3ab-45fd-a1c0-876cc51c89c0", + "metadata": {}, + "source": [ + "## Flow control" + ] + }, + { + "cell_type": "markdown", + "id": "e24b0155-ddec-4363-a111-a9d8b0670fc6", + "metadata": {}, + "source": [ + "## Abstraction" + ] + }, + { + "cell_type": "markdown", + "id": "02b69029-abb4-417e-8258-46e01502e971", + "metadata": {}, + "source": [ + "## How to solve problems with programming\n", + "\n", + "Breaking up and composing" + ] + } + ], + "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.8.10" + }, + "toc-autonumbering": false, + "toc-showcode": false, + "toc-showmarkdowntxt": false, + "toc-showtags": false + }, + "nbformat": 4, + "nbformat_minor": 5 +}