{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Topic 4: Starting Out with Python 2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This notebook covers control structures, including if statements, for loops and while loops, as well as continue and break"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### if elif else \n",
    "* If statements allow you to execute code if a given condition is True.\n",
    "* If statements are booleans. They are always True or False.\n",
    "* If the condition is True, the indented code will execute. If it is False, the indented code will be skipped."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "student_grade=80\n",
    "\n",
    "if student_grade<50:\n",
    "    print(\"Student has failed the task\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* elif statements allow you to test for multiple conditions.\n",
    "* else statements allow you to execute code when none of your conditions are satisfied.\n",
    "* else can be used with both if and elif conditions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Student has achieved a grade of Pass or higher\n"
     ]
    }
   ],
   "source": [
    "if student_grade<50:\n",
    "    print(\"Student has failed the task\")\n",
    "else:\n",
    "    print(\"Student has achieved a grade of Pass or higher\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Student has failed the task\n"
     ]
    }
   ],
   "source": [
    "plagiarism = True\n",
    "\n",
    "if student_grade<50 or plagiarism == True:\n",
    "    print(\"Student has failed the task\")\n",
    "else:\n",
    "    print(\"Student has achieved a grade of Pass or higher\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Bob achieved a Fail\n"
     ]
    }
   ],
   "source": [
    "#variable scope, if performance_band is declared inside \n",
    "#the if statements this variable scope exists only within that block of code.\n",
    "\n",
    "performance_band = \"\"\n",
    "\n",
    "if student_grade<50 or plagiarism==True:\n",
    "    performance_band = \"Fail\"\n",
    "elif student_grade<65 and student_grade>=50:\n",
    "    performance_band = \"Pass\"\n",
    "elif student_grade<75 and student_grade>=65:\n",
    "    performance_band = \"Credit\"\n",
    "elif student_grade<85 and student_grade>=75:\n",
    "    performance_band = \"Distinction\"\n",
    "else:\n",
    "    performance_band = \"High Distinction\"\n",
    "print(\"Bob achieved a\",performance_band)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Repetition"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This section will cover for loops and while loops"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### for loops\n",
    "\n",
    "* for loops iterate over each element in a collection\n",
    "* for loops allow you to perform operations on each element in a collection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Azam,Asif\n",
      "Bentley,Sara\n",
      "King,James\n",
      "Li,Fi Fi\n"
     ]
    }
   ],
   "source": [
    "#student_names_grades is a dictionary.\n",
    "#student name is the key, student score is the value\n",
    "student_names_grades = {'Azam,Asif': 34.5, 'Bentley,Sara': 90.3, 'King,James': 53.5, 'Li,Fi Fi': 77.9}\n",
    "\n",
    "for each_student in student_names_grades:\n",
    "    print(each_student)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Azam,Asif 34.5\n",
      "Bentley,Sara 90.3\n",
      "King,James 53.5\n",
      "Li,Fi Fi 77.9\n"
     ]
    }
   ],
   "source": [
    "#show key and value\n",
    "for each_student in student_names_grades:\n",
    "    grade = student_names_grades.get(each_student)\n",
    "    print(each_student, grade)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Asif Azam 34\n",
      "Sara Bentley 90\n",
      "James King 54\n",
      "Fi Fi Li 78\n"
     ]
    }
   ],
   "source": [
    "#split on \",\" and grap firstname and surname\n",
    "for each_student in student_names_grades:\n",
    "    student_surname = each_student.split(\",\")[0]\n",
    "    student_first_name = each_student.split(\",\")[1]\n",
    "    grade = student_names_grades.get(each_student)\n",
    "    rounded_grade = round(grade)\n",
    "    print(student_first_name,student_surname, rounded_grade)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### for loops with if statements"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Asif Azam Fail 34\n",
      "Sara Bentley High Distinction 90\n",
      "James King Pass 54\n",
      "Fi Fi Li Distinction 78\n"
     ]
    }
   ],
   "source": [
    "\n",
    "for each_student in student_names_grades:\n",
    "    student_surname = each_student.split(\",\")[0]\n",
    "    student_first_name = each_student.split(\",\")[1]\n",
    "    student_grade = student_names_grades.get(each_student)\n",
    "    rounded_grade = round(student_grade)\n",
    "    performance_band = \"\"\n",
    "    if student_grade<50:\n",
    "        performance_band = \"Fail\"\n",
    "    elif student_grade<65 and student_grade>=50:\n",
    "        performance_band = \"Pass\"\n",
    "    elif student_grade<75 and student_grade>=65:\n",
    "        performance_band = \"Credit\"\n",
    "    elif student_grade<85 and student_grade>=75:\n",
    "        performance_band = \"Distinction\"\n",
    "    else:\n",
    "        performance_band = \"High Distinction\"\n",
    "    print(student_first_name,student_surname, performance_band, rounded_grade)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Bentley,Sara']\n"
     ]
    }
   ],
   "source": [
    "#add items from dictionary to a list\n",
    "high_achievers = []\n",
    "student_names_grades = {'Azam,Asif': 34.5, 'Bentley,Sara': 90.3, 'King,James': 53.5, 'Li,Fi Fi': 77.9}\n",
    "for each_student in student_names_grades:\n",
    "    if student_names_grades.get(each_student)>85:\n",
    "        high_achievers.append(each_student)\n",
    "print(high_achievers)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### len(collection) - a  very useful function!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "students_who_plagarised=[\"Wiley,Veronica\",\"Milton,Tahj\",\"Peng,Xi\",\"Gates,Steve\",\"Jobs,Bill\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Number of students who plagiarised: 5\n"
     ]
    }
   ],
   "source": [
    "print(\"Number of students who plagiarised:\",len(students_who_plagarised))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### while loops (and random number generator)\n",
    "* while loops allow you to perform one or more operations for as long as a condition is True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Felicity', 'Xi', 'Felicity']\n"
     ]
    }
   ],
   "source": [
    "#randomly select 3 students from  list called students\n",
    "import random\n",
    "\n",
    "students = [\"Bob\", \"Jane\", \"Ahmed\", \"Felicity\", \"Adrian\",\"Xi\",\"Melanie\", \"Juan\"]\n",
    "\n",
    "student_group = []\n",
    "\n",
    "while len(student_group)<3:\n",
    "    student = random.randint(0, len(students)-1)\n",
    "    student_group.append(students[student])\n",
    "    \n",
    "print(student_group)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'Juan', 'Melanie', 'Xi'}\n"
     ]
    }
   ],
   "source": [
    "#show no repeats, select 3 random students from the students list.\n",
    "import random\n",
    "\n",
    "students = [\"Bob\", \"Jane\", \"Ahmed\", \"Felicity\", \"Adrian\",\"Xi\",\"Melanie\", \"Juan\"]\n",
    "\n",
    "student_group_1 = set()\n",
    "\n",
    "while len(student_group_1)<3:\n",
    "    student = random.randint(0, len(students)-1)\n",
    "    student_group_1.add(students[student])\n",
    "print(student_group_1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### continue\n",
    "* The continue keyword allows you to skip performing operations on a specific element in a collection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'Bob', 'Adrian', 'Felicity'}\n"
     ]
    }
   ],
   "source": [
    "student_group_2 = set()\n",
    "\n",
    "while len(student_group_2)< 3:\n",
    "    student = random.randint(0, len(students)-1)\n",
    "    if students[student] in student_group_1:\n",
    "        continue\n",
    "    student_group_2.add(students[student])\n",
    "print(student_group_2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### break\n",
    "* break statements allow you to exit a for or while loop prematurely\n",
    "* break statements are useful in preventing endless loops"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'Jane', 'Ahmed'}\n"
     ]
    }
   ],
   "source": [
    "#add 3 students to group 3\n",
    "student_group_3 = set()\n",
    "\n",
    "while len(student_group_3)< 3:\n",
    "    if len(student_group_1)+len(student_group_2)+len(student_group_3)==len(students):\n",
    "        break\n",
    "    student = random.randint(0, len(students)-1)   \n",
    "    if students[student] in student_group_1 or students[student] in student_group_2:\n",
    "        continue\n",
    "    student_group_3.add(students[student])  \n",
    "print(student_group_3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
