{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python Refresher (do check out __[Python docs](https://docs.python.org/3/reference/)__ )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Outputting\n",
    "### Hello world!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "print() is a function in Python 3 - you need the parentheses.\n",
    "String literals are contained within either single or double quotes.\n",
    "This is handy when you want to output a single quote or vice-versa"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world!\n",
      "hello world!\n",
      "'lo world and all who live there!\n"
     ]
    }
   ],
   "source": [
    "# this is a comment and the next line outputs a string of characters\n",
    "print('hello world!')\n",
    "#so does this\n",
    "print(\"hello world!\")\n",
    "print(\"'lo world and all who live there!\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "Missing parentheses in call to 'print'. Did you mean print(\"hello world\")? (<ipython-input-4-67aebb9513e1>, line 3)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-4-67aebb9513e1>\"\u001b[1;36m, line \u001b[1;32m3\u001b[0m\n\u001b[1;33m    print \"hello world\"\u001b[0m\n\u001b[1;37m                      ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m Missing parentheses in call to 'print'. Did you mean print(\"hello world\")?\n"
     ]
    }
   ],
   "source": [
    "#python 2 allowed no brackets  >> print \"hello world\"\n",
    "#python 3 doesn't.  Run this...\n",
    "print \"hello world\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We'll come back to strings in a minute after we recap on variables. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Variables and Built in Data Types\n",
    "Python is dynamically typed but not statically typed.\n",
    "\n",
    "### Variable Naming Rules:\n",
    "\n",
    "-  A variable can have a short name (like x and y) or a more descriptive name (velocity, partNo).\n",
    "\n",
    "-  A variable name must start with a letter or the underscore character\n",
    "\n",
    "-  A variable name cannot start with a number\n",
    "\n",
    "-  A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )\n",
    "\n",
    "-  Variable names are case-sensitive (age, Age and AGE are three different variables)\n",
    "\n",
    "### Numeric types are defined by assignment:-\n",
    "\n",
    "Integers\n",
    "x=4\n",
    "\n",
    "Floats\n",
    "y=4.0\n",
    "\n",
    "Complex\n",
    "z=4 + 3j\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x is 4 ,y is 4.0 and z is (4+3j)\n",
      "<class 'int'> <class 'float'> <class 'complex'>\n"
     ]
    }
   ],
   "source": [
    "#Integers\n",
    "x=4\n",
    "#Floats\n",
    "y=4.0\n",
    "#Complex\n",
    "z=4 + 3j\n",
    "print(\"x is\",x,\",y is\",y,\"and z is\",z)\n",
    "print(type(x),type(y),type(z))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "To find a variable's type call its type method (these built in types are actually classes!)\n",
    "### Boolean Values\n",
    "\n",
    "These always evaluate to True or False\n",
    "empty values, such as (), [], {}, \"\", the number 0, and the value None."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False True False False\n"
     ]
    }
   ],
   "source": [
    "a=''\n",
    "x=True\n",
    "y=False\n",
    "z=0\n",
    "print(bool(a),bool(x),bool(y),bool(z))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Summary of Built-in Data Types\n",
    "\n",
    "\n",
    "Python has the following data types built-in by default, in these categories:\n",
    "\n",
    "Text Type:\tstr\n",
    "\n",
    "Numeric Types:\tint, float, complex\n",
    "\n",
    "Sequence Types:\tlist, tuple, range\n",
    "\n",
    "Mapping Type:\tdict\n",
    "\n",
    "Set Types:\tset, frozenset\n",
    "\n",
    "Boolean Type:\tbool\n",
    "\n",
    "Binary Types:\tbytes, bytearray, memoryview"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can caste variables using syntax similar to C, etc."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3 <class 'int'>\n",
      "3.0 <class 'float'>\n",
      "3.0 <class 'complex'>\n"
     ]
    }
   ],
   "source": [
    "x=3\n",
    "print(x,type(x))\n",
    "y=float(x)\n",
    "print(y,type(y))\n",
    "z=complex(x)\n",
    "print(y,type(z))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Common Arithmetic and Logical Operators in Python\n",
    "\n",
    "\n",
    "Python recognises the following groups of operators\n",
    "\n",
    "### Arithmetic operators\n",
    "\n",
    " #### Assignment\n",
    " x = 5\n",
    " #### Increment by and assign\n",
    " x += 3\tequivalent to x = x + 3\t\n",
    " #### Decrement by  and assign\n",
    " x -= 3 equivalent to x = x - 3\t\n",
    " #### Multiply by and assign\n",
    " x *= 3 equivalent to x = x * 3\t\n",
    " #### Divide by and assign\n",
    " x /= 3 equivalent to x = x / 3\t\n",
    " #### Modular division (remainder)  and assign\n",
    " x %= 3 equivalent to x = x % 3\t\n",
    " #### Integer division and assign\n",
    " x //= 3 equivalent to x = x // 3\n",
    " #### Power and assign\n",
    " x \\*\\**= 3 equivalent to x = x \\*\\** 3\t\n",
    " #### Bitwise And and assign\n",
    " x &= 3 equivalent to x = x & 3\t\n",
    " #### Bitwise Or and assign\n",
    " x |= 3 equivalent to x = x | 3\t\n",
    " #### Bitwise ExOR and assign\n",
    " x ^= 3\tequivalent to x = x ^ 3\t\n",
    " #### Bitwise right shift and assign\n",
    " x >>= 3\tequivalent to x = x >> 3\t\n",
    " #### Bitwise left shift and assign\n",
    " x <<= 3\tequivalent to x = x << 3\n",
    " \n",
    " y = x + 3\t\n",
    " y = x - 3\t\n",
    " y = x * 3\t\n",
    " y = x / 3\t\n",
    " #### Modular division (remainder)\n",
    " y = x % 3\t\n",
    " #### Integer division\n",
    " y = x // 3\n",
    " #### Power\n",
    " y = x ** 3\t\n",
    " #### Bitwise And\n",
    " y = x & 3\t\n",
    " #### Bitwise Or\n",
    " y = x | 3\t\n",
    " #### Bitwise ExOr\n",
    " y= x ^ 3\t\n",
    " #### Bitwise right shift\n",
    " y x >> 3\t\n",
    " #### Bitwise left shift\n",
    " y x << 3\n",
    " #### Bitwise Not\n",
    " y = ~x\n",
    "### Comparison operators\n",
    "Equal                       x == y\t\n",
    "Not equal                   x != y\t\n",
    "Greater than                x > y\t\n",
    "Less than                   x < y\t\n",
    "Greater than or equal to\tx >= y\t\n",
    "Less than or equal to\t    x <= y\n",
    "\n",
    "#### Logical operators\n",
    "and returns True if both statements are true, eg \ta < 4 and  z==6\n",
    "and returns True if either statements are true, eg \ta < 4 or  z==6\n",
    "not\treturns False if the result is true and vice versa\n",
    "#### Identity operators\n",
    "Identity operators are used to compare the objects, not if they are equal, but if they are actually the same object, with the same memory location:\n",
    "is \treturns True if both variables are the same object,\tx is y\t\n",
    "is not\treturns True if both variables are not the same object,\tx is not y\n",
    "### Membership operators\n",
    "in \t:returns True if a sequence with the specified value is present in the object,\tx in y\t\n",
    "not in\t:returns True if a sequence with the specified value is not present in the object,x not in y\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n",
      "False\n",
      "0\n",
      "8\n"
     ]
    }
   ],
   "source": [
    "\n",
    "print(65 > 64)\n",
    "print(3 == 4)\n",
    "print(13 < 2)\n",
    "print(10&5)\n",
    "print(2**3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Returning to strings\n",
    "\n",
    "Python allows the % symbol - similar to C style formatting, but this can be difficult to read, remember C?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Hello, Gordon. You are 21.'"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name = \"Gordon\"\n",
    ">>> age = 21\n",
    ">>> \"Hello, %s. You are %s.\" % (name, age)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Python 3.6+ has formatted string literals! F-string is a string literal that is prefixed with 'f' or 'F'. These strings may contain replacement fields, which are expressions delimited by curly braces {}. While other string literals always have a constant value, formatted strings are really expressions evaluated at run time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello Elen, you are 10 years old!\n"
     ]
    }
   ],
   "source": [
    "x=4+6\n",
    "you=\"Elen\"\n",
    "print(f'hello {you}, you are {x} years old!')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## If Statement\n",
    "Note the colon and the use of indentation instead of braces or begin/end pairs.  Python uses indentation as part of its syntax and it defines scope.  Be careful."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "It's true b is greater than a!\n",
      "hi\n"
     ]
    }
   ],
   "source": [
    "a=5\n",
    "b=7.2\n",
    "if b>a:\n",
    "    print(\"It's true b is greater than a!\")\n",
    "    print(\"hi\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### elif and else\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "It's true b is greater than a!\n"
     ]
    }
   ],
   "source": [
    "a=3\n",
    "b=5\n",
    "if b>a:\n",
    "    print(\"It's true b is greater than a!\")\n",
    "elif  a==b:\n",
    "    print(\"a is greater than 4\")\n",
    "else:\n",
    "    print(\"a must be greater than b\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "for x in range(6):\n",
    "  print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['orange', 'kiwi', 'melon']\n"
     ]
    }
   ],
   "source": [
    "thislist = [\"apple\", \"banana\", \"cherry\", \"orange\", \"kiwi\", \"melon\", \"mango\"]\n",
    "print(thislist[-4:-1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Add some more cells that describe while loop syntax with examples.\n",
    "Also describe Sequence Types: list, tuple, range Mapping Type: dict.\n",
    "Also write up the use of \"y=input()\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "When you've done this: write a piece of code that:- converts from degrees Fahrenheit to degrees C and vice versa.\n",
    "\n",
    "Code up FizzBuzz game (o/p Fizz if number is divisible by 3, buzz if 5 and fizzbuzz if divisable by both. Use for loop and range statement."
   ]
  }
 ],
 "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.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
