|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "c4ff441a", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# Classes" |
| 9 | + ] |
| 10 | + }, |
| 11 | + { |
| 12 | + "cell_type": "markdown", |
| 13 | + "id": "54ceb1e6", |
| 14 | + "metadata": {}, |
| 15 | + "source": [ |
| 16 | + "A Python class is a blueprint for creating objects that bundle data (attributes) and behavior (methods). It defines a custom type by specifying the structure and functionality that its instances will have, supporting encapsulation and reuse. Classes enable object-oriented features like inheritance and polymorphism, allowing extension and customization of behavior." |
| 17 | + ] |
| 18 | + }, |
| 19 | + { |
| 20 | + "cell_type": "markdown", |
| 21 | + "id": "2c3fcdc8", |
| 22 | + "metadata": {}, |
| 23 | + "source": [ |
| 24 | + "## Defining a class" |
| 25 | + ] |
| 26 | + }, |
| 27 | + { |
| 28 | + "cell_type": "code", |
| 29 | + "execution_count": 9, |
| 30 | + "id": "bdd215a1", |
| 31 | + "metadata": {}, |
| 32 | + "outputs": [ |
| 33 | + { |
| 34 | + "name": "stdout", |
| 35 | + "output_type": "stream", |
| 36 | + "text": [ |
| 37 | + " 2023 Volvo XC90\n", |
| 38 | + " 2023 Volvo XC60\n" |
| 39 | + ] |
| 40 | + } |
| 41 | + ], |
| 42 | + "source": [ |
| 43 | + "class Car:\n", |
| 44 | + " def __init__(self, make, model, year):\n", |
| 45 | + " self.make = make\n", |
| 46 | + " self.model = model\n", |
| 47 | + " self.year = year\n", |
| 48 | + " \n", |
| 49 | + " def display(self):\n", |
| 50 | + " print(f\" {self.year} {self.make} {self.model}\")\n", |
| 51 | + "\n", |
| 52 | + "if __name__ == '__main__':\n", |
| 53 | + " car = Car(\"Volvo\", \"XC90\", \"2023\")\n", |
| 54 | + " car.display()\n", |
| 55 | + " car.model = \"XC60\"\n", |
| 56 | + " car.display()" |
| 57 | + ] |
| 58 | + }, |
| 59 | + { |
| 60 | + "cell_type": "markdown", |
| 61 | + "id": "a9c324ce", |
| 62 | + "metadata": {}, |
| 63 | + "source": [ |
| 64 | + "* **__init__()** : is the instance initializer that runs automatically after a new object is created typically used to set up initial state (attributes) for that instance.\n", |
| 65 | + "* **self** : is the reference to the current object, passed as the first parameter to instance methods so you can access and modify the object attributes and call its other methods." |
| 66 | + ] |
| 67 | + } |
| 68 | + ], |
| 69 | + "metadata": { |
| 70 | + "kernelspec": { |
| 71 | + "display_name": "notes (3.12.0)", |
| 72 | + "language": "python", |
| 73 | + "name": "python3" |
| 74 | + }, |
| 75 | + "language_info": { |
| 76 | + "codemirror_mode": { |
| 77 | + "name": "ipython", |
| 78 | + "version": 3 |
| 79 | + }, |
| 80 | + "file_extension": ".py", |
| 81 | + "mimetype": "text/x-python", |
| 82 | + "name": "python", |
| 83 | + "nbconvert_exporter": "python", |
| 84 | + "pygments_lexer": "ipython3", |
| 85 | + "version": "3.12.0" |
| 86 | + } |
| 87 | + }, |
| 88 | + "nbformat": 4, |
| 89 | + "nbformat_minor": 5 |
| 90 | +} |
0 commit comments