{ "metadata": { "name": "", "signature": "sha256:1cc1ac9d61c99e04ae63566bdfa677df063ee787bc20b035f2efc0afd4350185" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 1, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Introduction" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Noufal Ibrahim\n", "* Freelance programmer and trainer\n", "* http://nibrahim.net.in\n", "* Also http://thelycaeum.in\n", "* noufal@nibrahim.net.in\n" ] }, { "cell_type": "heading", "level": 1, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Plan" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Day 1 \n", " - Introduction\n", " - Basic syntax and types.\n", " - Control structures\n", " - Functions\n", " - Modules and importing them\n", " - Standard library\n", " - Introduction to objects and classes.\n", "* Day 2 \n", " - Project - Simple chat server and gui client." ] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Intro" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Python is a \"scripting\" language\n", "* Compiled to byte code and interpreted.\n", "* High level\n", "* Dynamically typed\n", "* Strongly typed" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Data types" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Numeric types - int, float, complex\n", "* Strings\n", " * Basic methods\n", " * Indexing\n", " * Slicing\n", "* Lists\n", " * Basic methods\n", " * Mutability - sorting/reversing\n", "* Sets\n", " * Mathematical sets\n", " * Unordered\n", "* Dictionaries\n", " * Hash tables. \n", " * Key/value association" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print 9 + 5\n", "print 9 - 5\n", "print 9 / 5\n", "print 9.0 / 5\n", "print 9 ** 2\n", "print 9 % 4" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "14\n", "4\n", "1\n", "1.8\n", "81\n", "1\n" ] } ], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "x = 2+3j\n", "y = 5\n", "\n", "print x + y" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "(7+3j)\n" ] } ], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "t = -5\n", "print abs(t)\n", "\n", "t = 2+ 3j\n", "print abs(t)\n", "\n", "t = 2 + 3j\n", "\n", "print t.real\n", "print t.imag" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "5\n", "3.60555127546\n", "2.0\n", "3.0\n" ] } ], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": [ "x = \"Python primer\" # Double quotes\n", "print x\n", "x = 'python primer' # Single quotes\n", "print x\n", "x = '\"Go there\", he said\"' # Quotes of one kind protect the other\n", "print x\n", "x = \"\"\"\n", "Python primer\n", "\n", "10 October 2015\n", "\"\"\" # Triple quotes hold multi line strings\n", "print x" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Python primer\n", "python primer\n", "\"Go there\", he said\"\n", "\n", "Python primer\n", "\n", "10 October 2015\n", "\n" ] } ], "prompt_number": 12 }, { "cell_type": "code", "collapsed": false, "input": [ "# String methods\n", "x = \"python primer\"\n", "print x.capitalize()\n", "print x.upper()\n", "print x.lower()\n", "print x.center(50, \"*\")\n", "print x * 2\n", "print \"%d + %d\"%(5, 4) # Old style string formatting\n", "print \"{} + {}\".format(5,4) # New style string formatting\n", "print x + 2\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Python primer\n", "PYTHON PRIMER\n", "python primer\n", "******************python primer*******************\n", "python primerpython primer\n", "5 + 4\n", "5 + 4\n" ] }, { "ename": "TypeError", "evalue": "cannot concatenate 'str' and 'int' objects", "output_type": "pyerr", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 8\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;34m\"%d + %d\"\u001b[0m\u001b[1;33m%\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m4\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# Old style string formatting\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 9\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;34m\"{} + {}\"\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# New style string formatting\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 10\u001b[1;33m \u001b[1;32mprint\u001b[0m \u001b[0mx\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;36m2\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 11\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mTypeError\u001b[0m: cannot concatenate 'str' and 'int' objects" ] } ], "prompt_number": 18 }, { "cell_type": "code", "collapsed": false, "input": [ "x = \"python primer\"\n", "print x[0]\n", "print x[1]\n", "print x[0:7]\n", "print x[0:10:2]\n", "print x[-1]\n", "print x[-7:-1]\n", "print x[-1:-7:-1]\n", "\n", "x[0] = \"T\" # Will error out since strings are immutable in python" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "p\n", "y\n", "python \n", "pto r\n", "r\n", " prime\n", "remirp\n" ] }, { "ename": "TypeError", "evalue": "'str' object does not support item assignment", "output_type": "pyerr", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 8\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m7\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 9\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 10\u001b[1;33m \u001b[0mx\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m\"T\"\u001b[0m \u001b[1;31m# Will error out since strings are immutable in python\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mTypeError\u001b[0m: 'str' object does not support item assignment" ] } ], "prompt_number": 17 }, { "cell_type": "code", "collapsed": false, "input": [ "# Lists\n", "x = [1, 2, 3]\n", "print x\n", "y = [\"IEEE\", \"Python\", 10, 10, 2015]\n", "print y\n", "print y[0]\n", "y[0]= 'ieee'\n", "print y\n", "print y[0]\n", "print y[-1]\n", "y.append(\"Calicut\")\n", "print y\n", "y.extend([10, 11, 12])\n", "print y\n", "print \"Kozhikode\" in y\n", "print \"Calicut\" in y" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[1, 2, 3]\n", "['IEEE', 'Python', 10, 10, 2015]\n", "IEEE\n", "['ieee', 'Python', 10, 10, 2015]\n", "ieee\n", "2015\n", "['ieee', 'Python', 10, 10, 2015, 'Calicut']\n", "['ieee', 'Python', 10, 10, 2015, 'Calicut', 10, 11, 12]\n", "False\n", "True\n" ] } ], "prompt_number": 40 }, { "cell_type": "code", "collapsed": false, "input": [ "# Sets - Unique elements, unordered\n", "\n", "x = set([])\n", "print x\n", "x = {1, 2, 3}\n", "print x\n", "# print x[0] # This will error out since sets are unordered\n", "print 5 in x\n", "print 3 in x\n", "\n", "x.add(10)\n", "print x\n", "x.add(10)\n", "print x\n", "x = {1,2}\n", "y = {2,3}\n", "\n", "print x.union(y)\n", "print x.intersection(y)\n", "print x - y" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "set([])\n", "set([1, 2, 3])\n", "False\n", "True\n", "set([1, 2, 3, 10])\n", "set([1, 2, 3, 10])\n", "set([1, 2, 3])\n", "set([2])\n", "set([1])\n" ] } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "# Dictionaries\n", "\n", "x = {\"name\" : \"Noufal\",\n", " \"class\" : \"python\",\n", " \"place\" : \"Calicut\"}\n", "\n", "print x[\"name\"]\n", "print x[\"class\"]\n", "# print x[\"date\"] # Will not work since there is no key \"date\"\n", "x[\"strength\"] = 50\n", "print x\n", "print x.keys()\n", "print x.values()\n", "print x.items()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Noufal\n", "python\n", "{'strength': 50, 'place': 'Calicut', 'name': 'Noufal', 'class': 'python'}\n", "['strength', 'place', 'name', 'class']\n", "[50, 'Calicut', 'Noufal', 'python']\n", "[('strength', 50), ('place', 'Calicut'), ('name', 'Noufal'), ('class', 'python')]\n" ] } ], "prompt_number": 11 }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Control structures" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* print statment and string formatting.\n", "* if\n", "* for\n", "* while\n", "* def\n", "* try/except\n", "* list comprehensions\n", "\n", "\n", "* Assignment and deconstruction\n", "* Assignment semantics" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x = \"Python\"\n", "for i in x:\n", " print i\n", " \n", "print \"-\"*10\n", "\n", "x = [1,2,3,4]\n", "for i in x:\n", " print i\n", " \n", "for i in range(10):\n", " print \"5 x %d = %d\"%(i, i*5)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "P\n", "y\n", "t\n", "h\n", "o\n", "n\n", "----------\n", "1\n", "2\n", "3\n", "4\n", "5 x 0 = 0\n", "5 x 1 = 5\n", "5 x 2 = 10\n", "5 x 3 = 15\n", "5 x 4 = 20\n", "5 x 5 = 25\n", "5 x 6 = 30\n", "5 x 7 = 35\n", "5 x 8 = 40\n", "5 x 9 = 45\n" ] } ], "prompt_number": 15 }, { "cell_type": "code", "collapsed": false, "input": [ "x = 10\n", "if x > 5:\n", " print \"Yes, it's greater\"\n", "else:\n", " print \"Nope. It's lesser\"\n", " \n", "if x > 10:\n", " print \"Too large\"\n", "elif x < 5:\n", " print \"Too less\"\n", "else:\n", " print \"Just right\"\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Yes, it's greater\n", "Just right\n" ] } ], "prompt_number": 17 }, { "cell_type": "code", "collapsed": false, "input": [ "# While loop\n", "x = [1,2,3,4,5]\n", "while x:\n", " print x\n", " x.pop()\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[1, 2, 3, 4, 5]\n", "[1, 2, 3, 4]\n", "[1, 2, 3]\n", "[1, 2]\n", "[1]\n" ] } ], "prompt_number": 18 }, { "cell_type": "code", "collapsed": false, "input": [ "# Functions\n", "def double(x):\n", " return x * 2\n", "\n", "print double(10)\n", "print double(\"abc\")\n", "print double([1,2,3])\n", "# print double(dict(x=1, y=2)) # Will error out since you can't multiple a dictionary with a number\n", "\n", "\n", "def add(first, second):\n", " return first + second\n", "\n", "print add(5, 6)\n", "print add(5)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "20\n", "abcabc\n", "[1, 2, 3, 1, 2, 3]\n", "11\n" ] }, { "ename": "TypeError", "evalue": "add() takes exactly 2 arguments (1 given)", "output_type": "pyerr", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 13\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 14\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[0madd\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m6\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 15\u001b[1;33m \u001b[1;32mprint\u001b[0m \u001b[0madd\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mTypeError\u001b[0m: add() takes exactly 2 arguments (1 given)" ] } ], "prompt_number": 24 }, { "cell_type": "code", "collapsed": false, "input": [ "# Fizz bizz\n", "\n", "def fizzbizz(n):\n", " for i in range(1, n+1):\n", " if i % 15 == 0:\n", " print \"fizzbizz\"\n", " elif i % 5 == 0:\n", " print \"bizz\"\n", " elif i% 3 == 0:\n", " print \"fizz\"\n", " else:\n", " print i\n", " \n", "fizzbizz(20)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1\n", "2\n", "fizz\n", "4\n", "bizz\n", "fizz\n", "7\n", "8\n", "fizz\n", "bizz\n", "11\n", "fizz\n", "13\n", "14\n", "fizzbizz\n", "16\n", "17\n", "fizz\n", "19\n", "bizz\n" ] } ], "prompt_number": 27 }, { "cell_type": "code", "collapsed": false, "input": [ "def palindrome(s):\n", " return s == s[::-1]\n", "\n", "print palindrome(\"malayalam\")\n", "print palindrome(\"abba\")\n", "print palindrome(\"Calicut\")" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "True\n", "True\n", "False\n" ] } ], "prompt_number": 29 }, { "cell_type": "code", "collapsed": false, "input": [ "def panagram(s):\n", " for i in \"abcdefghijklmnopqrstuvwxyz\":\n", " if i not in s.lower():\n", " return False\n", " return True\n", "\n", "print panagram(\"the quick brown fox jumps over the lazy dog\")\n", "print panagram(\"the quick brown fox jumped over the lazy dog\")" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "True\n", "False\n" ] } ], "prompt_number": 30 }, { "cell_type": "code", "collapsed": false, "input": [ "def freq(s):\n", " counts = dict()\n", " for i in s:\n", " if i in counts:\n", " counts[i] += 1\n", " else:\n", " counts[i] = 1\n", " \n", " for i in counts:\n", " print \"{} :: {}\".format(i, counts[i])\n", " \n", "freq(\"she sells sea shells on the sea shore!\")" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "a :: 2\n", " :: 7\n", "e :: 7\n", "h :: 4\n", "l :: 4\n", "o :: 2\n", "n :: 1\n", "s :: 8\n", "r :: 1\n", "! :: 1\n", "t :: 1\n" ] } ], "prompt_number": 32 }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Basic Object Oriented programming." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import math\n", "\n", "class Point(object):\n", " def __init__(self, x, y):\n", " self.x = x\n", " self.y = y\n", " \n", " def dist(self):\n", " return math.sqrt(self.x **2 + self.y **2)\n", " \n", " def __repr__(self):\n", " return \"Point({}, {})\".format(self.x, self.y)\n", " \n", " def __add__(self, other):\n", " return Point(self.x + other.x, self.y + other.y)\n", " \n", "p0 = Point(3,4)\n", "print p0.x, p0.y\n", "\n", "print p0.dist()\n", "\n", "#print p0.quad() # I should get 1\n", "#print Point(-2, 3).quad() # I should get 2\n", "#print Point(-2, -3).quad() # I should get 3\n", "#print Point(2, -3).quad() # I should get 4\n", "\n", "print p0\n", "p1 = Point(1, 2)\n", "\n", "print p0 + p1" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "3 4\n", "5.0\n", "Point(3, 4)\n", "Point(4, 6)\n" ] } ], "prompt_number": 39 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Inheritance" ] }, { "cell_type": "code", "collapsed": false, "input": [ "class Shape(object):\n", " def area(self):\n", " raise NotImplementedError()\n", " \n", " def perimeter(self):\n", " raise NotImplementedError()\n", " \n", "class Rectangle(Shape):\n", " def __init__(self, l, b):\n", " self.length = l\n", " self.breadth = b\n", " \n", " def area(self):\n", " return self.length * self.breadth\n", " \n", " def perimeter(self):\n", " return 2*(self.length + self.breadth)\n", "\n", "class Square(Rectangle):\n", " def __init__(self, s):\n", " self.side = s\n", " super(Square, self).__init__(s, s)\n", " \n", "s = Square(5)\n", "print s.area()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "25\n" ] } ], "prompt_number": 11 }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Project" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Exercises" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Write a program to print the times tables for a given number. e.g. `tables(5)` should print something like this\n", "\n", "\n", " 1 x 5 = 5\n", " 2 x 5 = 10\n", " 3 x 5 = 15 \n", " 4 x 5 = 20\n", " 5 x 5 = 25\n", " 6 x 5 = 30\n", " 7 x 5 = 35\n", " 8 x 5 = 40\n", " 9 x 5 = 45\n", " 10 x 5 = 50 \n", "\n", "* FizzBizz. Write a program to print numbers from 1 to a given n. For multiples of 3, print `fizz`, for multiples of 5, print `bizz` and for multiples of 15, print `fizzbizz`. \n", "* Write a program to check if a string is an panagram\n", "* Write a program to check if a string is a palindrome\n", "* Write a program to print letter frequencies in a given string\n", "\n", "\n", " freq(\"she sells sea shells on the sea shore\")\n", " a :: 2\n", " :: 7\n", " e :: 7\n", " h :: 4\n", " l :: 4\n", " o :: 2\n", " n :: 1\n", " s :: 8\n", " r :: 1\n", " t :: 1" ] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Better exercises" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Write a program to compute the primes between 1 and a given number `n` using the sieve of eratosthenes.\n", "* Write a program (using the above) to find prime factors of a given number.\n", "* Write a program to print all permutations of a given list." ] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Object oriented programming" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Basic OO" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* `class` statement\n", "* simple `Point` implementation\n", "* methods `distance` method\n", "* Implement a `quadrant` method\n", "* Magic methods `__repr__`, `__add__`\n" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Exercise" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Implement a vector class that takes `i`, `j`, `k`\n", "* You should be able to add 2 vectors.\n", "* Multiple a vector by a scalar.\n", "* Modulus of a vector (`abs(u)`)\n", "* Dot product of 2 vectors (`u.dot(v)`)\n", "* Cross product of 2 vectors (`u.cross(v)`) - \n", "(Formula : u \u00d7 v = (u 2 v 3 \u2212 u 3 v 2 )i + (u 3 v 1 \u2212 u 1 v 3 )j + (u 1 v 2 \u2212 u 2 v 1 )k)" ] } ], "metadata": {} } ] }