{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Lesson A1 – Hello, World!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Hello, World!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

We will start this tutorial writing a \"Hello, World!\" program, i.e a program that outputs \"Hello, World!\" on the monitor.

\n", "\n", "
\n", "\n", "**Info:** The \"Hello, World!\" program is a little tradition in starting to learn a programming language. You can find examples of its implementation in all kinds of languages [here](http://helloworldcollection.de/).\n", "\n", "
\n", "\n", "

In Python, we can do this with the following line. We open a Jupyter notebook session and execute this statement in a code cell:

" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, World!\n" ] } ], "source": [ "print(\"Hello, World!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Although this is fairly simple, it includes already a lot of the\n", "elements, we need to understand to learn programming in Python.\n", "First of all, let's consider the expression `\"Hello, World!\"` in double\n", "quotes itself. By putting a sequence of alpha-numerical characters in\n", "quotes, Python recognises this as a *string* object. We will learn\n", "more on strings later. Second, we have used the `print` directive. This is\n", "considered a *function* object. Functions are used to do specific things.\n", "While we will look at functions in Python\n", "in more detail later, note only so much now: A function can be *called*,\n", "that is told to execute its purpose, by using parentheses: `print()`. This\n", "function takes a string, `\"Hello, World!\"`, as an *argument*. As a\n", "consequence of our function call, the string passed to the function, is\n", "displayed on the screen." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "**Note:** Python knows different __objects__. The text `\"Hello, World!\"` is a __string__ object. The command `print` is a __function__ object.\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In a Jupyter notebook (or any other interactive Python session),\n", "you can also just type in `\"Hello, World!\"` without using the `print`\n", "function." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello, World!'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"Hello, World!\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "**Advanced:** If you wonder, why the result looks different, this is\n", " because a fundamentally different thing happens here. Simply using\n", " an object as __input__, will give you the largely unmodified object\n", " back as __output__. Using the `print` function on the other hand, processes\n", " its argument, prints it to the screen, and __returns__ an empty\n", " object (`None`) as output.\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Comments" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now that you have written your first program in Python, let's take a\n", "moment to discuss one of the most important topics for writing code, which\n", "is *documenting* and *commenting* what you write. Comments are parts\n", "of your code that are ignored by the interpreter and are not executed.\n", "One way to do this, is to put a number sign (#) in your code, after which\n", "everything is regarded a comment." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, World!\n" ] } ], "source": [ "print(\"Hello, World!\") # Comment in-line" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, World!\n" ] } ], "source": [ "print(\"Hello, World!\")\n", "# Comment after the commented statement" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Alternatively you can introduce comments to your code as strings. By\n", "enclosing the text of a comment between three quotation marks at the\n", "beginning and the end, you can use multi-line strings as comments. It is\n", "considered good practice to use a comment like this at the very beginning of\n", "programs or program parts you write its usage. Others who will read your code and especially\n", "your future self will be grateful to you." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, World!\n" ] } ], "source": [ "\"\"\"This is a little program that prints \"Hello, World!\" to the screen.\n", "\n", "A comment like this can span over multiple lines.\n", "Everything inside the three quotes will be ignored.\"\"\";\n", "\n", "print(\"Hello, World!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "**Note:** In an interactive Python session, typing in a bare string\n", " like this will give you the string back as output. To surpress\n", " this you can use a semicolon (;) after the string.\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Arguably, the above line of code is quite self-explanatory. Always try to\n", "make your code as comprehensible as possible by itself and avoid redundant\n", "comments. For instance, don't do this (except for making a point, of course):" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, World!\n" ] } ], "source": [ "print(\"Hello, World!\") # prints: \"Hello, World!\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Errors" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "One of the typical things you will encounter while programming, is that\n", "your programs are not doing what you intended or what you have might expected.\n", "You are likely to produce errors when running your code. Errors are a\n", "very good thing. The worst case scenario that can happen, is that your\n", "program does something unexpected and does not throw an error at you. Errors\n", "are your friends, because they tell you what has gone wrong. Here, we will\n", "provoke an error by calling something Python does not know (more on defining objects and making Python know things in the next exercise)." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'x' is not defined", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mNameError\u001b[0m: name 'x' is not defined" ] } ], "source": [ "print(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Error messages can be quite confusing and long, depending on the situation.\n", "For a start, you might want to look at the very end of the printed message.\n", "In the last line of the error message, Python will tell you the *type*\n", "of error that has been *raised*. The `NameError` means, that Python does\n", "not know an object named 'x', yet. It simply does not exist. After the error\n", "type, you will get a short explanation of the error. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "**Note:** For different kinds of situations, Python raises different\n", " kinds of [errors](https://docs.python.org/3/library/exceptions.html).\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To resolve the error in your code, you may want to read the message\n", "from the top. What is printed there is called a `Traceback`, which can be\n", "considered a special log of the most recent events in your program. An\n", "arrow (`---->`) pointing to a line number (`1`) indicates the position in your\n", "code where the error occurred. We will learn more on how to handle errors\n", "later. Here is just another type of error, that is very common." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "unexpected EOF while parsing (, line 1)", "output_type": "error", "traceback": [ "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m print(x\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m unexpected EOF while parsing\n" ] } ], "source": [ "print(x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A `SyntaxError` occurs if you have violated Python's grammar in a way that the \n", "interpreter does not understand what you mean. Often it is just that parenthesis\n", "have been forgotten or misplaced. Luckily those are easy to solve\n", "most of the time. " ] } ], "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.3" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": true, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": { "height": "calc(100% - 180px)", "left": "10px", "top": "150px", "width": "263.6px" }, "toc_section_display": true, "toc_window_display": false }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }