{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Python Slicing: Strings and Lists"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "student_name = \"Jobs,Bill\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Jobs,\n"
     ]
    }
   ],
   "source": [
    "print(student_name[0:-4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "students=[\"Wiley,Veronica\",\"Milton,Tahj\",\"Peng,Xi\",\"Gates,Steve\",\"Jobs,Bill\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Wiley,Veronica\n"
     ]
    }
   ],
   "source": [
    "print(students[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Jobs,Bill\n"
     ]
    }
   ],
   "source": [
    "print(students[-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "students[-1]=\"Jobs,Bob\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Wiley,Veronica', 'Milton,Tahj', 'Peng,Xi', 'Gates,Steve', 'Jobs, Bob']\n"
     ]
    }
   ],
   "source": [
    "print(students)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Wiley,Veronica', 'Peng,Xi', 'Gates,Steve', 'Jobs, Bob']\n"
     ]
    }
   ],
   "source": [
    "del students[1]\n",
    "print(students)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "student_marks = [56,78,90,34,23,56,67,89]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[23, 34, 56, 56, 67, 78, 89, 90]\n"
     ]
    }
   ],
   "source": [
    "sorted_marks=sorted(student_marks)\n",
    "print(sorted_marks)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "first n elements"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[23, 34, 56]\n"
     ]
    }
   ],
   "source": [
    "print(sorted_marks[:3])#bottom 3 students"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "last n elements"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[78, 89, 90]\n"
     ]
    }
   ],
   "source": [
    "print(sorted_marks[-3:])#top 3 students"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "all but the last n elements"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[23, 34, 56, 56, 67]\n"
     ]
    }
   ],
   "source": [
    "print(sorted_marks[:-3])#excluding the top 3 students"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "All but the first n elements"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[56, 67, 78, 89, 90]\n"
     ]
    }
   ],
   "source": [
    "print(sorted_marks[3:])#excluding first 3 marks"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Every nth element"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[34, 56, 78, 90]\n"
     ]
    }
   ],
   "source": [
    "print(sorted_marks[::2])#get every second mark, even"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[34, 56, 78, 90]\n"
     ]
    }
   ],
   "source": [
    "print(sorted_marks[1::2])#get evry second mark, odd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Reversing a list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[90, 89, 78, 67, 56, 56, 34, 23]\n"
     ]
    }
   ],
   "source": [
    "print(sorted_marks[::-1])#"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "fruit=\"banana\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ananab\n"
     ]
    }
   ],
   "source": [
    "print(fruit[::-1])#"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Making a copy of a list using slicing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[23, 34, 56, 56, 67, 78, 89, 90]\n"
     ]
    }
   ],
   "source": [
    "copy_marks=sorted_marks[:]#duplicate the list\n",
    "print(copy_marks)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Merge two lists"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "students1=[\"Wiley,Veronica\",\"Milton,Tahj\"]\n",
    "students2= [\"Peng,Xi\",\"Gates,Steve\",\"Jobs,Bill\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Peng,Xi', 'Gates,Steve', 'Jobs,Bill', 'Wiley,Veronica', 'Milton,Tahj']\n"
     ]
    }
   ],
   "source": [
    "#pretend there is an invisible index position just after 0, and put the second list in that position\n",
    "students1[0:0]=students2\n",
    "print(students1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Write a program to generate student usernames"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [],
   "source": [
    "student_names_DOB = {'Peng,Xi':1997, 'Gates,Steve':1986, 'Jobs,Bill':1988, 'Wiley,Veronica':2001, 'Milton,Tahj':1999}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Pen97\n",
      "Gat86\n",
      "Job88\n",
      "Wil01\n",
      "Mil99\n"
     ]
    }
   ],
   "source": [
    "for each_student in student_names_DOB:\n",
    "    letters = each_student[:3]\n",
    "    year = student_names_DOB[each_student]\n",
    "    numbers = str(year)[-2:]\n",
    "    username = letters+numbers\n",
    "    print(username)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Pen97\n",
      "Gat86\n",
      "Job88\n",
      "Wil01\n",
      "Mil99\n"
     ]
    }
   ],
   "source": [
    "for each_student in student_names_DOB:\n",
    "    print(each_student[:3]+str(student_names_DOB[each_student])[-2:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [],
   "source": [
    "list_of_grade_types = [\"Fail\",\"Pass\",\"Credit\",\"Distinction\",\"High Distinction\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Write a program to generate acronyms for each of the above grade descriptions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['F', 'P', 'C', 'D', 'HD']\n"
     ]
    }
   ],
   "source": [
    "acronyms=[]\n",
    "for grade in list_of_grade_types:\n",
    "    split_word=grade.split(\" \")\n",
    "    acronym=\"\"\n",
    "    for each_word in split_word:\n",
    "        acronym+=each_word[:1]\n",
    "    acronyms.append(acronym)\n",
    "print(acronyms)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "bob is a Palindrome!\n",
      "madam is a Palindrome!\n",
      "poop is a Palindrome!\n"
     ]
    }
   ],
   "source": [
    "#Palindrome checker\n",
    "words = [\"bob\", \"millie\", \"madam\", \"Mum\", \"poop\"]\n",
    "\n",
    "for each_word in words:\n",
    "    if each_word[::1]==each_word[::-1]:\n",
    "        print(\"{} is a Palindrome!\".format(each_word))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### zip"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Student: Bob Jones   |   Grade: 56%\n",
      "Student: Jane Smith   |   Grade: 87%\n",
      "Student: Lil Dwayne   |   Grade: 99%\n",
      "Student: Ahmed Karim   |   Grade: 5%\n"
     ]
    }
   ],
   "source": [
    "students = [\"Bob Jones\", \"Jane Smith\",\"Lil Dwayne\", \"Ahmed Karim\"]\n",
    "grades = [56, 87, 99, 5]\n",
    "\n",
    "for student, grade in zip(students, grades):\n",
    "    print(\"Student: {}   |   Grade: {}%\".format(student, grade))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### enumerate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'Lil Dwayne': 99, 'Jane Smith': 87, 'Bob Jones': 56, 'Ahmed Karim': 5}\n"
     ]
    }
   ],
   "source": [
    "students_names_only = [\"Bob Jones\", \"Jane Smith\",\"Lil Dwayne\", \"Ahmed Karim\"]\n",
    "grades = [56, 87, 99, 5]\n",
    "students_to_grades = dict()\n",
    "\n",
    "#later we'll write a function def get_student_ranking()\n",
    "while len(grades)>0:\n",
    "    for i, grade in enumerate(grades):\n",
    "        if grade is max(grades):\n",
    "            student_name = students_names_only[i]\n",
    "            student_dict = {student_name: grade}\n",
    "            students_to_grades.update(student_dict)\n",
    "            del students_names_only[i]\n",
    "            del grades[i]\n",
    "print(students_to_grades)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]\n"
     ]
    }
   ],
   "source": [
    "list_values = [1,2,3,4,5,6,7,8,9,10]\n",
    "double_values = [2*a for a in list_values]\n",
    "print(double_values)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[61, 92, 99, 10]\n"
     ]
    }
   ],
   "source": [
    "students_names_only = [\"Bob Jones\", \"Jane Smith\",\"Lil Dwayne\", \"Ahmed Karim\"]\n",
    "grades = [56, 87, 99, 5]\n",
    "\n",
    "new_grades = [grade+5 if grade<95 else grade for grade in grades]\n",
    "print(new_grade)"
   ]
  }
 ],
 "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
}
