The Algorithms logo
The Algorithms
À proposFaire un don

Python Basics

H
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python Basic\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "what is name ?mosh\n",
      "HI mosh\n"
     ]
    }
   ],
   "source": [
    "name = input(\"what is name ?\")\n",
    "print('HI ' +name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "what is your birthdate ? 2000\n",
      "19\n"
     ]
    }
   ],
   "source": [
    "name = input('what is your birthyear ? ')\n",
    "year = 2019 -int(name);\n",
    "print(year)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Type"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'str'>\n"
     ]
    }
   ],
   "source": [
    "#type \n",
    "\n",
    "print(type(name))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "what is weight 45\n",
      "20.25\n"
     ]
    }
   ],
   "source": [
    "weight = input(\"what is weight \")\n",
    "final = int(weight)*0.45\n",
    "print(final)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "you're awesome\n",
      "im \"Hritik\"\n",
      "\n",
      "        Hii buddy \n",
      "\n",
      "        im Hritik \n",
      "\n",
      "        you look awesome \n",
      "\n",
      "        thank you,\n",
      " \n"
     ]
    }
   ],
   "source": [
    "#if want to use this type statement -> year's then we have to use double quotes \"\"\n",
    "string = \"you're awesome\"\n",
    "print(string)\n",
    "\n",
    "# if want to assign \"important msg\" like this we have to use single quote  ''\n",
    "str = 'im \"Hritik\"'\n",
    "print(str)\n",
    "\n",
    "# for multitline string ''' % ''' for sending mail \n",
    "\n",
    "mail = '''\n",
    "        Hii buddy \n",
    "\n",
    "        im Hritik \n",
    "\n",
    "        you look awesome \n",
    "\n",
    "        thank you,\n",
    " '''\n",
    "\n",
    "print(mail)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "p\n",
      "r\n",
      "python beginners\n",
      "python beginners\n",
      "pytho\n",
      "pytho\n",
      "yt\n",
      "ython beginner\n"
     ]
    }
   ],
   "source": [
    "course = 'python beginners'\n",
    "print(course[0])\n",
    "print(course[-2])\n",
    "print(course[0:])\n",
    "print(course[:])\n",
    "print(course[:5])\n",
    "print(course[0:5])\n",
    "print(course[1:3]) #exclude 3 only takes 1,2\n",
    "\n",
    "print(course[1:-1]) # exclude -1 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Inbuilt - Function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9\n"
     ]
    }
   ],
   "source": [
    "#length of string including space\n",
    "c = 'hii buddy'\n",
    "print(len(c))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "python is very imp lang\n",
      "PYTHON IS VERY IMP LANG\n",
      "2\n",
      "2\n",
      "Python Is Very Imp Lang\n",
      "python are very imp lang\n"
     ]
    }
   ],
   "source": [
    "#various function \n",
    "msg = 'python is very imp lang'\n",
    "print(msg.lower())\n",
    "print(msg.upper())\n",
    "print(msg.count('i'))\n",
    "print(msg.find('t'))\n",
    "print(msg.title())\n",
    "print(msg.replace('is','are'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "22\n"
     ]
    }
   ],
   "source": [
    "#ordering \n",
    "# exp > (mul or div) > (add or sub)\n",
    "exp = 10 + 3*2**2\n",
    "print(exp)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Math function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n",
      "8\n"
     ]
    }
   ],
   "source": [
    "#Math function\n",
    "# 1) round\n",
    "x = 6.5\n",
    "y = 7.66\n",
    "print(round(x))\n",
    "print(round(y))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.5\n"
     ]
    }
   ],
   "source": [
    "# 2) abs -> return positive no.\n",
    "x = -3.5\n",
    "print(abs(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "# 3) Mathametical function\n",
    "import math\n",
    "pi = 3.142\n",
    "math.cos((60*pi)/180)\n",
    "\n",
    "# ceil and floor\n",
    "print(math.ceil(2.9))\n",
    "print(math.floor(2.9))\n",
    "\n",
    "#thier are many more maths module"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# If - Statement"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "nothing\n",
      "SUM OF X AND Y : 15\n"
     ]
    }
   ],
   "source": [
    "#if statement\n",
    "\n",
    "x = 5\n",
    "y = 10\n",
    "\n",
    "if (x>y):\n",
    "    print(\"truee\")\n",
    "elif (x==y):\n",
    "    print(\"falsee\")\n",
    "else:\n",
    "    print(\"nothing\")\n",
    "    \n",
    "print(f\"SUM OF X AND Y : {x+y}\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Format"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = \"Hii\"\n",
    "y = 'Hritik {}'\n",
    "z = 'Jaiswal'\n",
    "print(f\"sum of string is x + y : {x+y}\")\n",
    "print(y.format(z))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "# we can use 'and' & 'or' in if statement\n",
    "a=2 \n",
    "b=3\n",
    "c=4\n",
    "if (a<b) and (c>b):\n",
    "    print('True')\n",
    "elif (a==b) or (c>a):\n",
    "    print('Truee')\n",
    "elif (b>a) and not(b>c):\n",
    "    print('Trueee')\n",
    "else:\n",
    "    print('false')\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Weight :45\n",
      "(L)bs or (K)gv\n",
      "100.0\n"
     ]
    }
   ],
   "source": [
    "#Exercise\n",
    "\n",
    "weight = int(input('Weight :'))\n",
    "unit = input('(L)bs or (K)g')\n",
    "if unit.lower()=='l' or unit.upper()=='L':\n",
    "    c = weight*0.45;\n",
    "else:\n",
    "    c = weight/0.45;\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# While loop"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "*\n",
      "**\n",
      "***\n",
      "****\n"
     ]
    }
   ],
   "source": [
    "#while\n",
    "\n",
    "i=1\n",
    "while (i<5):\n",
    "    print('*'*i)\n",
    "    i+=1\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# for loop"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "T\n",
      "e\n",
      "a\n",
      "c\n",
      "h\n",
      "e\n",
      "r\n"
     ]
    }
   ],
   "source": [
    "#for loop\n",
    "for item in 'Teacher':\n",
    "    print(item)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hii\n",
      "buddy \n",
      "whats\n",
      "up\n",
      "-----------------\n",
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "-----------------\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "-----------------\n",
      "2\n",
      "5\n",
      "-----------------\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n"
     ]
    }
   ],
   "source": [
    "for i in ['hii','buddy ','whats','up']:\n",
    "    print(i)\n",
    "print('-----------------')\n",
    "for i in range(4):\n",
    "    print(i)\n",
    "print('-----------------')\n",
    "for i in range(2,8):\n",
    "    print(i)\n",
    "print('-----------------')\n",
    "for i in range(2,8,3):\n",
    "    print(i)\n",
    "print('-----------------')\n",
    "for i in [5,6,7,8]:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "14\n",
      "-----------\n",
      "14\n"
     ]
    }
   ],
   "source": [
    "r = [2,3,4,5]\n",
    "total = 0\n",
    "for i in r:\n",
    "    total+=i\n",
    "print(total)\n",
    "print('-----------')\n",
    "r = [2,3,4,5]\n",
    "e = sum(r)\n",
    "print(e)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(0,0)\n",
      "(0,1)\n",
      "(0,2)\n",
      "(1,0)\n",
      "(1,1)\n",
      "(1,2)\n",
      "(2,0)\n",
      "(2,1)\n",
      "(2,2)\n",
      "(3,0)\n",
      "(3,1)\n",
      "(3,2)\n"
     ]
    }
   ],
   "source": [
    "for i in range(4):\n",
    "    for j in range(3):\n",
    "        print(f'({i},{j})')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "$\n",
      "$$\n",
      "$$$\n",
      "$$$$\n",
      "$$$$$\n"
     ]
    }
   ],
   "source": [
    "#array\n",
    "num = [1,2,3,4,5]\n",
    "for i in num:\n",
    "    print('$'*i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ab \n",
      "ae\n"
     ]
    }
   ],
   "source": [
    "name = ['ab ','ac','ad','ae']\n",
    "print(name[0])\n",
    "print(name[-1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# List"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 146,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3]\n",
      "[4, 5, 6]\n",
      "[7, 8, 9]\n",
      "-----print individual item-----\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "#2D-List \n",
    "matrix = [[1,2,3],[4,5,6],[7,8,9]]\n",
    "for i in matrix:\n",
    "    print(i)\n",
    "print('-----print individual item-----')\n",
    "for i in matrix:\n",
    "    for j in i:\n",
    "        print(j)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Inbuilt - Function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 158,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-----Append---------\n",
      "[2, 3, 4, 6, 9]\n",
      "-----Insert---------\n",
      "[2, 3, 8, 4, 6, 9]\n",
      "-----Remove---------\n",
      "[2, 3, 4, 6, 9]\n",
      "-----pop---------\n",
      "[2, 3, 4, 6]\n",
      "-----clear---------\n",
      "[]\n"
     ]
    }
   ],
   "source": [
    "#working with function \n",
    "# 1.append 2.insert 3.remove 4.pop 5.clear\n",
    "number = [2,3,4,6]\n",
    "print('-----Append---------')\n",
    "number.append(9)\n",
    "print(number)\n",
    "print('-----Insert---------')\n",
    "number.insert(2,8)\n",
    "print(number)\n",
    "print('-----Remove---------')\n",
    "number.remove(8)\n",
    "print(number)\n",
    "print('-----pop---------')\n",
    "number.pop()\n",
    "print(number)\n",
    "print('-----clear---------')\n",
    "number.clear()\n",
    "print(number)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 162,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "#index \n",
    "list_item = [2,4,6,7,2,4]\n",
    "print(list_item.index(6))\n",
    "print(list_item.count(2))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 164,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 5, 8]\n",
      "[8, 5, 3, 2, 1]\n"
     ]
    }
   ],
   "source": [
    "# 1.sort 2.reverse \n",
    "item = [3,5,2,8,1]\n",
    "item.sort()\n",
    "print(item)\n",
    "item.reverse()\n",
    "print(item)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 3, 4, 5]\n"
     ]
    }
   ],
   "source": [
    "#list will update if we changing list item before calling copy function ,\n",
    "# but list will not be change when u are appending and deleting after copy function\n",
    "a = [2,3,4,5]\n",
    "b = a.copy()\n",
    "a.append(10)\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 3, 4, 5]\n"
     ]
    }
   ],
   "source": [
    "#excercise -> remove a duplicate no. from the list\n",
    "numbers = [2,3,4,4,3,5]\n",
    "unique = []\n",
    "\n",
    "for i in numbers:\n",
    "    if i not in unique:\n",
    "        unique.append(i)\n",
    "        \n",
    "print(unique)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# tuple"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    }
   ],
   "source": [
    "#tuple -> we can not append , remove , pop , insert , u can not modify your list .\n",
    "#only thing u can do -> count , index a item in list\n",
    "\n",
    "## The main diffrence between tuple and list is :- tuple is immutable and list is mutable\n",
    "## that mean we can not change tuple value by modifying it but in list we can do that.\n",
    "number = (1,2,3)\n",
    "print(number[0])\n",
    "\n",
    "# this will throw error :\n",
    "# number[0] = 5\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Set"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 3}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "{1,2,3}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 4, 5}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# display unique value \n",
    "{1,1,4,2,2,5}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 4, 8}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set([1,2,4,4,8,8,4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "# add value to the set\n",
    "s = {1,2,3}\n",
    "s.add(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 3, 5}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "def times(var):\n",
    "    return var*2;\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "times(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 4, 6]"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "seq = [1,2,3]\n",
    "\n",
    "list(map(times,seq))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Lambda "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "# instead of writing like this we can write \n",
    "# def times(var):\n",
    "#     return var*2;\n",
    "\n",
    "t = lambda var : var*2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 4, 6]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(map(t,seq))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Filter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[False, True, False]"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(map(lambda num : num %2 ==0,seq))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2]"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(filter(lambda num : num %2 ==0,seq))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Unpacking"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "#unpacking\n",
    "coordinates = [2,3,4]\n",
    "# x = coordinates[0]\n",
    "# y = coordinates[1]\n",
    "# z = coordinates[2]\n",
    "# instead of writing like this we can write \n",
    "x,y,z = coordinates  #unpacking -> work with both list and tuple\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Dictionaries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hritik Jaiswal\n",
      "may 31 2000\n",
      "None\n",
      "HritiK Dinesh Jaiswal\n"
     ]
    }
   ],
   "source": [
    "#Dictionaries -> contain key - value pairs \n",
    "\n",
    "people = {\n",
    "    \"name\" :\"Hritik Jaiswal\",\n",
    "    \"age\" : 19,\n",
    "    \"is_male\" : True\n",
    "}\n",
    "print(people[\"name\"])\n",
    "\n",
    "# u can not write like -> print(people[\"Name\"])\n",
    "# we can use GET method to take key-value pair if its not present and display key value pair \n",
    "print(people.get(\"birth\",\"may 31 2000\"))\n",
    "print(people.get(\"gender\"))   #None -> is the object that represent absence of the value\n",
    "\n",
    "people[\"name\"] = \"HritiK Dinesh Jaiswal\"  # modify name\n",
    "print(people.get(\"name\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "phone : 105\n",
      "1\n",
      "0\n",
      "5\n",
      "One zero Five \n"
     ]
    }
   ],
   "source": [
    "#excersice -> inuput 1234 display output : one two three four\n",
    "phone =  input(\"phone : \")\n",
    "\n",
    "dict  = {\n",
    "    \"1\" :\"One\",\n",
    "    \"2\":\"Two\",\n",
    "    \"3\" :\"Three\",\n",
    "    \"4\" :\"Four\",\n",
    "    \"5\" :\"Five\",\n",
    "    \"6\" :\"Six\",\n",
    "    \"7\" :\"Seven\",\n",
    "    \"8\" :\"Eight\",\n",
    "    \"9\" :\"Nine\",\n",
    "    \"0\" :\"zero\",\n",
    "}\n",
    "output = \"\" \n",
    "for i in phone:\n",
    "    \n",
    "    output += (dict.get(i,\"?\")) + \" \" \n",
    "    print(i)\n",
    "print(output) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter the word : one seven two\n",
      "['one', 'seven', 'two']\n",
      "1\n",
      "7\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "#excerisce -> opposite \n",
    "words = [\"one\", \"two\",\"three\",\"four\",\"five\",\"six\", \"seven\",\"eight\",\"nine\",\"zero\"]\n",
    "\n",
    "word = input(\"Enter the word : \")\n",
    "mapping  = {\n",
    "    \"one\":\"1\", \n",
    "    \"two\":\"2\",\n",
    "    \"three\":\"3\",\n",
    "    \"four\":\"4\",\n",
    "    \"five\":\"5\",\n",
    "    \"six\":\"6\", \n",
    "    \"seven\":\"7\",\n",
    "    \"eight\":\"8\",\n",
    "    \"nine\":\"9\",\n",
    "    \"zero\":\"0\"\n",
    "}\n",
    "spliting = word.split()\n",
    "print(spliting)\n",
    "for i in spliting:\n",
    "    print(mapping.get(i,\"&\"))\n",
    "    \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      ">good morning :)\n",
      "good morning 😄 \n"
     ]
    }
   ],
   "source": [
    "#excersice -> print emoji\n",
    "message = input(\">\")\n",
    "words= message.split()\n",
    "emoji = {\n",
    "    \":)\" :\"😄\",\n",
    "    \":(\" :\"😔\"\n",
    "}\n",
    "output = \"\"\n",
    "for i in words:\n",
    "    output += emoji.get(i,i) + \" \"\n",
    "    \n",
    "print(output)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Function "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hii\n",
      "im hritik\n"
     ]
    }
   ],
   "source": [
    "#function\n",
    "def text():\n",
    "    print(\"im hritik\")\n",
    "print(\"hii\")\n",
    "text()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Parameter and Arguments"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "thanks\n",
      "Hii hritik and jaiswal\n"
     ]
    }
   ],
   "source": [
    "#function with parameter\n",
    "#parameter ->is placeholder that we passed to the function\n",
    "#argumetnt -> is actual value that u gone pass inside function\n",
    "def text(f_name, l_name):\n",
    "    print(f'Hii {f_name} and {l_name}')\n",
    "print(\"thanks\")\n",
    "text('hritik','jaiswal')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# keyword argument"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello\n",
      "Here total is 5000 including shipping charges 2000 + discount 500\n"
     ]
    }
   ],
   "source": [
    "#keyword argument -> its helpful when u don't want to pass argument in order u can pass in any order \n",
    "def text(discount , shipping ,total ):\n",
    "    print(f'Here total is {total} including shipping charges {shipping} + discount {discount}')\n",
    "print(\"hello\")\n",
    "text(shipping=2000,total=5000,discount=500)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "25\n"
     ]
    }
   ],
   "source": [
    "#return statement \n",
    "def square(num):\n",
    "    return num*num\n",
    "result = square(5)\n",
    "print(result)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "27\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "#tric -> if u don't write return statement then it will return object which is : none \n",
    "def cube(n):\n",
    "    print(n**3)\n",
    "print(cube(3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Exception"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "your age : twenty\n",
      "invalid value\n"
     ]
    }
   ],
   "source": [
    "#exception -> when we try to input string value instead of int \n",
    "# age = int(input(\"your age\"))\n",
    "# print(age)\n",
    "\n",
    "try:\n",
    "    age = int(input(\"your age : \"))\n",
    "    print(age)\n",
    "except ValueError:\n",
    "    print('invalid value')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "your age : 0\n",
      "invalid value or age can't be negative \n"
     ]
    }
   ],
   "source": [
    "\n",
    "try:\n",
    "    age = int(input(\"your age : \"))\n",
    "    income = 20000\n",
    "    risk = float(income/age);\n",
    "    print(f'risk is {risk}')\n",
    "except ValueError and ZeroDivisionError:\n",
    "    print(\"invalid value or age can't be negative \")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Class"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "area\n"
     ]
    }
   ],
   "source": [
    "#class \n",
    "# 1) type -1\n",
    "\n",
    "class rect:\n",
    "    def rect_area(self):\n",
    "        print(\"area\")\n",
    "p = rect()\n",
    "p.rect_area()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hritik Jaiswal\n",
      "Hritik Jaiswal\n"
     ]
    }
   ],
   "source": [
    "# 2) type -2\n",
    "class Employee:\n",
    "    def __init__(self, first, last , salary):\n",
    "        self.first = first\n",
    "        self.last = last\n",
    "        self.salary = salary\n",
    "        self.email = first + '.'+last +'@gmail.com'\n",
    "    \n",
    "    def fullname(self):\n",
    "        return \"{} {}\".format(self.first,self.last)\n",
    "\n",
    "emp1 = Employee('Hritik','Jaiswal',5000)\n",
    "emp2 = Employee('Aniket','Jaiswal',6000)\n",
    "\n",
    "#their are two methods \n",
    "print(emp1.fullname())\n",
    "print(Employee.fullname(emp1))\n",
    "              "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "area of square :9\n",
      "area of rectangle is : 6\n"
     ]
    }
   ],
   "source": [
    "# 3) type -3\n",
    "class Point:\n",
    "    def __init__(self,a,l,h):\n",
    "        self.a = a\n",
    "        self.l = l\n",
    "        self.h = h\n",
    "        \n",
    "    def square(self):\n",
    "        print(f\"area of square :{self.a*self.a}\")\n",
    "    \n",
    "    def rectangle(self):\n",
    "        print(\"area of rectangle is : {}\".format(self.l*self.h))\n",
    "    \n",
    "\n",
    "#create a object \n",
    "point1 = Point(3,2,3)\n",
    "point1.square()\n",
    "point1.rectangle()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Inheritance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "bark\n",
      "walk\n"
     ]
    }
   ],
   "source": [
    "#inheritance -> dog and cat are inherite a class mammel\n",
    "class mammel:\n",
    "    def walk(self):\n",
    "        print(\"walk\")\n",
    "\n",
    "class dog(mammel):\n",
    "    def bark(self):\n",
    "        print(\"bark\")\n",
    "\n",
    "class cat(mammel):\n",
    "    pass\n",
    "\n",
    "dog1 = dog()\n",
    "dog1.bark()\n",
    "\n",
    "cat1 = cat()\n",
    "cat1.walk()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Module\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.0\n"
     ]
    }
   ],
   "source": [
    "#module -> module.ipynb file which we have created we can directly import function also\n",
    "#we need to install from anaconda prompt -> pip install import-ipynb\n",
    "import import_ipynb\n",
    "\n",
    "import module\n",
    "from module import cm2m\n",
    "\n",
    "cm2m(100)\n",
    "m2cm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "numbers = [5,4,6,8,10]\n",
    "print(max(numbers))\n",
    "print(min(numbers))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Packages\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "package -: we can create a seperate .py file and extract this file and import into another file as similar to module \n",
    "\n",
    "package is collections of different modules\n",
    "\n",
    "# Type of Module\n",
    "\n",
    "        1) Absolute module \n",
    "from mypackage.mymodule1 import class A\n",
    "obj = class A\n",
    "\n",
    "        2) relative module\n",
    "if im working in \"module1\" & i want to import Class C from \"module2\" into my \"module1\"\n",
    "\n",
    "from module2 import classC\n",
    "obj = classC()\n",
    "\n",
    "\n",
    "        \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Random\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.8556515733440572\n",
      "0.9018671283206765\n",
      "0.6655666651378818\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "\n",
    "for i in range(3):\n",
    "    print(random.random())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12\n",
      "20\n",
      "15\n"
     ]
    }
   ],
   "source": [
    "for i in range(3):\n",
    "    print(random.randint(10,20))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "jaiswal\n"
     ]
    }
   ],
   "source": [
    "# randomly choose the value \n",
    "members = ['hritik', 'jaiswal','aniket','shweta']\n",
    "show = random.choice(members)\n",
    "print(show)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(2,4)\n"
     ]
    }
   ],
   "source": [
    "#excercise -> dice thrown give random value\n",
    "\n",
    "class Dice:\n",
    "    def roll(self):\n",
    "        x = (1,2,3,4,5,6)\n",
    "        y = (1,2,3,4,5,6)\n",
    "        m = random.choice(x)\n",
    "        l = random.choice(y)\n",
    "        print(\"({},{})\".format(m,l))\n",
    "        \n",
    "\n",
    "r = Dice()\n",
    "r.roll()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(5, 6)\n"
     ]
    }
   ],
   "source": [
    "# another method\n",
    "\n",
    "class Dice:\n",
    "    def roll(self):\n",
    "        first = random.randint(1,6)\n",
    "        second = random.randint(1,6)\n",
    "        return first,second\n",
    "dice = Dice()\n",
    "print(dice.roll())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Files and Directories"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "from pathlib import Path\n",
    "path = Path(\".\")\n",
    "print(path.exists())\n",
    "\n",
    "\n",
    "#if u want to make new directory\n",
    "\n",
    "# path1 = Path(\"Files_Directories\")\n",
    "# path1.mkdir()\n",
    "\n",
    "#when u want to remove directory\n",
    "#path.rmdir()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ch02.ipynb\n",
      "module.ipynb\n",
      "Python-1.ipynb\n"
     ]
    }
   ],
   "source": [
    "path2 = Path()\n",
    "for file in path2.glob(\"*.ipynb\"):\n",
    "    print(file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      ".ipynb_checkpoints\n",
      "ch02.ipynb\n",
      "Files_Directories\n",
      "module.ipynb\n",
      "Python-1.ipynb\n"
     ]
    }
   ],
   "source": [
    "path3 = Path()\n",
    "for file in path3.glob(\"*\"):\n",
    "    print(file)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Working with spreadsheet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "transaction_id\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "import openpyxl as xl\n",
    "from openpyxl.chart import BarChart,Reference\n",
    "\n",
    "# Here openpyxl -> package , chart -> module , BarChart -> class\n",
    "#instead of passing a file name ->  we can use function and store the path in \"filename\" variable and pass as argument to function \n",
    "\n",
    "wb = xl.load_workbook(r'C:\\Users\\Hritik Jaiswal\\Downloads\\Spreadsheet\\transactions.xlsx')\n",
    "sheet = wb['Sheet1']\n",
    "\n",
    "\n",
    "#method to get a cell \n",
    "\n",
    "cell = sheet['a1']\n",
    "#another method ->   cell = sheet.cell(1,1)\n",
    "\n",
    "print(cell.value)\n",
    "#print max row\n",
    "print(sheet.max_row)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5.95\n",
      "6.95\n",
      "7.95\n"
     ]
    }
   ],
   "source": [
    "#we have to modify value of the cell and store into another excel file\n",
    "\n",
    "for row in range(2,sheet.max_row+1):\n",
    "    cell = sheet.cell(row,3)  # column is 3\n",
    "    print(cell.value)\n",
    "    corrected_value = cell.value * 0.9\n",
    "    \n",
    "    #now we have to place a corrected value into anther column \n",
    "    corrected_value_cell = sheet.cell(row,4)  #add corrected value into the 4 column\n",
    "    \n",
    "    corrected_value_cell.value = corrected_value\n",
    "#Excersice \n",
    "\n",
    "# u have to create a bar graph in excel\n",
    "\n",
    "\n",
    "values = Reference(sheet, \n",
    "          \n",
    "          min_row=2,max_row = sheet.max_row,\n",
    "         \n",
    "          min_col = 4 , max_col = 4\n",
    "         )\n",
    "\n",
    "chart = BarChart()\n",
    "chart.add_data(values)\n",
    "sheet.add_chart(chart, 'f2')\n",
    "\n",
    "\n",
    "wb.save(\"transaction2.xlsx\")\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Machine learning"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Steps :\n",
    "    \n",
    "                    1) Import the Data \n",
    "                    2) clean the Data\n",
    "                    3) split the Data into training/test sets\n",
    "                    4) create a model\n",
    "                    5) train the model\n",
    "                    6) make prediction\n",
    "                    7) Evaluate and Improve\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(16598, 11)"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Importing a data set\n",
    "import pandas as pd\n",
    "df = pd.read_csv('vgsales.csv')\n",
    "df.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Rank</th>\n",
       "      <th>Year</th>\n",
       "      <th>NA_Sales</th>\n",
       "      <th>EU_Sales</th>\n",
       "      <th>JP_Sales</th>\n",
       "      <th>Other_Sales</th>\n",
       "      <th>Global_Sales</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>16598.000000</td>\n",
       "      <td>16327.000000</td>\n",
       "      <td>16598.000000</td>\n",
       "      <td>16598.000000</td>\n",
       "      <td>16598.000000</td>\n",
       "      <td>16598.000000</td>\n",
       "      <td>16598.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>8300.605254</td>\n",
       "      <td>2006.406443</td>\n",
       "      <td>0.264667</td>\n",
       "      <td>0.146652</td>\n",
       "      <td>0.077782</td>\n",
       "      <td>0.048063</td>\n",
       "      <td>0.537441</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>4791.853933</td>\n",
       "      <td>5.828981</td>\n",
       "      <td>0.816683</td>\n",
       "      <td>0.505351</td>\n",
       "      <td>0.309291</td>\n",
       "      <td>0.188588</td>\n",
       "      <td>1.555028</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1980.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.010000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>4151.250000</td>\n",
       "      <td>2003.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.060000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>8300.500000</td>\n",
       "      <td>2007.000000</td>\n",
       "      <td>0.080000</td>\n",
       "      <td>0.020000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.010000</td>\n",
       "      <td>0.170000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>12449.750000</td>\n",
       "      <td>2010.000000</td>\n",
       "      <td>0.240000</td>\n",
       "      <td>0.110000</td>\n",
       "      <td>0.040000</td>\n",
       "      <td>0.040000</td>\n",
       "      <td>0.470000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>16600.000000</td>\n",
       "      <td>2020.000000</td>\n",
       "      <td>41.490000</td>\n",
       "      <td>29.020000</td>\n",
       "      <td>10.220000</td>\n",
       "      <td>10.570000</td>\n",
       "      <td>82.740000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               Rank          Year      NA_Sales      EU_Sales      JP_Sales  \\\n",
       "count  16598.000000  16327.000000  16598.000000  16598.000000  16598.000000   \n",
       "mean    8300.605254   2006.406443      0.264667      0.146652      0.077782   \n",
       "std     4791.853933      5.828981      0.816683      0.505351      0.309291   \n",
       "min        1.000000   1980.000000      0.000000      0.000000      0.000000   \n",
       "25%     4151.250000   2003.000000      0.000000      0.000000      0.000000   \n",
       "50%     8300.500000   2007.000000      0.080000      0.020000      0.000000   \n",
       "75%    12449.750000   2010.000000      0.240000      0.110000      0.040000   \n",
       "max    16600.000000   2020.000000     41.490000     29.020000     10.220000   \n",
       "\n",
       "        Other_Sales  Global_Sales  \n",
       "count  16598.000000  16598.000000  \n",
       "mean       0.048063      0.537441  \n",
       "std        0.188588      1.555028  \n",
       "min        0.000000      0.010000  \n",
       "25%        0.000000      0.060000  \n",
       "50%        0.010000      0.170000  \n",
       "75%        0.040000      0.470000  \n",
       "max       10.570000     82.740000  "
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 'Wii Sports', 'Wii', ..., 3.77, 8.46, 82.74],\n",
       "       [2, 'Super Mario Bros.', 'NES', ..., 6.81, 0.77, 40.24],\n",
       "       [3, 'Mario Kart Wii', 'Wii', ..., 3.79, 3.31, 35.82],\n",
       "       ...,\n",
       "       [16598, 'SCORE International Baja 1000: The Official Game', 'PS2',\n",
       "        ..., 0.0, 0.0, 0.01],\n",
       "       [16599, 'Know How 2', 'DS', ..., 0.0, 0.0, 0.01],\n",
       "       [16600, 'Spirits & Spells', 'GBA', ..., 0.0, 0.0, 0.01]],\n",
       "      dtype=object)"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.values"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Shortcut"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "we can use shift-tab to describe function\n",
    "\n",
    "all shortcut is present when we click 'h' in editor"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Real world problem\n",
    "\n",
    "recommend various music albums thier likely to buy based on age and gender\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Importing Data "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>age</th>\n",
       "      <th>gender</th>\n",
       "      <th>genre</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>20</td>\n",
       "      <td>1</td>\n",
       "      <td>HipHop</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>23</td>\n",
       "      <td>1</td>\n",
       "      <td>HipHop</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>25</td>\n",
       "      <td>1</td>\n",
       "      <td>HipHop</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>26</td>\n",
       "      <td>1</td>\n",
       "      <td>Jazz</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>29</td>\n",
       "      <td>1</td>\n",
       "      <td>Jazz</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>30</td>\n",
       "      <td>1</td>\n",
       "      <td>Jazz</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>31</td>\n",
       "      <td>1</td>\n",
       "      <td>Classical</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>33</td>\n",
       "      <td>1</td>\n",
       "      <td>Classical</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>37</td>\n",
       "      <td>1</td>\n",
       "      <td>Classical</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>20</td>\n",
       "      <td>0</td>\n",
       "      <td>Dance</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>21</td>\n",
       "      <td>0</td>\n",
       "      <td>Dance</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>25</td>\n",
       "      <td>0</td>\n",
       "      <td>Dance</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>26</td>\n",
       "      <td>0</td>\n",
       "      <td>Acoustic</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>27</td>\n",
       "      <td>0</td>\n",
       "      <td>Acoustic</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>30</td>\n",
       "      <td>0</td>\n",
       "      <td>Acoustic</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>31</td>\n",
       "      <td>0</td>\n",
       "      <td>Classical</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>34</td>\n",
       "      <td>0</td>\n",
       "      <td>Classical</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>35</td>\n",
       "      <td>0</td>\n",
       "      <td>Classical</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    age  gender      genre\n",
       "0    20       1     HipHop\n",
       "1    23       1     HipHop\n",
       "2    25       1     HipHop\n",
       "3    26       1       Jazz\n",
       "4    29       1       Jazz\n",
       "5    30       1       Jazz\n",
       "6    31       1  Classical\n",
       "7    33       1  Classical\n",
       "8    37       1  Classical\n",
       "9    20       0      Dance\n",
       "10   21       0      Dance\n",
       "11   25       0      Dance\n",
       "12   26       0   Acoustic\n",
       "13   27       0   Acoustic\n",
       "14   30       0   Acoustic\n",
       "15   31       0  Classical\n",
       "16   34       0  Classical\n",
       "17   35       0  Classical"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "data = pd.read_csv('music.csv')\n",
    "data\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>age</th>\n",
       "      <th>gender</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>18.000000</td>\n",
       "      <td>18.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>27.944444</td>\n",
       "      <td>0.500000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>5.127460</td>\n",
       "      <td>0.514496</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>20.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>25.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>28.000000</td>\n",
       "      <td>0.500000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>31.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>37.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             age     gender\n",
       "count  18.000000  18.000000\n",
       "mean   27.944444   0.500000\n",
       "std     5.127460   0.514496\n",
       "min    20.000000   0.000000\n",
       "25%    25.000000   0.000000\n",
       "50%    28.000000   0.500000\n",
       "75%    31.000000   1.000000\n",
       "max    37.000000   1.000000"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "# we will create two input data set that will be 'age' and 'gender'\n",
    "# we will pass 'age' and 'gender' and based on the input we predict the output \n",
    "# output will be stored in 'genre' \n",
    "\n",
    "X = data.drop(columns=['genre'])\n",
    "\n",
    "Y = data['genre']\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Learning and Predicting"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['Dance', 'HipHop'], dtype=object)"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.tree import DecisionTreeClassifier\n",
    "\n",
    "model = DecisionTreeClassifier()\n",
    "# takes two attributes 1. input dataset 2. output dataset\n",
    "model.fit(X,Y)\n",
    "\n",
    "\n",
    "#let make a prediction by passing input Here 22 is age and 0 is female\n",
    "\n",
    "prediction = model.predict([[22,0],[25,1]])\n",
    "prediction\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Calculating the Accuracy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    age  gender\n",
      "0    20       1\n",
      "3    26       1\n",
      "5    30       1\n",
      "6    31       1\n",
      "12   26       0\n",
      "9    20       0\n",
      "11   25       0\n",
      "2    25       1\n",
      "10   21       0\n",
      "Accuracy is : 0.6666666666666666\n"
     ]
    }
   ],
   "source": [
    "# for calculating the accuracy we need to test and train the model \n",
    "# generally 70-80% data need to training and 20-30% for testing\n",
    "\n",
    "from sklearn.model_selection import train_test_split \n",
    "\n",
    "# Here we use 20% for testing and check with respect to the predicted value\n",
    "# the function will return 4 tuple we have to get that result into a variable \n",
    "\n",
    "X_train,X_test,Y_train,Y_test = train_test_split(X,Y,test_size = 0.5)\n",
    "print(X_train)\n",
    "model.fit(X_train,Y_train)\n",
    "\n",
    "# we pass input as X_test in attribute\n",
    "prediction = model.predict(X_test)\n",
    "\n",
    "# now to check accurancy we have to compair the prediction with y_test()\n",
    "from sklearn.metrics import accuracy_score\n",
    "\n",
    "score = accuracy_score(Y_test,prediction)\n",
    "print(\"Accuracy is : {}\".format(score))\n",
    "\n",
    "#every time we run are model accuracy will be changing"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Model Persistance"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "for training model again again takes lot of time instead \n",
    "we can save the trained model in one joblib file \n",
    "\n",
    "run these piece of code after applying model i.e after :\n",
    "\n",
    "        model = DecisionTreeClassifier()\n",
    "        \n",
    "        from sklearn.externals import joblib \n",
    "        joblib.dump(model,'model-recommender.joblib')\n",
    "\n",
    "after runing these commment the trained model syntax and and then direclty load \n",
    "\n",
    "        joblib.load('model-recommender.joblib')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "# Visualizing a Decision Tree\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "# u can see a graph i.e decision tree on visual studio code \n",
    "# by clicking a sidebar preview button\n",
    "    \n",
    "from sklearn import tree\n",
    "tree.export_graphviz(model,\n",
    "                    out_file = \"music-recommender.dot\",\n",
    "                    class_names = sorted(Y.unique()),\n",
    "                    label = 'all',\n",
    "                    rounded =True ,\n",
    "                    filled= True\n",
    "                    )\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "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.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
À propos de cet Algorithme

Python Basic

name = input("what is name ?")
print('HI ' +name)
what is name ?mosh
HI mosh
name = input('what is your birthyear ? ')
year = 2019 -int(name);
print(year)
what is your birthdate ? 2000
19

Type

#type 

print(type(name))
&amp;lt;class &#x27;str&#x27;&amp;gt;
weight = input("what is weight ")
final = int(weight)*0.45
print(final)
what is weight 45
20.25
#if want to use this type statement -&gt; year's then we have to use double quotes ""
string = "you're awesome"
print(string)

# if want to assign "important msg" like this we have to use single quote  ''
str = 'im "Hritik"'
print(str)

# for multitline string ''' % ''' for sending mail 

mail = '''
        Hii buddy 

        im Hritik 

        you look awesome 

        thank you,
 '''

print(mail)
you&#x27;re awesome
im &quot;Hritik&quot;

        Hii buddy 

        im Hritik 

        you look awesome 

        thank you,
 
course = 'python beginners'
print(course[0])
print(course[-2])
print(course[0:])
print(course[:])
print(course[:5])
print(course[0:5])
print(course[1:3]) #exclude 3 only takes 1,2

print(course[1:-1]) # exclude -1 
p
r
python beginners
python beginners
pytho
pytho
yt
ython beginner

Inbuilt - Function

#length of string including space
c = 'hii buddy'
print(len(c))
9
#various function 
msg = 'python is very imp lang'
print(msg.lower())
print(msg.upper())
print(msg.count('i'))
print(msg.find('t'))
print(msg.title())
print(msg.replace('is','are'))
python is very imp lang
PYTHON IS VERY IMP LANG
2
2
Python Is Very Imp Lang
python are very imp lang
#ordering 
# exp &gt; (mul or div) &gt; (add or sub)
exp = 10 + 3*2**2
print(exp)
22

Math function

#Math function
# 1) round
x = 6.5
y = 7.66
print(round(x))
print(round(y))
6
8
# 2) abs -&gt; return positive no.
x = -3.5
print(abs(x))
3.5
# 3) Mathametical function
import math
pi = 3.142
math.cos((60*pi)/180)

# ceil and floor
print(math.ceil(2.9))
print(math.floor(2.9))

#thier are many more maths module
3
2

If - Statement

#if statement

x = 5
y = 10

if (x&gt;y):
    print("truee")
elif (x==y):
    print("falsee")
else:
    print("nothing")
    
print(f"SUM OF X AND Y : {x+y}")
nothing
SUM OF X AND Y : 15

Format

x = "Hii"
y = 'Hritik {}'
z = 'Jaiswal'
print(f"sum of string is x + y : {x+y}")
print(y.format(z))
# we can use 'and' & 'or' in if statement
a=2 
b=3
c=4
if (a&lt;b) and (c&gt;b):
    print('True')
elif (a==b) or (c&gt;a):
    print('Truee')
elif (b&gt;a) and not(b&gt;c):
    print('Trueee')
else:
    print('false')
    
True
#Exercise

weight = int(input('Weight :'))
unit = input('(L)bs or (K)g')
if unit.lower()=='l' or unit.upper()=='L':
    c = weight*0.45;
else:
    c = weight/0.45;
print(c)
Weight :45
(L)bs or (K)gv
100.0

While loop

#while

i=1
while (i&lt;5):
    print('*'*i)
    i+=1
    
*
**
***
****

for loop

#for loop
for item in 'Teacher':
    print(item)
T
e
a
c
h
e
r
for i in ['hii','buddy ','whats','up']:
    print(i)
print('-----------------')
for i in range(4):
    print(i)
print('-----------------')
for i in range(2,8):
    print(i)
print('-----------------')
for i in range(2,8,3):
    print(i)
print('-----------------')
for i in [5,6,7,8]:
    print(i)
hii
buddy 
whats
up
-----------------
0
1
2
3
-----------------
2
3
4
5
6
7
-----------------
2
5
-----------------
5
6
7
8
r = [2,3,4,5]
total = 0
for i in r:
    total+=i
print(total)
print('-----------')
r = [2,3,4,5]
e = sum(r)
print(e)
14
-----------
14
for i in range(4):
    for j in range(3):
        print(f'({i},{j})')
(0,0)
(0,1)
(0,2)
(1,0)
(1,1)
(1,2)
(2,0)
(2,1)
(2,2)
(3,0)
(3,1)
(3,2)
#array
num = [1,2,3,4,5]
for i in num:
    print('$'*i)
$
$$
$$$
$$$$
$$$$$
name = ['ab ','ac','ad','ae']
print(name[0])
print(name[-1])
ab 
ae

List

#2D-List 
matrix = [[1,2,3],[4,5,6],[7,8,9]]
for i in matrix:
    print(i)
print('-----print individual item-----')
for i in matrix:
    for j in i:
        print(j)
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
-----print individual item-----
1
2
3
4
5
6
7
8
9

Inbuilt - Function

#working with function 
# 1.append 2.insert 3.remove 4.pop 5.clear
number = [2,3,4,6]
print('-----Append---------')
number.append(9)
print(number)
print('-----Insert---------')
number.insert(2,8)
print(number)
print('-----Remove---------')
number.remove(8)
print(number)
print('-----pop---------')
number.pop()
print(number)
print('-----clear---------')
number.clear()
print(number)
-----Append---------
[2, 3, 4, 6, 9]
-----Insert---------
[2, 3, 8, 4, 6, 9]
-----Remove---------
[2, 3, 4, 6, 9]
-----pop---------
[2, 3, 4, 6]
-----clear---------
[]
#index 
list_item = [2,4,6,7,2,4]
print(list_item.index(6))
print(list_item.count(2))
2
2
# 1.sort 2.reverse 
item = [3,5,2,8,1]
item.sort()
print(item)
item.reverse()
print(item)
[1, 2, 3, 5, 8]
[8, 5, 3, 2, 1]
#list will update if we changing list item before calling copy function ,
# but list will not be change when u are appending and deleting after copy function
a = [2,3,4,5]
b = a.copy()
a.append(10)
print(b)
[2, 3, 4, 5]
#excercise -&gt; remove a duplicate no. from the list
numbers = [2,3,4,4,3,5]
unique = []

for i in numbers:
    if i not in unique:
        unique.append(i)
        
print(unique)
[2, 3, 4, 5]

tuple

#tuple -&gt; we can not append , remove , pop , insert , u can not modify your list .
#only thing u can do -&gt; count , index a item in list

## The main diffrence between tuple and list is :- tuple is immutable and list is mutable
## that mean we can not change tuple value by modifying it but in list we can do that.
number = (1,2,3)
print(number[0])

# this will throw error :
# number[0] = 5
1

Set

{1,2,3}
{1, 2, 3}
# display unique value 
{1,1,4,2,2,5}
{1, 2, 4, 5}
set([1,2,4,4,8,8,4])
{1, 2, 4, 8}
# add value to the set
s = {1,2,3}
s.add(5)
s
{1, 2, 3, 5}

Map

def times(var):
    return var*2;
    
times(2)
4
seq = [1,2,3]

list(map(times,seq))
[2, 4, 6]

Lambda

# instead of writing like this we can write 
# def times(var):
#     return var*2;

t = lambda var : var*2
t(3)
6
list(map(t,seq))
[2, 4, 6]

Filter

list(map(lambda num : num %2 ==0,seq))
[False, True, False]
list(filter(lambda num : num %2 ==0,seq))
[2]

Unpacking

#unpacking
coordinates = [2,3,4]
# x = coordinates[0]
# y = coordinates[1]
# z = coordinates[2]
# instead of writing like this we can write 
x,y,z = coordinates  #unpacking -&gt; work with both list and tuple
print(x)
2

Dictionaries

#Dictionaries -&gt; contain key - value pairs 

people = {
    "name" :"Hritik Jaiswal",
    "age" : 19,
    "is_male" : True
}
print(people["name"])

# u can not write like -&gt; print(people["Name"])
# we can use GET method to take key-value pair if its not present and display key value pair 
print(people.get("birth","may 31 2000"))
print(people.get("gender"))   #None -&gt; is the object that represent absence of the value

people["name"] = "HritiK Dinesh Jaiswal"  # modify name
print(people.get("name"))
Hritik Jaiswal
may 31 2000
None
HritiK Dinesh Jaiswal
#excersice -&gt; inuput 1234 display output : one two three four
phone =  input("phone : ")

dict  = {
    "1" :"One",
    "2":"Two",
    "3" :"Three",
    "4" :"Four",
    "5" :"Five",
    "6" :"Six",
    "7" :"Seven",
    "8" :"Eight",
    "9" :"Nine",
    "0" :"zero",
}
output = "" 
for i in phone:
    
    output += (dict.get(i,"?")) + " " 
    print(i)
print(output) 
phone : 105
1
0
5
One zero Five 
#excerisce -&gt; opposite 
words = ["one", "two","three","four","five","six", "seven","eight","nine","zero"]

word = input("Enter the word : ")
mapping  = {
    "one":"1", 
    "two":"2",
    "three":"3",
    "four":"4",
    "five":"5",
    "six":"6", 
    "seven":"7",
    "eight":"8",
    "nine":"9",
    "zero":"0"
}
spliting = word.split()
print(spliting)
for i in spliting:
    print(mapping.get(i,"&"))
    
    
Enter the word : one seven two
[&#x27;one&#x27;, &#x27;seven&#x27;, &#x27;two&#x27;]
1
7
2
#excersice -&gt; print emoji
message = input("&gt;")
words= message.split()
emoji = {
    ":)" :"😄",
    ":(" :"😔"
}
output = ""
for i in words:
    output += emoji.get(i,i) + " "
    
print(output)
&amp;gt;good morning :)
good morning 😄 

Function

#function
def text():
    print("im hritik")
print("hii")
text()
hii
im hritik

Parameter and Arguments

#function with parameter
#parameter -&gt;is placeholder that we passed to the function
#argumetnt -&gt; is actual value that u gone pass inside function
def text(f_name, l_name):
    print(f'Hii {f_name} and {l_name}')
print("thanks")
text('hritik','jaiswal')
thanks
Hii hritik and jaiswal

keyword argument

#keyword argument -&gt; its helpful when u don't want to pass argument in order u can pass in any order 
def text(discount , shipping ,total ):
    print(f'Here total is {total} including shipping charges {shipping} + discount {discount}')
print("hello")
text(shipping=2000,total=5000,discount=500)
hello
Here total is 5000 including shipping charges 2000 + discount 500
#return statement 
def square(num):
    return num*num
result = square(5)
print(result)
    
25
#tric -&gt; if u don't write return statement then it will return object which is : none 
def cube(n):
    print(n**3)
print(cube(3))
27
None

Exception

#exception -&gt; when we try to input string value instead of int 
# age = int(input("your age"))
# print(age)

try:
    age = int(input("your age : "))
    print(age)
except ValueError:
    print('invalid value')
your age : twenty
invalid value

try:
    age = int(input("your age : "))
    income = 20000
    risk = float(income/age);
    print(f'risk is {risk}')
except ValueError and ZeroDivisionError:
    print("invalid value or age can't be negative ")
your age : 0
invalid value or age can&#x27;t be negative 

Class

#class 
# 1) type -1

class rect:
    def rect_area(self):
        print("area")
p = rect()
p.rect_area()

area
# 2) type -2
class Employee:
    def __init__(self, first, last , salary):
        self.first = first
        self.last = last
        self.salary = salary
        self.email = first + '.'+last +'@gmail.com'
    
    def fullname(self):
        return "{} {}".format(self.first,self.last)

emp1 = Employee('Hritik','Jaiswal',5000)
emp2 = Employee('Aniket','Jaiswal',6000)

#their are two methods 
print(emp1.fullname())
print(Employee.fullname(emp1))
              
Hritik Jaiswal
Hritik Jaiswal
# 3) type -3
class Point:
    def __init__(self,a,l,h):
        self.a = a
        self.l = l
        self.h = h
        
    def square(self):
        print(f"area of square :{self.a*self.a}")
    
    def rectangle(self):
        print("area of rectangle is : {}".format(self.l*self.h))
    

#create a object 
point1 = Point(3,2,3)
point1.square()
point1.rectangle()
area of square :9
area of rectangle is : 6

Inheritance

#inheritance -&gt; dog and cat are inherite a class mammel
class mammel:
    def walk(self):
        print("walk")

class dog(mammel):
    def bark(self):
        print("bark")

class cat(mammel):
    pass

dog1 = dog()
dog1.bark()

cat1 = cat()
cat1.walk()
bark
walk

Module

#module -&gt; module.ipynb file which we have created we can directly import function also
#we need to install from anaconda prompt -&gt; pip install import-ipynb
import import_ipynb

import module
from module import cm2m

cm2m(100)
m2cm
1.0
numbers = [5,4,6,8,10]
print(max(numbers))
print(min(numbers))
10
4

Packages

package -: we can create a seperate .py file and extract this file and import into another file as similar to module

package is collections of different modules

Type of Module

    1) Absolute module 

from mypackage.mymodule1 import class A obj = class A

    2) relative module

if im working in "module1" & i want to import Class C from "module2" into my "module1"

from module2 import classC obj = classC()

Random

import random

for i in range(3):
    print(random.random())
0.8556515733440572
0.9018671283206765
0.6655666651378818
for i in range(3):
    print(random.randint(10,20))
12
20
15
# randomly choose the value 
members = ['hritik', 'jaiswal','aniket','shweta']
show = random.choice(members)
print(show)
jaiswal
#excercise -&gt; dice thrown give random value

class Dice:
    def roll(self):
        x = (1,2,3,4,5,6)
        y = (1,2,3,4,5,6)
        m = random.choice(x)
        l = random.choice(y)
        print("({},{})".format(m,l))
        

r = Dice()
r.roll()

(2,4)
# another method

class Dice:
    def roll(self):
        first = random.randint(1,6)
        second = random.randint(1,6)
        return first,second
dice = Dice()
print(dice.roll())
(5, 6)

Files and Directories

from pathlib import Path
path = Path(".")
print(path.exists())


#if u want to make new directory

# path1 = Path("Files_Directories")
# path1.mkdir()

#when u want to remove directory
#path.rmdir()

True
path2 = Path()
for file in path2.glob("*.ipynb"):
    print(file)
ch02.ipynb
module.ipynb
Python-1.ipynb
path3 = Path()
for file in path3.glob("*"):
    print(file)
.ipynb_checkpoints
ch02.ipynb
Files_Directories
module.ipynb
Python-1.ipynb

Working with spreadsheet

import openpyxl as xl
from openpyxl.chart import BarChart,Reference

# Here openpyxl -&gt; package , chart -&gt; module , BarChart -&gt; class
#instead of passing a file name -&gt;  we can use function and store the path in "filename" variable and pass as argument to function 

wb = xl.load_workbook(r'C:\Users\Hritik Jaiswal\Downloads\Spreadsheet\transactions.xlsx')
sheet = wb['Sheet1']


#method to get a cell 

cell = sheet['a1']
#another method -&gt;   cell = sheet.cell(1,1)

print(cell.value)
#print max row
print(sheet.max_row)
transaction_id
4
#we have to modify value of the cell and store into another excel file

for row in range(2,sheet.max_row+1):
    cell = sheet.cell(row,3)  # column is 3
    print(cell.value)
    corrected_value = cell.value * 0.9
    
    #now we have to place a corrected value into anther column 
    corrected_value_cell = sheet.cell(row,4)  #add corrected value into the 4 column
    
    corrected_value_cell.value = corrected_value
#Excersice 

# u have to create a bar graph in excel


values = Reference(sheet, 
          
          min_row=2,max_row = sheet.max_row,
         
          min_col = 4 , max_col = 4
         )

chart = BarChart()
chart.add_data(values)
sheet.add_chart(chart, 'f2')


wb.save("transaction2.xlsx")

5.95
6.95
7.95

Machine learning

Steps :

                1) Import the Data 
                2) clean the Data
                3) split the Data into training/test sets
                4) create a model
                5) train the model
                6) make prediction
                7) Evaluate and Improve
#Importing a data set
import pandas as pd
df = pd.read_csv('vgsales.csv')
df.shape
(16598, 11)
df.describe()
Rank Year NA_Sales EU_Sales JP_Sales Other_Sales Global_Sales
count 16598.000000 16327.000000 16598.000000 16598.000000 16598.000000 16598.000000 16598.000000
mean 8300.605254 2006.406443 0.264667 0.146652 0.077782 0.048063 0.537441
std 4791.853933 5.828981 0.816683 0.505351 0.309291 0.188588 1.555028
min 1.000000 1980.000000 0.000000 0.000000 0.000000 0.000000 0.010000
25% 4151.250000 2003.000000 0.000000 0.000000 0.000000 0.000000 0.060000
50% 8300.500000 2007.000000 0.080000 0.020000 0.000000 0.010000 0.170000
75% 12449.750000 2010.000000 0.240000 0.110000 0.040000 0.040000 0.470000
max 16600.000000 2020.000000 41.490000 29.020000 10.220000 10.570000 82.740000
df.values
array([[1, &#x27;Wii Sports&#x27;, &#x27;Wii&#x27;, ..., 3.77, 8.46, 82.74],
       [2, &#x27;Super Mario Bros.&#x27;, &#x27;NES&#x27;, ..., 6.81, 0.77, 40.24],
       [3, &#x27;Mario Kart Wii&#x27;, &#x27;Wii&#x27;, ..., 3.79, 3.31, 35.82],
       ...,
       [16598, &#x27;SCORE International Baja 1000: The Official Game&#x27;, &#x27;PS2&#x27;,
        ..., 0.0, 0.0, 0.01],
       [16599, &#x27;Know How 2&#x27;, &#x27;DS&#x27;, ..., 0.0, 0.0, 0.01],
       [16600, &#x27;Spirits &amp; Spells&#x27;, &#x27;GBA&#x27;, ..., 0.0, 0.0, 0.01]],
      dtype=object)

Shortcut

we can use shift-tab to describe function

all shortcut is present when we click 'h' in editor

Real world problem

recommend various music albums thier likely to buy based on age and gender

Importing Data

import pandas as pd

data = pd.read_csv('music.csv')
data
age gender genre
0 20 1 HipHop
1 23 1 HipHop
2 25 1 HipHop
3 26 1 Jazz
4 29 1 Jazz
5 30 1 Jazz
6 31 1 Classical
7 33 1 Classical
8 37 1 Classical
9 20 0 Dance
10 21 0 Dance
11 25 0 Dance
12 26 0 Acoustic
13 27 0 Acoustic
14 30 0 Acoustic
15 31 0 Classical
16 34 0 Classical
17 35 0 Classical
data.describe()
age gender
count 18.000000 18.000000
mean 27.944444 0.500000
std 5.127460 0.514496
min 20.000000 0.000000
25% 25.000000 0.000000
50% 28.000000 0.500000
75% 31.000000 1.000000
max 37.000000 1.000000
# we will create two input data set that will be 'age' and 'gender'
# we will pass 'age' and 'gender' and based on the input we predict the output 
# output will be stored in 'genre' 

X = data.drop(columns=['genre'])

Y = data['genre']

Learning and Predicting

from sklearn.tree import DecisionTreeClassifier

model = DecisionTreeClassifier()
# takes two attributes 1. input dataset 2. output dataset
model.fit(X,Y)


#let make a prediction by passing input Here 22 is age and 0 is female

prediction = model.predict([[22,0],[25,1]])
prediction

array([&#x27;Dance&#x27;, &#x27;HipHop&#x27;], dtype=object)

Calculating the Accuracy

# for calculating the accuracy we need to test and train the model 
# generally 70-80% data need to training and 20-30% for testing

from sklearn.model_selection import train_test_split 

# Here we use 20% for testing and check with respect to the predicted value
# the function will return 4 tuple we have to get that result into a variable 

X_train,X_test,Y_train,Y_test = train_test_split(X,Y,test_size = 0.5)
print(X_train)
model.fit(X_train,Y_train)

# we pass input as X_test in attribute
prediction = model.predict(X_test)

# now to check accurancy we have to compair the prediction with y_test()
from sklearn.metrics import accuracy_score

score = accuracy_score(Y_test,prediction)
print("Accuracy is : {}".format(score))

#every time we run are model accuracy will be changing
    age  gender
0    20       1
3    26       1
5    30       1
6    31       1
12   26       0
9    20       0
11   25       0
2    25       1
10   21       0
Accuracy is : 0.6666666666666666

Model Persistance

for training model again again takes lot of time instead we can save the trained model in one joblib file

run these piece of code after applying model i.e after :

    model = DecisionTreeClassifier()
    
    from sklearn.externals import joblib 
    joblib.dump(model,'model-recommender.joblib')

after runing these commment the trained model syntax and and then direclty load

    joblib.load('model-recommender.joblib')

Visualizing a Decision Tree

# u can see a graph i.e decision tree on visual studio code 
# by clicking a sidebar preview button
    
from sklearn import tree
tree.export_graphviz(model,
                    out_file = "music-recommender.dot",
                    class_names = sorted(Y.unique()),
                    label = 'all',
                    rounded =True ,
                    filled= True
                    )