{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "

Day17 Object Oriented Programming in Python

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Recap\n", "\n", "\n", "- Comprehensions\n", " - List\n", " - Dictionary\n", " - Set\n", " - Tuple\n", "- Lambda\n", "- pass\n", "- map()\n", "- filter()\n", "\n", "\n", "### Today Objectives\n", "\n", "\n", "- Iterators\n", "- Generators\n", "- Object Oriented Programming" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Iterators\n", "\n", "\n", "list, string, tuple, set, dictionary" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "100 lines of code\n", "\n", "string with 50 charac\n", "\n", "50 lines I need 25 character\n", "\n", "100 I need 25 char" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "s1 = \"\"\"Python is an interpreted high-level general-purpose programming language. Python's design philosophy emphasizes code readability with its notable use of significant indentation. Wikipedia\n", "Developer: Python Software Foundation\n", "Stable release: 3.9.5 / 3 May 2021; 29 days ago\n", "Preview release: 3.10.0b1 / 3 May 2021; 29 days ago\n", "Typing discipline: Duck, dynamic, strong typing; gradual (since 3.5, but ignored in CPython)\n", "First appeared: February 1991; 30 years ago\n", "Paradigm: Multi-paradigm: object-oriented, procedural (imperative), functional, structured, reflective\"\"\"" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "it = iter(s1)\n", "\n", "\n", "print(it)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'P'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "y\n" ] } ], "source": [ "print(next(it))" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "t\n", "h\n", "o\n", "n\n", " \n", "i\n", "s\n", " \n", "a\n", "n\n" ] } ], "source": [ "for i in range(10):\n", " print(next(it))" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "' '" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'i'" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "it1 = iter('Python')\n", "it2 = 'python'" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "P\n", "y\n", "t\n", "h\n", "o\n", "n\n" ] } ], "source": [ "for i in it1:\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "p\n", "y\n", "t\n", "h\n", "o\n", "n\n" ] } ], "source": [ "for j in it2:\n", " print(j)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "ename": "StopIteration", "evalue": "", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mStopIteration\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnext\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mit1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mStopIteration\u001b[0m: " ] } ], "source": [ "print(next(it1))" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "for i in it1:\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "p\n", "y\n", "t\n", "h\n", "o\n", "n\n" ] } ], "source": [ "for i in it2:\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "li = [1, 2, 3, 4, 5, 6]\n", "\n", "li2 = iter([1, 2, 3, 4, 5, 6])" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3, 4, 5, 6] \n" ] } ], "source": [ "print(li, li2)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(li2)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n" ] } ], "source": [ "print(next(li2))" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(li2)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(li2)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "ename": "StopIteration", "evalue": "", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mStopIteration\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mnext\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mli2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mStopIteration\u001b[0m: " ] } ], "source": [ "next(li2)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3, 4, 5, 6]\n" ] } ], "source": [ "print(li)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "li2 = iter([1, 2, 3, 4, 5, 6])" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n" ] } ], "source": [ "for i in range(3):\n", " print(next(li2))" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n", "5\n", "6\n" ] } ], "source": [ "for i in range(3):\n", " print(next(li2))" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "ename": "StopIteration", "evalue": "", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mStopIteration\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 1\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnext\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mli2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mStopIteration\u001b[0m: " ] } ], "source": [ "for i in range(3):\n", " print(next(li2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Generator" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [], "source": [ "def gen():\n", " a = 10\n", " yield a\n", " \n", " a **= 5\n", " yield a\n", " \n", " a **= 10\n", " yield a" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [], "source": [ "g = gen()" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(g)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "100000" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(g)" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "100000000000000000000000000000000000000000000000000" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(g)" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "ename": "StopIteration", "evalue": "", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mStopIteration\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mnext\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mg\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mStopIteration\u001b[0m: " ] } ], "source": [ "next(g)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [], "source": [ "t1 = (i for i in range(100))" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " at 0x000001EF79E14350>\n" ] } ], "source": [ "print(t1)" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(t1)" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [], "source": [ "li = range(0, 100)" ] }, { "cell_type": "code", "execution_count": 47, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n", "10\n" ] } ], "source": [ "for i in li:\n", " print(i)\n", " if i == 10:\n", " break" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n", "10\n" ] } ], "source": [ "for i in li:\n", " print(i)\n", " if i == 10:\n", " break" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n", "10\n" ] } ], "source": [ "for i in t1:\n", " print(i)\n", " if i == 10:\n", " break" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "11\n", "12\n", "13\n", "14\n", "15\n", "16\n", "17\n", "18\n", "19\n", "20\n" ] } ], "source": [ "for i in t1:\n", " print(i)\n", " if i == 20:\n", " break" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Object Oriented Programming In Python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Attributes -> Variables created inside class\n", "- Method -> Function created inside the class\n", "- Class -> Blueprint for the object -> Design of a thing -> No memory\n", "- Object -> Instance of the class -> It occupies memory\n", "\n", "\n", "\n", "### Structure\n", "\n", "```python\n", "\n", "class class_name:\n", " \"\"\"Doc String\"\"\"\n", " Attributes\n", " Methods\n", "```" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [], "source": [ "class Student:\n", " \"\"\"This is Blueprint created for student Id card\"\"\"" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "unexpected EOF while parsing (, line 2)", "output_type": "error", "traceback": [ "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m2\u001b[0m\n\u001b[1;33m \u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m unexpected EOF while parsing\n" ] } ], "source": [ "class Student2:\n", " " ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [], "source": [ "class Student2:\n", " pass" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [], "source": [ "apssdc = Student()" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "<__main__.Student object at 0x000001EF79E04E20>\n" ] } ], "source": [ "print(apssdc)" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'This is Blueprint created for student Id card'" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "apssdc.__doc__" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'This is Blueprint created for student Id card'" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Student.__doc__" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [], "source": [ "class Student:\n", " \"\"\"This is Blueprint created for student Id card\"\"\"\n", " college = 'APSSDC'\n", " branch = 'Data Science'\n", " collegeAddress = 'Tadepalli, Guntur'" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [], "source": [ "std1 = Student()\n", "std2 = Student()" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'APSSDC'" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "std1.college" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'APSSDC'" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "std2.college" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Tadepalli, Guntur'" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "std1.collegeAddress" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [], "source": [ "class Student:\n", " \"\"\"This is Blueprint created for student Id card\"\"\"\n", " college = 'APSSDC'\n", " branch = 'Data Science'\n", " collegeAddress = 'Tadepalli, Guntur'\n", " def __init__(self): # dendur init, constructor, instance method, initializer\n", " print(\"Student object is created\")" ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Student object is created\n", "Student object is created\n" ] } ], "source": [ "std1 = Student()\n", "std2 = Student()" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'APSSDC'" ] }, "execution_count": 76, "metadata": {}, "output_type": "execute_result" } ], "source": [ "std1.college" ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [], "source": [ "class Student:\n", " \"\"\"This is Blueprint created for student Id card\"\"\"\n", " college = 'APSSDC'\n", " branch = 'Data Science'\n", " collegeAddress = 'Tadepalli, Guntur'\n", " def __init__(self, name, rollNo, mobileNo): # dendur init, constructor, instance method, initializer\n", " self.name = name\n", " self.rollNo = rollNo\n", " self.mobileNo = mobileNo\n", " print(\"{} object is created\".format(self.name))" ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Python object is created\n", "Data object is created\n" ] } ], "source": [ "std1 = Student('Python', '123456', '9876543210')\n", "std2 = Student('Data', '789987', '1234567890')" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Python'" ] }, "execution_count": 79, "metadata": {}, "output_type": "execute_result" } ], "source": [ "std1.name" ] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Data'" ] }, "execution_count": 80, "metadata": {}, "output_type": "execute_result" } ], "source": [ "std2.name" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "('APSSDC', 'APSSDC')" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ "std1.college, std2.college" ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "<__main__.Student object at 0x000001EF7BCC1730> <__main__.Student object at 0x000001EF7BCC1760>\n" ] } ], "source": [ "print(std1, std2)" ] }, { "cell_type": "code", "execution_count": 87, "metadata": {}, "outputs": [], "source": [ "class Student:\n", " \"\"\"This is Blueprint created for student Data\"\"\"\n", " college = 'APSSDC'\n", " branch = 'Data Science'\n", " collegeAddress = 'Tadepalli, Guntur'\n", " def __init__(self, name, rollNo, mobileNo, python, java): # dendur init, constructor, instance method, initializer\n", " self.name = name\n", " self.rollNo = rollNo\n", " self.mobileNo = mobileNo\n", " self.python = python\n", " self.java = java\n", " print(\"{} object is created\".format(self.name))\n", " \n", " def average(self):\n", " avg = (self.python + self.java) / 2\n", " return avg\n", " \n", " def py_status(self):\n", " if self.python >= 40:\n", " print(\"Hurry you have passed python subject\")\n", " else:\n", " print(\"Hurry you have failed python subject\")\n", " \n", " def java_status(self):\n", " if self.java >= 40:\n", " print(\"Hurry you have passed Java subject\")\n", " else:\n", " print(\"Hurry you have failed Java subject\")" ] }, { "cell_type": "code", "execution_count": 88, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Python object is created\n", "Data object is created\n" ] } ], "source": [ "std1 = Student('Python', '123456', '9876543210', 85, 65)\n", "std2 = Student('Data', '789987', '1234567890', 65, 85)" ] }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "75.0" ] }, "execution_count": 89, "metadata": {}, "output_type": "execute_result" } ], "source": [ "std1.average()" ] }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "75.0" ] }, "execution_count": 90, "metadata": {}, "output_type": "execute_result" } ], "source": [ "std2.average()" ] }, { "cell_type": "code", "execution_count": 93, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "85 65\n" ] } ], "source": [ "print(std1.python, std2.python)" ] }, { "cell_type": "code", "execution_count": 94, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hurry you have passed Java subject\n" ] } ], "source": [ "std1.java_status()" ] }, { "cell_type": "code", "execution_count": 95, "metadata": {}, "outputs": [], "source": [ "class Student:\n", " \"\"\"This is Blueprint created for student Data\"\"\"\n", " college = 'APSSDC'\n", " branch = 'Data Science'\n", " collegeAddress = 'Tadepalli, Guntur'\n", " def __init__(self, name, rollNo, mobileNo, python, java): # dendur init, constructor, instance method, initializer\n", " self.name = name\n", " self.rollNo = rollNo\n", " self.mobileNo = mobileNo\n", " self.python = python\n", " self.java = java\n", " print(\"{} object is created\".format(self.name))\n", " \n", " def average(self):\n", " avg = (self.python + self.java) / 2\n", " return avg\n", " \n", " def py_status(self, PyaddOn):\n", " self.python += PyaddOn\n", " if self.python >= 40:\n", " print(\"Hurry you have passed python subject\")\n", " else:\n", " print(\"Hurry you have failed python subject\")\n", " \n", " def java_status(self, JaddOn):\n", " self.java += JaddOn\n", " if self.java >= 40:\n", " print(\"Hurry you have passed Java subject\")\n", " else:\n", " print(\"Hurry you have failed Java subject\")" ] }, { "cell_type": "code", "execution_count": 96, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Python object is created\n", "Data object is created\n" ] } ], "source": [ "std1 = Student('Python', '123456', '9876543210', 35, 65)\n", "std2 = Student('Data', '789987', '1234567890', 65, 25)" ] }, { "cell_type": "code", "execution_count": 97, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hurry you have passed python subject\n" ] } ], "source": [ "std1.py_status(5)" ] }, { "cell_type": "code", "execution_count": 98, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "40" ] }, "execution_count": 98, "metadata": {}, "output_type": "execute_result" } ], "source": [ "std1.python" ] }, { "cell_type": "code", "execution_count": 99, "metadata": {}, "outputs": [ { "ename": "AttributeError", "evalue": "'Student' object has no attribute 'pyaddOn'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mstd1\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpyaddOn\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mAttributeError\u001b[0m: 'Student' object has no attribute 'pyaddOn'" ] } ], "source": [ "std1.pyaddOn" ] }, { "cell_type": "code", "execution_count": 100, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hurry you have passed python subject\n" ] } ], "source": [ "std2.py_status(0)" ] }, { "cell_type": "code", "execution_count": 101, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Python object is created\n", "Data object is created\n" ] } ], "source": [ "std1 = Student('Python', '123456', '9876543210', 35, 65)\n", "std2 = Student('Data', '789987', '1234567890', 65, 25)" ] }, { "cell_type": "code", "execution_count": 102, "metadata": {}, "outputs": [], "source": [ "std2.name = 'Data Science'" ] }, { "cell_type": "code", "execution_count": 103, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Data Science'" ] }, "execution_count": 103, "metadata": {}, "output_type": "execute_result" } ], "source": [ "std2.name" ] }, { "cell_type": "code", "execution_count": 104, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "<__main__.Student object at 0x000001EF7BCBC4F0> <__main__.Student object at 0x000001EF7BCBCFD0>\n" ] } ], "source": [ "print(std1, std2)" ] }, { "cell_type": "code", "execution_count": 108, "metadata": {}, "outputs": [], "source": [ "class Student:\n", " \"\"\"This is Blueprint created for student Data\"\"\"\n", " college = 'APSSDC'\n", " branch = 'Data Science'\n", " collegeAddress = 'Tadepalli, Guntur'\n", " def __init__(self, name, rollNo, mobileNo, python, java): # dendur init, constructor, instance method, initializer\n", " self.name = name\n", " self.rollNo = rollNo\n", " self.mobileNo = mobileNo\n", " self.python = python\n", " self.java = java\n", " print(\"{} object is created\".format(self.name))\n", " \n", " def average(self):\n", " avg = (self.python + self.java) / 2\n", " return avg\n", " \n", " def py_status(self, PyaddOn):\n", " self.python += PyaddOn\n", " if self.python >= 40:\n", " print(\"Hurry you have passed python subject\")\n", " else:\n", " print(\"Hurry you have failed python subject\")\n", " \n", " def java_status(self, JaddOn):\n", " self.java += JaddOn\n", " if self.java >= 40:\n", " print(\"Hurry you have passed Java subject\")\n", " else:\n", " print(\"Hurry you have failed Java subject\")\n", " def __str__(self):\n", " return \"This class belongs to {}\".format(self.name)" ] }, { "cell_type": "code", "execution_count": 109, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Python object is created\n", "Data object is created\n" ] } ], "source": [ "std1 = Student('Python', '123456', '9876543210', 35, 65)\n", "std2 = Student('Data', '789987', '1234567890', 65, 25)" ] }, { "cell_type": "code", "execution_count": 110, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This class belongs to Python This class belongs to Data\n" ] } ], "source": [ "print(std1, std2)" ] }, { "cell_type": "code", "execution_count": 119, "metadata": {}, "outputs": [], "source": [ "class Student:\n", " \"\"\"This is Blueprint created for student Data\"\"\"\n", " college = 'APSSDC'\n", " branch = 'Data Science'\n", " collegeAddress = 'Tadepalli, Guntur'\n", " def __init__(self, name, rollNo, mobileNo, python, java): # dendur init, constructor, instance method, initializer\n", " self.name = name\n", " self.rollNo = rollNo\n", " self.mobileNo = mobileNo\n", " self.python = python\n", " self.java = java\n", " print(\"{} object is created\".format(self.name))\n", " \n", " def average(self):\n", " avg = (self.python + self.java) / 2\n", " return avg\n", " \n", " def py_status(self, PyaddOn):\n", " self.python += PyaddOn\n", " if self.python >= 40:\n", " print(\"Hurry you have passed python subject\")\n", " else:\n", " print(\"Hurry you have failed python subject\")\n", " \n", " def java_status(self, JaddOn):\n", " self.java += JaddOn\n", " if self.java >= 40:\n", " print(\"Hurry you have passed Java subject\")\n", " else:\n", " print(\"Hurry you have failed Java subject\")\n", " def __str__(self):\n", " return \"This class belongs to {}\".format(self.name)\n", " def __del__(self): # destructor\n", " print(\"{} data has deleted\".format(self.name))" ] }, { "cell_type": "code", "execution_count": 121, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "data object is created\n", "Python data has deleted\n", "python object is created\n", "Data data has deleted\n" ] } ], "source": [ "std1 = Student('data', '123456', '9876543210', 35, 65)\n", "std2 = Student('python', '789987', '1234567890', 65, 25)" ] }, { "cell_type": "code", "execution_count": 114, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 114, "metadata": {}, "output_type": "execute_result" } ], "source": [ "std1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Types of methods\n", "\n", "\n", "- Instance Method\n", "- Class method\n", "- Static Method" ] }, { "cell_type": "code", "execution_count": 122, "metadata": {}, "outputs": [], "source": [ "class Student:\n", " \"\"\"This is Blueprint created for student Data\"\"\"\n", " college = 'APSSDC'\n", " branch = 'Data Science'\n", " collegeAddress = 'Tadepalli, Guntur'\n", " def __init__(self, name, rollNo, mobileNo, python, java): # dendur init, constructor, instance method, initializer\n", " self.name = name\n", " self.rollNo = rollNo\n", " self.mobileNo = mobileNo\n", " self.python = python\n", " self.java = java\n", " print(\"{} object is created\".format(self.name))\n", " \n", " def average(self):\n", " avg = (self.python + self.java) / 2\n", " return avg\n", " \n", " def py_status(self, PyaddOn):\n", " self.python += PyaddOn\n", " if self.python >= 40:\n", " print(\"Hurry you have passed python subject\")\n", " else:\n", " print(\"Hurry you have failed python subject\")\n", " \n", " def java_status(self, JaddOn):\n", " self.java += JaddOn\n", " if self.java >= 40:\n", " print(\"Hurry you have passed Java subject\")\n", " else:\n", " print(\"Hurry you have failed Java subject\")\n", " def __str__(self):\n", " return \"This class belongs to {}\".format(self.name)\n", " def __del__(self): # destructor\n", " print(\"{} data has deleted\".format(self.name))\n", " \n", " @staticmethod # Decorator\n", " def clgNo():\n", " return 1234567890" ] }, { "cell_type": "code", "execution_count": 123, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1234567890" ] }, "execution_count": 123, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Student.clgNo()" ] }, { "cell_type": "code", "execution_count": 124, "metadata": {}, "outputs": [], "source": [ "class Student:\n", " \"\"\"This is Blueprint created for student Data\"\"\"\n", " college = 'APSSDC'\n", " branch = 'Data Science'\n", " collegeAddress = 'Tadepalli, Guntur'\n", " def __init__(self, name, rollNo, mobileNo, python, java): # dendur init, constructor, instance method, initializer\n", " self.name = name\n", " self.rollNo = rollNo\n", " self.mobileNo = mobileNo\n", " self.python = python\n", " self.java = java\n", " print(\"{} object is created\".format(self.name))\n", " \n", " def average(self):\n", " avg = (self.python + self.java) / 2\n", " return avg\n", " \n", " def py_status(self, PyaddOn):\n", " self.python += PyaddOn\n", " if self.python >= 40:\n", " print(\"Hurry you have passed python subject\")\n", " else:\n", " print(\"Hurry you have failed python subject\")\n", " \n", " def java_status(self, JaddOn):\n", " self.java += JaddOn\n", " if self.java >= 40:\n", " print(\"Hurry you have passed Java subject\")\n", " else:\n", " print(\"Hurry you have failed Java subject\")\n", " def __str__(self):\n", " return \"This class belongs to {}\".format(self.name)\n", " def __del__(self): # destructor\n", " print(\"{} data has deleted\".format(self.name))\n", " \n", " @staticmethod # Decorator\n", " def clgNo():\n", " return 1234567890\n", " \n", " @classmethod\n", " def ClgName(cls):\n", " return cls.college" ] }, { "cell_type": "code", "execution_count": 126, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'APSSDC'" ] }, "execution_count": 126, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Student.ClgName()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Oops Concepts\n", "- Encapsulation\n", "- Polymorphism\n", "- Inheritance\n", " - Types\n", " - Multi Level\n", " - Hybrid\n", " - Multiple\n", " - Hierarchy\n", " - Operator Overloading\n", " - Method overwriting\n", "- Abstraction" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Today Outcomes\n", "\n", "- Iterator\n", "- Generator\n", "- OOP\n", " - Class\n", " - Object\n", " - Method\n", " - Instance\n", " - Static\n", " - Class\n", " - Attribute\n", " - Instance\n", " - Class" ] } ], "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.8.3" } }, "nbformat": 4, "nbformat_minor": 4 }