{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "source": [
    "# Python and Jupyter Basics\n",
    "\n",
    "<a id='the_top'></a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Notebook \"cells\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The notebook is divided into cells of different types\n",
    "- **Code** cells\n",
    "- **Markdown** cells for formatting text and writing descriptions\n",
    "\n",
    "Browse the buttons and menus\n",
    "- You can insert cells above or below a selected cell\n",
    "- You can execute a cell using the \"play\" button\n",
    "    - Or, press **```ctrl-enter```** (VScode), or **```shift-enter```** (Jupyter lab/notebook), which is faster\n",
    "- Cells can be dragged to different locations\n",
    "- Buttons to Run All Cells, Restart the kernel, etc\n",
    "\n",
    "VScode, Juypyter lab, etc., are editors for notebook files that are attached to independent, language-specific kernels that run the code. Notebooks can run code for many different languages."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Code cells"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Code cells are where we write python code.\n",
    "\n",
    "The code is the same syntax whether we are using VScode, Jupyter Lab, Jupyter Notebook, or running python using a text file and a Python interpreter."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Comments"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- Comments in Python start with a ```#``` sign.   \n",
    "- Anything after the ```#``` is ignored.\n",
    "- Comments are good for explaining what you are doing, and showing units.\n",
    "- You should ALWAYS include descriptive comments in your code so that others will know what you are doing. \n",
    "    - Also, you will know what you did when you come back and try to read your own code days or weeks later."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Variables"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- Variables don't have to be declared (unlike many languages), just define them and go.\n",
    "- Variables start with letters\n",
    "    - But then can include letters, numbers, or underscores.\n",
    "    - Don't include crazy symbols like ```$``` or ```&```, etc.\n",
    "- Variables are case sensitive\n",
    "- Any variable that you declare with units MUST have a comment stating the units.\n",
    "- Line things up for clarity. \n",
    "- Add blank lines to break up sections.\n",
    "\n",
    "**Examples**\n",
    "- Compare the two versions below for readability"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "code_folding": []
   },
   "outputs": [],
   "source": [
    "T    = 298.15          # Temperature in (K)\n",
    "v_2  = 15.0            # velocity at position 2 (m/s)\n",
    "Pres = 101325.0        # Pressure in (Pa)\n",
    "\n",
    "mdot_in = 35.0            # kg/s\n",
    "mDot_in = 15.0            # kg/s (this is not the same as mdot_in)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "T = 298.15   # Temperature in (K)\n",
    "v_2 = 15.0   # velocity at position 2 (m/s)\n",
    "Pres = 101325.0  # Pressure in (Pa)\n",
    "mdot_in = 35.0   # kg/s\n",
    "mDot_in = 15.0   # kg/s (this is not the same as mdot_in)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Math operators\n",
    "\n",
    "- Use the standard operators: ```+ - / *```\n",
    "- Use a double star to take a power: ```2**4```\n",
    "- Use parentheses to group expressions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Indentation\n",
    "\n",
    "Indentation matters in Python. Blocks of code have to have the same indentation level\n",
    "- This usually shows up for \n",
    "    - conditional blocks (like ```if``` statements)\n",
    "    - loops\n",
    "    - functions\n",
    "    - classes\n",
    "- This is good. Consistent indentation makes code more readable.\n",
    "\n",
    "**Examples**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "T = 5\n",
    "v_3 = 6         # this line won't work if indented"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Notice how Python gives you an error, and tells you what is wrong."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if v_2 > 2.2 :\n",
    "    print(\"v_2 is > 2.2\")        # this works, indented\n",
    "    print(\"Another line\")\n",
    "print(\"This line is not indented, so is not part of the if statement\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Strings\n",
    "\n",
    "- Single and Double quotes are basically the same thing\n",
    "    - Single quotes 'some string'\n",
    "    - Double quotes \"some string\"\n",
    "- Triple strings are useful for a multi-line string: preserves new-lines and white space. \n",
    "    - Use for documenting a function\n",
    "    - Triple quotes \"\"\"some string\"\"\" or '''some string'''"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Printing results\n",
    "* The last statement entered in a cell gets output to the screen.\n",
    "    * (unless you end with a semicolon)\n",
    "* To output other values, use the ```print``` statement"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pi = 3.14\n",
    "Rg = 8314.46\n",
    "me = \"David\"\n",
    "print(\"Hi there,\", me)\n",
    "print(\"Rg =\", Rg, \"and pi =\", pi)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Getting help\n",
    "\n",
    "* Type  ```print?``` in a cell (note the \"?\"). When you run the cell, function documentation appears at the bottom of the screen.\n",
    "* You can also type ```help(print)```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "help(print)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Continuation\n",
    "\n",
    "* You can put more than one command on a line if you separate them with a semicolon \";\"\n",
    "* A command can be spread over several lines using a backslash \\ at the end of the continuation line.\n",
    "    * Or you can use parentheses to force continuation to the ending parentheses."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 5; b = 6       # two commands on one line separated by ;\n",
    "\n",
    "4.45 + \\\n",
    "6.7                # command runs onto next line with \n",
    "\n",
    "(4.45 +            # or use parentheses\n",
    "6.7)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Cell order\n",
    "O\n",
    "- Code is run top to bottom in a given cell, and have values depending on the order cells are run in.\n",
    "    - Variables and functions need to be defined when *used*, not necessarily by order in the notebook.\n",
    "- Variables and functions run in cells are visible and can be changed in other cells\n",
    "- The numbers to the left of cells indicate the order the cells have been run in.\n",
    "- Normally cells are run from top to bottom through the notebook\n",
    "    - If you get into trouble, clear the outputs and restart the kernel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5 6\n"
     ]
    }
   ],
   "source": [
    "print(a,b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "b = 6"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Markdown Cells"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Markdown is used for formatting text cells. \n",
    "* [Wikipedia page](https://en.wikipedia.org/wiki/Markdown)\n",
    "* [Official website](http://daringfireball.net/projects/markdown/)\n",
    "\n",
    "**More on this later**\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "hide_input": false,
  "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.14.6"
  },
  "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": 4
}
