{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "12b02b53",
   "metadata": {},
   "source": [
    "<h2>Functions and variables</h2>\n",
    "<hr>\n",
    "<h3>Strings</h3>\n",
    "We'll go through a few examples where we get input from users, use, format and then output strings more or less formatted"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c913485b",
   "metadata": {},
   "outputs": [],
   "source": [
    "#Getting the name and writing it back saying hello\n",
    "name = input(\"What's your name? \")\n",
    "print(\"hello,\", name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "12162899",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello, Kurt!"
     ]
    }
   ],
   "source": [
    "#Changing the default ending(which is new line)\n",
    "name = input(\"What's your name? \")\n",
    "print(\"hello,\", name, end=\"!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "07b46975",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello, Kalle nice day today\n",
      "hello, Kalle nice day today\n",
      "hello, Kalle { nice day today\n"
     ]
    }
   ],
   "source": [
    "#Concatenating \n",
    "#with comma\n",
    "name = input(\"What's your name? \")\n",
    "print(\"hello,\", name, \"nice day today\")\n",
    "#with +\n",
    "print(\"hello, \" + name + \" nice day today\")\n",
    "#with f-string\n",
    "print(f\"hello, {name} {'{'} nice day today\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0acb323e",
   "metadata": {},
   "source": [
    "<b>String format with f-string etc</b>\n",
    "<br>\n",
    "Check out string methods\n",
    "![Alt text](media/stringfunctions.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "63db02ef",
   "metadata": {},
   "outputs": [],
   "source": [
    "#Taking away whitespace\n",
    "name = input(\"What's your name? \").strip()\n",
    "print(f\"hello, {name}, nice day today\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "78ddaa07",
   "metadata": {},
   "outputs": [],
   "source": [
    "#Taking away whitespace only at the beginning\n",
    "name = input(\"What's your name? \").lstrip()\n",
    "print(f\"hello, {name}, nice day today\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "76ef14d7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello, Anders, nice day today\n"
     ]
    }
   ],
   "source": [
    "#Making shure we have a decent titleformat on the name\n",
    "name = input(\"What's your name? \").strip().title()\n",
    "print(f\"hello, {name}, nice day today\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "df2839a6",
   "metadata": {},
   "source": [
    "<b>Using replace, in this case replacing ;) with 😉</b>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "33f75418",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hey dude 😉\n"
     ]
    }
   ],
   "source": [
    "statement = input(\"Say what? \")\n",
    "print(statement.replace(\";)\",\"😉\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d8a02a9",
   "metadata": {},
   "source": [
    "<b>Using split() to split parts of a string into separate values</b>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "1ae63608",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello, Randler,\n"
     ]
    }
   ],
   "source": [
    "name = input(\"What's your name? \").strip().title()\n",
    "first, last = name.split(\" \")\n",
    "print(f\"hello, {first}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2d189109",
   "metadata": {},
   "source": [
    "<hr>\n",
    "<h3>Integers and Floats</h3>\n",
    "Using integers and float is common, so lets look at this."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "9ea2a1c4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The sum is 8\n",
      "The difference is 2\n",
      "The quotient is 1.6666666666666667\n",
      "The integer quotient is 1\n",
      "The remainder is 2\n",
      "5= 1*3+2\n"
     ]
    }
   ],
   "source": [
    "#Integer basics\n",
    "x = int(input(\"What's x? \"))\n",
    "y = int(input(\"What's y? \"))\n",
    "print(f\"The sum is {x+y}\")\n",
    "print(f\"The difference is {x-y}\")\n",
    "print(f\"The quotient is {x/y}\")\n",
    "print(f\"The integer quotient is {x//y}\")\n",
    "print(f\"The remainder is {x%y}\")\n",
    "print(f\"{x}= {x//y}*{y}+{x%y}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b566ebe0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The sum is 8.0\n",
      "The difference is 2.0\n",
      "The quotient is 1.6666666666666667\n",
      "The quotient is 1.667\n"
     ]
    }
   ],
   "source": [
    "#Float basics\n",
    "x = float(input(\"What's x? \"))\n",
    "y = float(input(\"What's y? \"))\n",
    "print(f\"The sum is {x+y}\")\n",
    "print(f\"The difference is {x-y}\")\n",
    "print(f\"The quotient is {x/y}\")\n",
    "print(f\"The quotient is {x/y:.3f}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d458aec6",
   "metadata": {},
   "source": [
    "<hr>\n",
    "<h3>Functions</h3>\n",
    "We've used this a lot already, but lets recap a bit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "c315c901",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The sum is 8\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "#Simple add function\n",
    "def add(x,y):\n",
    "    print(f\"The sum is {x+y}\")\n",
    "x = int(input(\"What's x? \"))\n",
    "y = int(input(\"What's y? \"))\n",
    "print(add(x,y))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "12bcf503",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The sum is 8\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "#Using return\n",
    "\n",
    "a = int(input(\"What's x? \"))\n",
    "b = int(input(\"What's y? \"))\n",
    "sum = add(x,y)\n",
    "print(sum)\n",
    "def add(x,y):\n",
    "    return (f\"The sum is {x+y}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6c498448",
   "metadata": {},
   "outputs": [],
   "source": [
    "#Using main, allows us to change the order a bit, good practice\n",
    "def main():\n",
    "    a = int(input(\"What's a? \"))\n",
    "    b = int(input(\"What's b? \"))\n",
    "    #Using add before it's defined in the text, but not in the flow.\n",
    "    print(add(a,b))\n",
    "    \n",
    "def add(x,y):\n",
    "    return (f\"The sum is {x+y}\")   \n",
    "\n",
    "main()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f490dacf",
   "metadata": {},
   "source": [
    "<b>Solving $ax^2+bx+c=0$ allowing complex solutions</b>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "9247748a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x1 = -1.0 + 1.5811388300841898i\n",
      "x2 = -1.0 - 1.5811388300841898i\n"
     ]
    }
   ],
   "source": [
    "#Quadsolver\n",
    "from math import*\n",
    "\n",
    "def main():\n",
    "    a = float(input(\"What's a? \"))\n",
    "    b = float(input(\"What's b? \"))\n",
    "    c = float(input(\"What's c? \"))\n",
    "    print(solveQuad(a,b,c))\n",
    "\n",
    "def solveQuad(a,b,c):\n",
    "\t#x1 = 0.0\n",
    "\t#x2 = 0.0\n",
    "\td = (b**2 - 4*a*c)\n",
    "\t#2 real solutions\n",
    "\tif d > 0:\n",
    "\t\tx1 = (-b + sqrt(d))/(2*a)\n",
    "\t\tx2 = (-b - sqrt(d))/(2*a)\n",
    "\t\treturn (\"x1 = \" + str(x1) + \"\\nx2 = \" + str(x2))\n",
    "\t#1 real solution\n",
    "\telif d == 0:\n",
    "\t\tx1 = (-b)/(2*a)\n",
    "\t\treturn (\"x1 & x2 = \" + str(x1))\n",
    "\t#2 imaginary solutions\n",
    "\telse:\n",
    "\t\tRe = (-b)/(2*a)\n",
    "\t\tIm = sqrt(-d)/(2*a)\n",
    "\t\treturn (\"x1 = \" + str(Re) + \" + \" + str(Im) + \"i\\nx2 = \" + str(Re) + \" - \" + str(Im) + \"i\")\n",
    "\n",
    "main()\n"
   ]
  }
 ],
 "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.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
