|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# OOPS:\n", |
| 8 | + "\n", |
| 9 | + "Object Oriented Programming or OOP is working with classes and objects. It involves interaction between objects having various attributes.\n", |
| 10 | + "\n", |
| 11 | + "Python is a object oriented programming language. This meand that all the types are representation or instance of a type class." |
| 12 | + ] |
| 13 | + }, |
| 14 | + { |
| 15 | + "cell_type": "markdown", |
| 16 | + "metadata": {}, |
| 17 | + "source": [ |
| 18 | + "## Components:\n", |
| 19 | + "1. Class\n", |
| 20 | + "2. Object\n" |
| 21 | + ] |
| 22 | + }, |
| 23 | + { |
| 24 | + "cell_type": "markdown", |
| 25 | + "metadata": {}, |
| 26 | + "source": [ |
| 27 | + "## Class:\n", |
| 28 | + "\n", |
| 29 | + "It is a structure or a building block (a code block) representing or defining the attributes (features and behaviour) of similar parameters.\n", |
| 30 | + "\n", |
| 31 | + "### Definition:\n", |
| 32 | + "Using `class` keyword.\n", |
| 33 | + "Name of a class should start with an uppercase letter.\n", |
| 34 | + "\n", |
| 35 | + "Syntax: `class Class_name`" |
| 36 | + ] |
| 37 | + }, |
| 38 | + { |
| 39 | + "cell_type": "code", |
| 40 | + "execution_count": 1, |
| 41 | + "metadata": {}, |
| 42 | + "outputs": [], |
| 43 | + "source": [ |
| 44 | + "class Student:\n", |
| 45 | + " roll_no = int()\n", |
| 46 | + " name = str()" |
| 47 | + ] |
| 48 | + }, |
| 49 | + { |
| 50 | + "cell_type": "markdown", |
| 51 | + "metadata": {}, |
| 52 | + "source": [ |
| 53 | + "## Object:\n", |
| 54 | + "\n", |
| 55 | + "It is an instance of a class.\n", |
| 56 | + "\n", |
| 57 | + "Syntax: `obj_name = Class_name()`" |
| 58 | + ] |
| 59 | + }, |
| 60 | + { |
| 61 | + "cell_type": "code", |
| 62 | + "execution_count": 2, |
| 63 | + "metadata": {}, |
| 64 | + "outputs": [], |
| 65 | + "source": [ |
| 66 | + "student1 = Student() # creating a new object\n", |
| 67 | + "\n", |
| 68 | + "student1.roll_no = 1\n", |
| 69 | + "student1.name = 'Prabhu'" |
| 70 | + ] |
| 71 | + }, |
| 72 | + { |
| 73 | + "cell_type": "code", |
| 74 | + "execution_count": 3, |
| 75 | + "metadata": {}, |
| 76 | + "outputs": [ |
| 77 | + { |
| 78 | + "name": "stdout", |
| 79 | + "output_type": "stream", |
| 80 | + "text": [ |
| 81 | + "Prabhu\n", |
| 82 | + "1\n" |
| 83 | + ] |
| 84 | + } |
| 85 | + ], |
| 86 | + "source": [ |
| 87 | + "print(student1.name)\n", |
| 88 | + "print(student1.roll_no)" |
| 89 | + ] |
| 90 | + }, |
| 91 | + { |
| 92 | + "cell_type": "markdown", |
| 93 | + "metadata": {}, |
| 94 | + "source": [ |
| 95 | + "## *self* Keyword:\n", |
| 96 | + "\n", |
| 97 | + "Calls the attributes for current instance or object.\n", |
| 98 | + "\n", |
| 99 | + "Syntax: `self.attribute_name`" |
| 100 | + ] |
| 101 | + }, |
| 102 | + { |
| 103 | + "cell_type": "markdown", |
| 104 | + "metadata": {}, |
| 105 | + "source": [ |
| 106 | + "## Defining attributes in a class:\n", |
| 107 | + "\n", |
| 108 | + "Attributes or Properties are defined using a constructor.\n", |
| 109 | + "\n", |
| 110 | + "### Constructor:\n", |
| 111 | + "Executes a set of code whenever a new object/instance is created.\n", |
| 112 | + "\n", |
| 113 | + "Defined by \\_\\_init\\_\\_ method." |
| 114 | + ] |
| 115 | + }, |
| 116 | + { |
| 117 | + "cell_type": "code", |
| 118 | + "execution_count": 7, |
| 119 | + "metadata": {}, |
| 120 | + "outputs": [], |
| 121 | + "source": [ |
| 122 | + "class Student:\n", |
| 123 | + " def __init__(self): # default constructor\n", |
| 124 | + " self.roll_no = 0\n", |
| 125 | + " self.name = 'Name'\n", |
| 126 | + "# print('__init__ file')\n", |
| 127 | + " \n", |
| 128 | + " def study():\n", |
| 129 | + " print('Studying....')" |
| 130 | + ] |
| 131 | + }, |
| 132 | + { |
| 133 | + "cell_type": "code", |
| 134 | + "execution_count": 8, |
| 135 | + "metadata": {}, |
| 136 | + "outputs": [ |
| 137 | + { |
| 138 | + "name": "stdout", |
| 139 | + "output_type": "stream", |
| 140 | + "text": [ |
| 141 | + "__init__ file\n", |
| 142 | + "Roll No: 1, Name: Ravi\n" |
| 143 | + ] |
| 144 | + } |
| 145 | + ], |
| 146 | + "source": [ |
| 147 | + "st1 = Student()\n", |
| 148 | + "st1.roll_no = 1\n", |
| 149 | + "st1.name = 'Ravi'\n", |
| 150 | + "print(f'Roll No: {st1.roll_no}, Name: {st1.name}')" |
| 151 | + ] |
| 152 | + }, |
| 153 | + { |
| 154 | + "cell_type": "code", |
| 155 | + "execution_count": 9, |
| 156 | + "metadata": {}, |
| 157 | + "outputs": [], |
| 158 | + "source": [ |
| 159 | + "class Student:\n", |
| 160 | + " def __init__(self, rn = 0, st_name = 'Name'): # Parametric Constructor\n", |
| 161 | + " self.roll_no = rn\n", |
| 162 | + " self.name = st_name\n", |
| 163 | + "# print('__init__ file')\n", |
| 164 | + " \n", |
| 165 | + " def study():\n", |
| 166 | + " print('Studying....')" |
| 167 | + ] |
| 168 | + }, |
| 169 | + { |
| 170 | + "cell_type": "code", |
| 171 | + "execution_count": 10, |
| 172 | + "metadata": {}, |
| 173 | + "outputs": [ |
| 174 | + { |
| 175 | + "name": "stdout", |
| 176 | + "output_type": "stream", |
| 177 | + "text": [ |
| 178 | + "Roll No: 2, Name: Rahul\n" |
| 179 | + ] |
| 180 | + } |
| 181 | + ], |
| 182 | + "source": [ |
| 183 | + "st2 = Student(2, 'Rahul')\n", |
| 184 | + "print(f'Roll No: {st2.roll_no}, Name: {st2.name}')" |
| 185 | + ] |
| 186 | + }, |
| 187 | + { |
| 188 | + "cell_type": "markdown", |
| 189 | + "metadata": {}, |
| 190 | + "source": [ |
| 191 | + "## destructor:\n", |
| 192 | + "Delete the current instance of class or object.\n", |
| 193 | + "\n", |
| 194 | + "Use \\_\\_del\\_\\_ method" |
| 195 | + ] |
| 196 | + }, |
| 197 | + { |
| 198 | + "cell_type": "code", |
| 199 | + "execution_count": null, |
| 200 | + "metadata": {}, |
| 201 | + "outputs": [], |
| 202 | + "source": [] |
| 203 | + } |
| 204 | + ], |
| 205 | + "metadata": { |
| 206 | + "kernelspec": { |
| 207 | + "display_name": "Python 3", |
| 208 | + "language": "python", |
| 209 | + "name": "python3" |
| 210 | + }, |
| 211 | + "language_info": { |
| 212 | + "codemirror_mode": { |
| 213 | + "name": "ipython", |
| 214 | + "version": 3 |
| 215 | + }, |
| 216 | + "file_extension": ".py", |
| 217 | + "mimetype": "text/x-python", |
| 218 | + "name": "python", |
| 219 | + "nbconvert_exporter": "python", |
| 220 | + "pygments_lexer": "ipython3", |
| 221 | + "version": "3.8.6" |
| 222 | + } |
| 223 | + }, |
| 224 | + "nbformat": 4, |
| 225 | + "nbformat_minor": 4 |
| 226 | +} |
0 commit comments