|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "04b39a3b-ead5-4103-b122-731e718b3aad", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "## Introduction to Object Oriented Programming (OOP) \n", |
| 9 | + "Object-Oriented Programming (OOP) is a powerful programming paradigm that allows developers to organize and structure their code in a more intuitive and modular way. It focuses on the creation of objects, which are instances of classes, and the interaction between these objects. Python, being a versatile and dynamic language, fully supports OOP principles and provides robust tools and features to implement them." |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "markdown", |
| 14 | + "id": "62472844-e3fe-4db9-a175-75918d2dd4b8", |
| 15 | + "metadata": {}, |
| 16 | + "source": [ |
| 17 | + "<center><img src=\"https://intellipaat.com/mediaFiles/2019/03/python10.png\" style=\"max-width:550px; height: auto\"></center> " |
| 18 | + ] |
| 19 | + }, |
| 20 | + { |
| 21 | + "cell_type": "markdown", |
| 22 | + "id": "eb175227-7e73-42fa-8c1b-7a55b025a568", |
| 23 | + "metadata": {}, |
| 24 | + "source": [ |
| 25 | + "**Why do we use object-oriented programming?**\n", |
| 26 | + "1. Modularity and code reusability.\n", |
| 27 | + "2. Abstraction and encapsulation.\n", |
| 28 | + "3. Organizing and maintaining code.\n", |
| 29 | + "4. Inheritance for code reuse.\n", |
| 30 | + "5. Flexibility through polymorphism.\n", |
| 31 | + "6. Collaboration in team development.\n", |
| 32 | + "7. Scalability and extensibility." |
| 33 | + ] |
| 34 | + }, |
| 35 | + { |
| 36 | + "cell_type": "markdown", |
| 37 | + "id": "7311184d-f8ce-4a69-a1dd-324ee0c6064c", |
| 38 | + "metadata": {}, |
| 39 | + "source": [ |
| 40 | + "### What is an Object?\n", |
| 41 | + "The object is an entity that has a state and behavior associated with it. It may be any real-world object like the mouse, keyboard, chair, table, pen, etc.\n", |
| 42 | + "\n", |
| 43 | + "Integers, strings, floating-point numbers, even arrays, and dictionaries, are all objects. More specifically, any single integer or any single string is an object. The number 12 is an object, the string\" Hello, world\" is an object, a list is an object that can hold other objects, and so on. You've been using objects all along and may not even realize it." |
| 44 | + ] |
| 45 | + }, |
| 46 | + { |
| 47 | + "cell_type": "markdown", |
| 48 | + "id": "1c4b376a-4c36-404e-a30c-2d2297f1eabd", |
| 49 | + "metadata": { |
| 50 | + "tags": [] |
| 51 | + }, |
| 52 | + "source": [ |
| 53 | + "### What is a Class?\n", |
| 54 | + "A class is a blueprint (a plan basically) that isused to define (bind together) a set of variables and methods (Characteristics) that are common to all objects of a particular kind. \n", |
| 55 | + "\n", |
| 56 | + "Example: If Car is a class, then Maruti 800 is an object of the Car class. All cars share similar features like wheels, 1 steeringwheel, windows, breaks, etc. Maruti 800 (the Car object) has all these features." |
| 57 | + ] |
| 58 | + }, |
| 59 | + { |
| 60 | + "cell_type": "markdown", |
| 61 | + "id": "64fc440a-b329-42b6-831a-b1762fcc92b7", |
| 62 | + "metadata": {}, |
| 63 | + "source": [ |
| 64 | + "### Classes vs Objects (Or Instances)" |
| 65 | + ] |
| 66 | + }, |
| 67 | + { |
| 68 | + "cell_type": "markdown", |
| 69 | + "id": "8fa9d0d9-4f2a-4867-b55a-deee3ec25905", |
| 70 | + "metadata": {}, |
| 71 | + "source": [ |
| 72 | + "* Classes are used to create user-defined data structures. Classes define functions called methods , which identify the behaviors and actions that an object created from the class can perform with its data. \n", |
| 73 | + "\n", |
| 74 | + "* In this module, you’ll create a Car class that stores some information about the characteristics and behaviors that an individual Car can have. \n", |
| 75 | + "\n", |
| 76 | + "* A class is a blueprint for how something should be defined. It doesn’t contain any data. The Car class specifies that a name and a top-speed are necessary for defining a Car , but it doesn’t contain the name or top-speed of any specific Car.\n", |
| 77 | + "\n", |
| 78 | + "* While the class is the blueprint, an instance is an object that is built from a class and contains real data. An instance of the Car class is not a blueprint anymore. It’s an actual car with a name , like Creta, and with a top speed of 200 Km/Hr.\n", |
| 79 | + "\n", |
| 80 | + "* Put another way, a class is like a form or a questionnaire. An instance is like a form that has been filled out with information. Just like many people can fill out the same form with their unique information, many instances can be created from a single class." |
| 81 | + ] |
| 82 | + }, |
| 83 | + { |
| 84 | + "cell_type": "markdown", |
| 85 | + "id": "5d83831c-9cbb-4f9f-b02e-41b321cbb28f", |
| 86 | + "metadata": {}, |
| 87 | + "source": [ |
| 88 | + "### Defining a Class in Python" |
| 89 | + ] |
| 90 | + }, |
| 91 | + { |
| 92 | + "cell_type": "code", |
| 93 | + "execution_count": 1, |
| 94 | + "id": "91347b3b-babd-4dd0-a08c-14d234f1c17e", |
| 95 | + "metadata": { |
| 96 | + "tags": [] |
| 97 | + }, |
| 98 | + "outputs": [], |
| 99 | + "source": [ |
| 100 | + "# Creating a car class\n", |
| 101 | + "class Car:\n", |
| 102 | + " pass" |
| 103 | + ] |
| 104 | + }, |
| 105 | + { |
| 106 | + "cell_type": "code", |
| 107 | + "execution_count": 2, |
| 108 | + "id": "bf8f2b81-76ff-4005-938b-4ad25e49a67f", |
| 109 | + "metadata": { |
| 110 | + "tags": [] |
| 111 | + }, |
| 112 | + "outputs": [], |
| 113 | + "source": [ |
| 114 | + "# Creating an instance (object) from that car class\n", |
| 115 | + "car1 = Car\n", |
| 116 | + "# Giving some attribute to the car object\n", |
| 117 | + "car1.name = \"Maruti 800\"\n", |
| 118 | + "car1.topspeed = 120" |
| 119 | + ] |
| 120 | + }, |
| 121 | + { |
| 122 | + "cell_type": "code", |
| 123 | + "execution_count": 3, |
| 124 | + "id": "1eb26f00-67b5-4bb2-a81a-11ea43ddaf54", |
| 125 | + "metadata": { |
| 126 | + "tags": [] |
| 127 | + }, |
| 128 | + "outputs": [ |
| 129 | + { |
| 130 | + "name": "stdout", |
| 131 | + "output_type": "stream", |
| 132 | + "text": [ |
| 133 | + "The name of the car is Maruti 800\n", |
| 134 | + "The topspeed of the car is 120 Km/h\n" |
| 135 | + ] |
| 136 | + } |
| 137 | + ], |
| 138 | + "source": [ |
| 139 | + "# Checking the attribute of the car object\n", |
| 140 | + "print(\"The name of the car is\", car1.name)\n", |
| 141 | + "print(\"The topspeed of the car is\", car1.topspeed, \"Km/h\")" |
| 142 | + ] |
| 143 | + }, |
| 144 | + { |
| 145 | + "cell_type": "code", |
| 146 | + "execution_count": 4, |
| 147 | + "id": "1e026e6a-c62d-47bd-a1fc-ebefea67e5c4", |
| 148 | + "metadata": { |
| 149 | + "tags": [] |
| 150 | + }, |
| 151 | + "outputs": [], |
| 152 | + "source": [ |
| 153 | + "# Creating another car object\n", |
| 154 | + "car2 = Car\n", |
| 155 | + "# Giving the attribute to the new car object\n", |
| 156 | + "car2.name = \"Creta\"\n", |
| 157 | + "car2.topspeed = 400\n", |
| 158 | + "car2.color = \"Yellow\"" |
| 159 | + ] |
| 160 | + }, |
| 161 | + { |
| 162 | + "cell_type": "code", |
| 163 | + "execution_count": 5, |
| 164 | + "id": "128d39c0-1aca-49dd-b0c7-3630746433ce", |
| 165 | + "metadata": { |
| 166 | + "tags": [] |
| 167 | + }, |
| 168 | + "outputs": [ |
| 169 | + { |
| 170 | + "name": "stdout", |
| 171 | + "output_type": "stream", |
| 172 | + "text": [ |
| 173 | + "The name of the new car is Creta\n", |
| 174 | + "The topseed of the new car is 400 Km/h\n", |
| 175 | + "The color of the new car is Yellow\n" |
| 176 | + ] |
| 177 | + } |
| 178 | + ], |
| 179 | + "source": [ |
| 180 | + "# Checking the attribute of the new car object\n", |
| 181 | + "print(\"The name of the new car is\", car2.name)\n", |
| 182 | + "print(\"The topseed of the new car is\", car2.topspeed, \"Km/h\")\n", |
| 183 | + "print(\"The color of the new car is\", car2.color)" |
| 184 | + ] |
| 185 | + } |
| 186 | + ], |
| 187 | + "metadata": { |
| 188 | + "kernelspec": { |
| 189 | + "display_name": "Python 3 (ipykernel)", |
| 190 | + "language": "python", |
| 191 | + "name": "python3" |
| 192 | + }, |
| 193 | + "language_info": { |
| 194 | + "codemirror_mode": { |
| 195 | + "name": "ipython", |
| 196 | + "version": 3 |
| 197 | + }, |
| 198 | + "file_extension": ".py", |
| 199 | + "mimetype": "text/x-python", |
| 200 | + "name": "python", |
| 201 | + "nbconvert_exporter": "python", |
| 202 | + "pygments_lexer": "ipython3", |
| 203 | + "version": "3.10.9" |
| 204 | + } |
| 205 | + }, |
| 206 | + "nbformat": 4, |
| 207 | + "nbformat_minor": 5 |
| 208 | +} |
0 commit comments