{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3f6bd5ac",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "source": [
    "# A.1 Basics"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b2831d20",
   "metadata": {},
   "source": [
    "## Before starting\n",
    "\n",
    "There are a few things that are worth mentioning upfront about Python, as they form typical traps for novice users. First of all, Python is case sensitive, so a variable with the name `avariable` is not the same as `Avariable` or `AVARIABLE`. Second, systematic indentation is not only good coding style, but it is a feature of the Python language. Third, Python starts counting at zero (for those who have trouble getting used to this, it may help to remember that the first hour of a day also starts at zero, or that you don’t turn one until one year after you were born). All three aspects will become clear in the examples that follow."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c2caec5a",
   "metadata": {},
   "source": [
    "## Arithmetic\n",
    "\n",
    "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Because Python is an interpreted language, one of its uses is as a basic calculator. For example, to compute 6 * 2, type"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c4b85c17",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "6 * 2 # Place the cursor here and hit [shift][enter]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d200f793",
   "metadata": {},
   "source": [
    "The spaces are added around the multiplication symbol to make the code more readable. `6*2` works just as well, but it is not as easy to read and is considered to be poor style. Style guidelines for Python can be found at [www.python.org/dev/peps/pep-0008/](https://peps.python.org/pep-0008/). The example above also shows how single-line comments can be included by typing text behind a `#` symbol. Python ignores any code on a line after a `#` symbol.\n",
    "<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Exponentiation is performed by using `**`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f039c54a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "64"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2 ** 6"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3ca50f6d",
   "metadata": {},
   "source": [
    "A hyphen (minus sign) swaps the sign of a number. Some care must be taken to make sure that the intended numerical operations are carried out in the correct order. In the following example, the number 2 is first raised to the power of 4, and only then is the sign swapped"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "404deabd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-16"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "-2 ** 4"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c9421e02",
   "metadata": {},
   "source": [
    "If, instead, the desired outcome is to raise −2 to the power 4, parentheses are required"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "850b155f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "16"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(-2) ** 4"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b34714b2",
   "metadata": {},
   "source": [
    "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Values may be stored in variables by typing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "762f2dad",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = 6\n",
    "b = 2\n",
    "a * b"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2164a127",
   "metadata": {},
   "source": [
    "Both `a` and `b` are now variables. Once a variable has been created in a session, it remains in memory, so it can be used in other code cells. Arithmetic operations can be performed on variables in the same way as on numbers. Because various operations can be performed on variables, their numerical value can change during the course of a program.\n",
    "<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Variable names cannot have spaces, nor can they start with a number. As mentioned earlier, variable names are case sensitive, so a variable called `v` is not the same as `V`. Some words are reserved and can’t be used as variable names, such as `and`, `True`, `False`, and `lambda`. Care must be taken not to choose variable names that have the same name as existing Python functions. Many code editors, including Jupyter Notebooks, recognize reserved words and color-code them (called syntax highlighting), which helps to avoid mistakes.\n",
    "<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Each variable has a type. The most common variable types include the following:\n",
    "\n",
    "1. Integer\n",
    "2. Floating-point number (or ‘float’)\n",
    "3. String (which contains text)\n",
    "4. Boolean (which is either true or false)\n",
    "5. Complex number\n",
    "\n",
    "Unlike some other programming languages, there is no need to explicitly specify the type of a variable when it is first defined because Python infers it automatically, as demonstrated in the following example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "951f8f32",
   "metadata": {},
   "outputs": [],
   "source": [
    "a1 = 6 # integer\n",
    "a2 = 6. # float\n",
    "a3 = 'a string is enclosed by single or double quotes' # string\n",
    "a4 = True # boolean\n",
    "a5 = 2 + 3j # complex number"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1a35809e",
   "metadata": {},
   "source": [
    "Note that the imaginary part of a complex number is indicated by `j` (rather than `i`). Also note that executing this code cell in a notebook does not generate any output, as was the case with the previous code cells. Only the last line of a code cell automatically prints output to the screen, except when the output is stored in a variable or when a semi-colon is added at the end of the last line.\n",
    "<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; In general, an operation on two variables of the same type results in a variable of the same type. For example, multiplication of two integers results in an integer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "df1040bf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "32"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "8 * 4 # results in an integer"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ee37af89",
   "metadata": {},
   "source": [
    "The only exception is that the division of two integers automatically gives a float, even if there is no remainder. (Historical note: This behavior used to be different and quite confusing in Python 2.)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "573a4916",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.0"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "8 / 4 # results in a float"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "92eed3f8",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "source": [
    "## Getting help\n",
    "\n",
    "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Before moving on, it might be useful to discuss the help options in a notebook. Help for functions can be obtained in several ways. The easiest is to type the name of the function in a code cell and then open a parenthesis like (if code-completion is active, a closing parenthesis might appear automatically)\\\n",
    "`float(`\\\n",
    "and then hit the [shift]+[tab] keys together to pop-up a help box. If there is a lot of help, you can scroll through the help box to see more. Click anywhere outside the help box to make it disappear agin. Alternatively, you can type\\\n",
    "`float?`\\\n",
    "in a code cell and hit [shift]+[enter]. The help will now appear as output below the code cell. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7c7b2442",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "source": [
    "## Screen output\n",
    "\n",
    "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The `print` function is used to display the value of a variable on the screen"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "546da523",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n"
     ]
    }
   ],
   "source": [
    "b = 6\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b19ce3fb",
   "metadata": {},
   "source": [
    "In the previous code cell, simply typing b on the last line also works, as explained previously. When using the print command, `Out[n]:` (`n` being a number) does not appear in front of what is being printed. The `print` function can take multiple arguments"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "cdf61258",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.0 3.0\n"
     ]
    }
   ],
   "source": [
    "print(a5.real, a5.imag)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "851daa49",
   "metadata": {},
   "source": [
    "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String arguments may be mixed with variables"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "b12adbc3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The value of a is 6\n"
     ]
    }
   ],
   "source": [
    "print('The value of a is', a)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5b2be956",
   "metadata": {},
   "source": [
    "It is better (and nicer) to combine text and variables using f-strings. The string must be preceded by the letter `f`, and the variables that are printed are enclosed by braces `{}`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "810fa703",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The value of a is 6\n"
     ]
    }
   ],
   "source": [
    "print(f'The value of a is {a}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ff49a1c5",
   "metadata": {},
   "source": [
    "## Lists\n",
    "\n",
    "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;A `list` is a Python data structure that contains a collection of data that can be of any type. It is defined by typing the elements of the list separated by commas and enclosed by square brackets. The following list contains a string, an integer, and a float."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "e41f279d",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Darcy', 0, 2.3]\n"
     ]
    }
   ],
   "source": [
    "alist = ['Darcy', 0, 2.3]\n",
    "print(alist)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2d1f5756",
   "metadata": {},
   "source": [
    "An individual item in a list can be accessed by specifying the index in the list between square brackets. Selecting elements like this is called indexing or slicing, which is discussed in more detail in the section on arrays. Remember that Python counts using a zero-based system, so the first element in the list has index number 0. A range of items can be selected by specifying the first index and the last index separated by a colon"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "4b9e3bbd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Darcy\n",
      "['Darcy', 0]\n"
     ]
    }
   ],
   "source": [
    "print(alist[0])\n",
    "print(alist[0:2]) # Returns items 0 and 1, but not item 2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "33d6ca88",
   "metadata": {},
   "source": [
    "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; The second print statement above only returns items 0 and 1, but not item 2. This is because of the way the counting works in Python: It starts at zero and then stops before the last number is reached. Items in a list can be changed. The first item in `alist` can be changed as follows"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "2ed7e3be",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Dupuit', 0, 2.3]\n"
     ]
    }
   ],
   "source": [
    "alist[0] = 'Dupuit'\n",
    "print(alist)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f225d540",
   "metadata": {},
   "source": [
    "Items can also be appended to a list. For example, the number 12 can be added to `alist`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "e980dc2b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Dupuit', 0, 2.3, 12]\n"
     ]
    }
   ],
   "source": [
    "alist.append(12)\n",
    "print(alist)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bfc8b91c",
   "metadata": {},
   "source": [
    "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; The `+` operator can be used to combine two lists"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "9f771b40",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, 3, 5, 10, 15)\n"
     ]
    }
   ],
   "source": [
    "a = (1, 2, 3)\n",
    "b = (5, 10, 15)\n",
    "print(a + b)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "75c5e035",
   "metadata": {},
   "source": [
    "Note that the use of the `+` sign in the last code cell does not imply an arithmetic operation. The numbers in the lists are not added. If the intent is to add the numbers, the lists must first be converted to `arrays`, which are discussed later on.\n",
    "<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; A `list` is one of several basic `sequence` types in Python. Another one is the `tuple`, which, like a `list`, is a sequence of items, but it is defined by enclosing the elements between parentheses rather than square brackets. The difference between a `list` and a `tuple` is that elements in a `tuple` cannot be changed after the tuple has been defined."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.13.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
