├── 1.Features & Variables.pptx ├── 2.Operators.pptx ├── 3.Data types.pptx ├── 4.Flow control.pptx ├── 5.Functions.pptx ├── 6.Exceptions.pptx ├── 7.File Handling.pptx ├── 8.Modules.pptx ├── Python Keywords and their uses.pdf ├── README.md └── oops.ipynb /1.Features & Variables.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nagarajuekkirala/Python_Training/b1cd6903abf78767c5b5f2c3b70299a060d0e0ed/1.Features & Variables.pptx -------------------------------------------------------------------------------- /2.Operators.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nagarajuekkirala/Python_Training/b1cd6903abf78767c5b5f2c3b70299a060d0e0ed/2.Operators.pptx -------------------------------------------------------------------------------- /3.Data types.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nagarajuekkirala/Python_Training/b1cd6903abf78767c5b5f2c3b70299a060d0e0ed/3.Data types.pptx -------------------------------------------------------------------------------- /4.Flow control.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nagarajuekkirala/Python_Training/b1cd6903abf78767c5b5f2c3b70299a060d0e0ed/4.Flow control.pptx -------------------------------------------------------------------------------- /5.Functions.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nagarajuekkirala/Python_Training/b1cd6903abf78767c5b5f2c3b70299a060d0e0ed/5.Functions.pptx -------------------------------------------------------------------------------- /6.Exceptions.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nagarajuekkirala/Python_Training/b1cd6903abf78767c5b5f2c3b70299a060d0e0ed/6.Exceptions.pptx -------------------------------------------------------------------------------- /7.File Handling.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nagarajuekkirala/Python_Training/b1cd6903abf78767c5b5f2c3b70299a060d0e0ed/7.File Handling.pptx -------------------------------------------------------------------------------- /8.Modules.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nagarajuekkirala/Python_Training/b1cd6903abf78767c5b5f2c3b70299a060d0e0ed/8.Modules.pptx -------------------------------------------------------------------------------- /Python Keywords and their uses.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nagarajuekkirala/Python_Training/b1cd6903abf78767c5b5f2c3b70299a060d0e0ed/Python Keywords and their uses.pdf -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 🎈Python is a high-level, interpreted, and versatile programming language that has gained immense popularity since its inception in the late 1980s. Developed by Guido van Rossum, Python has emerged as a powerful tool for developers, data scientists, and enthusiasts alike. This essay explores the features, applications, and advantages of Python, highlighting its significance in various fields. 2 | 3 | 🏆Simplicity and Readability: 4 | One of Python's defining characteristics is its simplicity and readability. The language emphasizes clean and intuitive syntax, making it easy for beginners to learn and understand. Python's code readability enables developers to write concise and expressive programs, enhancing productivity and reducing the time required for development and maintenance. 5 | 6 | 🔍Versatility: 7 | Python's versatility is another reason for its widespread adoption. It supports multiple programming paradigms, including procedural, object-oriented, and functional programming, allowing developers to choose the most appropriate approach for their projects. This flexibility enables Python to be used in various domains, such as web development, scientific computing, artificial intelligence, and data analysis. 8 | 9 | ✔Extensive Libraries and Frameworks: 10 | Python boasts an extensive collection of libraries and frameworks, which serve as building blocks for developers. The Python Standard Library provides a wide range of modules for tasks like file I/O, networking, and regular expressions. Additionally, popular libraries like NumPy, Pandas, Matplotlib, and TensorFlow offer specialized functionality for scientific computing, data manipulation, visualization, and machine learning. These libraries simplify complex tasks and accelerate development, making Python a go-to language for data scientists and researchers. 11 | 12 | 💎 Web Development: 13 | Python's simplicity and robust frameworks, such as Django and Flask, make it an excellent choice for web development. Django, a high-level web framework, provides a full-featured toolkit for building scalable and secure web applications. Flask, on the other hand, is a lightweight framework that enables rapid development and flexibility. Python's wide range of web development tools, combined with its readability, allows developers to create dynamic and efficient web applications with ease. 14 | 15 | 💎Data Analysis and Visualization: 16 | Python has emerged as a leading language for data analysis and visualization. With libraries like Pandas and NumPy, Python provides powerful tools for handling and manipulating large datasets. These libraries offer efficient data structures and functions for data cleaning, transformation, and analysis. Matplotlib, along with libraries such as Seaborn and Plotly, facilitates the creation of visually appealing charts, graphs, and plots. Python's dominance in data analysis is further solidified by Jupyter Notebook, a popular environment for interactive data exploration and storytelling. 17 | 18 | 💎 Artificial Intelligence and Machine Learning: 19 | Python has become the de facto language for artificial intelligence (AI) and machine learning (ML). Libraries like TensorFlow, Keras, and PyTorch provide comprehensive frameworks for building and training neural networks and deep learning models. The simplicity and readability of Python allow researchers and developers to experiment with complex AI algorithms, implement state-of-the-art models, and deploy them in production environments. Python's support for AI and ML has revolutionized industries ranging from healthcare and finance to autonomous vehicles and natural language processing. 20 | 21 | Conclusion: 22 | Python's simplicity, versatility, and extensive ecosystem of libraries and frameworks have made it an indispensable language for developers across various domains. From web development and data analysis to AI and machine learning, Python empowers programmers to create robust and efficient solutions. As the demand for data-driven applications and AI technologies continues to grow, Python's relevance and influence are expected to expand even further, solidifying its place as a leading programming language in the years to come. 23 | -------------------------------------------------------------------------------- /oops.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 19, 6 | "id": "8e4a08ac", 7 | "metadata": {}, 8 | "outputs": [ 9 | { 10 | "name": "stdout", 11 | "output_type": "stream", 12 | "text": [ 13 | "John\n", 14 | "30\n", 15 | "Hello, my name is John.\n", 16 | "Happy birthday! I am now 31 years old.\n" 17 | ] 18 | } 19 | ], 20 | "source": [ 21 | "class Person:\n", 22 | " def __init__(self, name, age):\n", 23 | " self.name = name\n", 24 | " self.age = age\n", 25 | "\n", 26 | " def say_hello(self):\n", 27 | " print(f\"Hello, my name is {self.name}.\")\n", 28 | "\n", 29 | " def celebrate_birthday(self):\n", 30 | " self.age += 1\n", 31 | " print(f\"Happy birthday! I am now {self.age} years old.\")\n", 32 | "\n", 33 | "\n", 34 | "# Create an instance of the Person class\n", 35 | "person = Person(\"John\", 30)\n", 36 | "\n", 37 | "# Access instance variables\n", 38 | "print(person.name) # Output: John\n", 39 | "print(person.age) # Output: 30\n", 40 | "\n", 41 | "# Call instance methods\n", 42 | "person.say_hello() # Output: Hello, my name is John.\n", 43 | "person.celebrate_birthday() # Output: Happy birthday! I am now 31 years old.\n" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": 20, 49 | "id": "3d5322d5", 50 | "metadata": {}, 51 | "outputs": [], 52 | "source": [ 53 | "class Bank:\n", 54 | " def __init__(self,name,accountno,balance):\n", 55 | " self.name = name\n", 56 | " self.accountno = accountno\n", 57 | " self.balance = balance\n", 58 | " \n", 59 | " def withdraw(self):\n", 60 | " amount = int(input(\"Enter amount to withdraw: \"))\n", 61 | " self.balance = self.balance - amount\n", 62 | " print(\"Balance in your account is\",self.balance)\n", 63 | " def deposit(self):\n", 64 | " amount = int(input(\"Enter amount to deposit: \"))\n", 65 | " self.balance = self.balance + amount\n", 66 | " print(\"Balance in your account is\",self.balance)\n", 67 | "\n", 68 | " def display(self):\n", 69 | " print(\"Balance in your account is\",self.balance)\n", 70 | " \n", 71 | "st = Bank(\"Rajesh\",201010,60000)" 72 | ] 73 | }, 74 | { 75 | "cell_type": "code", 76 | "execution_count": 5, 77 | "id": "931870e0", 78 | "metadata": {}, 79 | "outputs": [ 80 | { 81 | "name": "stdout", 82 | "output_type": "stream", 83 | "text": [ 84 | "Balance in your account is 60000\n" 85 | ] 86 | } 87 | ], 88 | "source": [ 89 | "st.display()" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": 6, 95 | "id": "76d58658", 96 | "metadata": {}, 97 | "outputs": [ 98 | { 99 | "name": "stdout", 100 | "output_type": "stream", 101 | "text": [ 102 | "Enter amount to deposit: 5000\n", 103 | "Balance in your account is 65000\n" 104 | ] 105 | } 106 | ], 107 | "source": [ 108 | "st.deposit()" 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": 7, 114 | "id": "bd034069", 115 | "metadata": {}, 116 | "outputs": [ 117 | { 118 | "name": "stdout", 119 | "output_type": "stream", 120 | "text": [ 121 | "Enter amount to withdraw: 50000\n", 122 | "Balance in your account is 15000\n" 123 | ] 124 | } 125 | ], 126 | "source": [ 127 | "st.withdraw()" 128 | ] 129 | }, 130 | { 131 | "cell_type": "markdown", 132 | "id": "6752a056", 133 | "metadata": {}, 134 | "source": [ 135 | "# Inheritance" 136 | ] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "execution_count": 8, 141 | "id": "867a7496", 142 | "metadata": {}, 143 | "outputs": [], 144 | "source": [ 145 | "# Base class\n", 146 | "class Animal:\n", 147 | " def __init__(self, name):\n", 148 | " self.name = name\n", 149 | "\n", 150 | " def eat(self):\n", 151 | " print(f\"{self.name} is eating.\")\n", 152 | "\n", 153 | " def sleep(self):\n", 154 | " print(f\"{self.name} is sleeping.\")" 155 | ] 156 | }, 157 | { 158 | "cell_type": "markdown", 159 | "id": "7341ffb3", 160 | "metadata": {}, 161 | "source": [ 162 | "Animal serves as the base class, \n", 163 | "which has common attributes and methods for all animals." 164 | ] 165 | }, 166 | { 167 | "cell_type": "code", 168 | "execution_count": 9, 169 | "id": "2a22c854", 170 | "metadata": {}, 171 | "outputs": [], 172 | "source": [ 173 | "\n", 174 | "# Single inheritance\n", 175 | "class Dog(Animal):\n", 176 | " def bark(self):\n", 177 | " print(f\"{self.name} is barking.\")\n" 178 | ] 179 | }, 180 | { 181 | "cell_type": "code", 182 | "execution_count": 14, 183 | "id": "95406950", 184 | "metadata": {}, 185 | "outputs": [ 186 | { 187 | "name": "stdout", 188 | "output_type": "stream", 189 | "text": [ 190 | "Buddy is eating.\n", 191 | "Buddy is sleeping.\n", 192 | "Buddy is barking.\n" 193 | ] 194 | } 195 | ], 196 | "source": [ 197 | "# Create instances and demonstrate inheritance\n", 198 | "dog = Dog(\"Buddy\")\n", 199 | "dog.eat()\n", 200 | "dog.sleep()\n", 201 | "dog.bark()" 202 | ] 203 | }, 204 | { 205 | "cell_type": "markdown", 206 | "id": "5e514cbb", 207 | "metadata": {}, 208 | "source": [ 209 | "Dog inherits from Animal using single inheritance.\n", 210 | "It adds a specific method bark to the base class" 211 | ] 212 | }, 213 | { 214 | "cell_type": "code", 215 | "execution_count": 10, 216 | "id": "0e7ed5b0", 217 | "metadata": {}, 218 | "outputs": [], 219 | "source": [ 220 | "# Multiple inheritance\n", 221 | "class Eagle(Animal):\n", 222 | " def fly(self):\n", 223 | " print(f\"{self.name} is flying.\")\n", 224 | "\n", 225 | "\n", 226 | "class Predator:\n", 227 | " def hunt(self):\n", 228 | " print(f\"{self.name} is hunting.\")\n", 229 | "\n", 230 | "\n", 231 | "class FlyingPredator(Eagle, Predator):\n", 232 | " def __init__(self, name):\n", 233 | " super().__init__(name)" 234 | ] 235 | }, 236 | { 237 | "cell_type": "code", 238 | "execution_count": 15, 239 | "id": "c86d4f08", 240 | "metadata": {}, 241 | "outputs": [ 242 | { 243 | "name": "stdout", 244 | "output_type": "stream", 245 | "text": [ 246 | "Aquila is eating.\n", 247 | "Aquila is sleeping.\n", 248 | "Aquila is flying.\n" 249 | ] 250 | } 251 | ], 252 | "source": [ 253 | "eagle = Eagle(\"Aquila\")\n", 254 | "eagle.eat()\n", 255 | "eagle.sleep()\n", 256 | "eagle.fly()" 257 | ] 258 | }, 259 | { 260 | "cell_type": "code", 261 | "execution_count": 16, 262 | "id": "2a07a4bc", 263 | "metadata": {}, 264 | "outputs": [ 265 | { 266 | "name": "stdout", 267 | "output_type": "stream", 268 | "text": [ 269 | "Hawk is eating.\n", 270 | "Hawk is sleeping.\n", 271 | "Hawk is flying.\n", 272 | "Hawk is hunting.\n" 273 | ] 274 | } 275 | ], 276 | "source": [ 277 | "flying_predator = FlyingPredator(\"Hawk\")\n", 278 | "flying_predator.eat()\n", 279 | "flying_predator.sleep()\n", 280 | "flying_predator.fly()\n", 281 | "flying_predator.hunt()" 282 | ] 283 | }, 284 | { 285 | "cell_type": "markdown", 286 | "id": "c6478f8a", 287 | "metadata": {}, 288 | "source": [ 289 | "Eagle also inherits from Animal using single inheritance. It adds a specific method fly to the base class." 290 | ] 291 | }, 292 | { 293 | "cell_type": "code", 294 | "execution_count": 11, 295 | "id": "b55cb9b1", 296 | "metadata": {}, 297 | "outputs": [], 298 | "source": [ 299 | "\n", 300 | "# Multilevel inheritance\n", 301 | "class GermanShepherd(Dog):\n", 302 | " def __init__(self, name):\n", 303 | " super().__init__(name)\n", 304 | "\n", 305 | " def guard(self):\n", 306 | " print(f\"{self.name} is guarding.\")" 307 | ] 308 | }, 309 | { 310 | "cell_type": "code", 311 | "execution_count": 17, 312 | "id": "db9dfea3", 313 | "metadata": {}, 314 | "outputs": [ 315 | { 316 | "name": "stdout", 317 | "output_type": "stream", 318 | "text": [ 319 | "Rocky is eating.\n", 320 | "Rocky is sleeping.\n", 321 | "Rocky is barking.\n", 322 | "Rocky is guarding.\n" 323 | ] 324 | } 325 | ], 326 | "source": [ 327 | "\n", 328 | "german_shepherd = GermanShepherd(\"Rocky\")\n", 329 | "german_shepherd.eat()\n", 330 | "german_shepherd.sleep()\n", 331 | "german_shepherd.bark()\n", 332 | "german_shepherd.guard()" 333 | ] 334 | }, 335 | { 336 | "cell_type": "markdown", 337 | "id": "27c2a73f", 338 | "metadata": {}, 339 | "source": [ 340 | "GermanShepherd showcases multilevel inheritance by inheriting from Dog, which itself inherits from Animal. It adds a specific method guard" 341 | ] 342 | }, 343 | { 344 | "cell_type": "code", 345 | "execution_count": 12, 346 | "id": "8a14f3f6", 347 | "metadata": {}, 348 | "outputs": [], 349 | "source": [ 350 | "\n", 351 | "# Hierarchical inheritance\n", 352 | "class Cat(Animal):\n", 353 | " def meow(self):\n", 354 | " print(f\"{self.name} is meowing.\")" 355 | ] 356 | }, 357 | { 358 | "cell_type": "code", 359 | "execution_count": 18, 360 | "id": "fd242add", 361 | "metadata": {}, 362 | "outputs": [ 363 | { 364 | "name": "stdout", 365 | "output_type": "stream", 366 | "text": [ 367 | "Simba is eating.\n", 368 | "Simba is sleeping.\n", 369 | "Simba is meowing.\n", 370 | "Simba is hunting.\n" 371 | ] 372 | } 373 | ], 374 | "source": [ 375 | "lion = Lion(\"Simba\")\n", 376 | "lion.eat()\n", 377 | "lion.sleep()\n", 378 | "lion.meow()\n", 379 | "lion.hunt()" 380 | ] 381 | }, 382 | { 383 | "cell_type": "markdown", 384 | "id": "f7b62a8c", 385 | "metadata": {}, 386 | "source": [ 387 | "Lion demonstrates hybrid inheritance by inheriting from both Cat and Predator. It uses super() to initialize the inherited classes." 388 | ] 389 | }, 390 | { 391 | "cell_type": "markdown", 392 | "id": "7ebcbfdc", 393 | "metadata": {}, 394 | "source": [ 395 | "# PolyMorphism" 396 | ] 397 | }, 398 | { 399 | "cell_type": "markdown", 400 | "id": "56eabb0f", 401 | "metadata": {}, 402 | "source": [ 403 | "one name, many forms" 404 | ] 405 | }, 406 | { 407 | "cell_type": "markdown", 408 | "id": "3e51c8f4", 409 | "metadata": {}, 410 | "source": [ 411 | "Polymorphism is a concept in programming that allows objects of different types to be treated as if they belong to a common type. In simple terms, it means that different objects can respond to the same method or function call in different ways." 412 | ] 413 | }, 414 | { 415 | "cell_type": "code", 416 | "execution_count": 21, 417 | "id": "4981c3e7", 418 | "metadata": {}, 419 | "outputs": [ 420 | { 421 | "name": "stdout", 422 | "output_type": "stream", 423 | "text": [ 424 | "Woof!\n", 425 | "Meow!\n", 426 | "Moo!\n" 427 | ] 428 | } 429 | ], 430 | "source": [ 431 | "class Dog:\n", 432 | " def make_sound(self):\n", 433 | " print(\"Woof!\")\n", 434 | "\n", 435 | "class Cat:\n", 436 | " def make_sound(self):\n", 437 | " print(\"Meow!\")\n", 438 | "\n", 439 | "class Cow:\n", 440 | " def make_sound(self):\n", 441 | " print(\"Moo!\")\n", 442 | "\n", 443 | "def make_sound(animal):\n", 444 | " animal.make_sound()\n", 445 | "\n", 446 | "dog = Dog()\n", 447 | "cat = Cat()\n", 448 | "cow = Cow()\n", 449 | "\n", 450 | "make_sound(dog) # Output: Woof!\n", 451 | "make_sound(cat) # Output: Meow!\n", 452 | "make_sound(cow) # Output: Moo!\n" 453 | ] 454 | }, 455 | { 456 | "cell_type": "markdown", 457 | "id": "f03bdc8b", 458 | "metadata": {}, 459 | "source": [ 460 | "In this example, we have three different classes: Dog, Cat, and Cow, each with its own implementation of the make_sound() method. The make_sound() function accepts an animal argument and calls the make_sound() method on that object." 461 | ] 462 | }, 463 | { 464 | "cell_type": "markdown", 465 | "id": "2f36a6f8", 466 | "metadata": {}, 467 | "source": [ 468 | "# Encapsulation" 469 | ] 470 | }, 471 | { 472 | "cell_type": "markdown", 473 | "id": "57d62153", 474 | "metadata": {}, 475 | "source": [ 476 | "Encapsulation is one of the fundamental principles of object-oriented programming (OOP) that focuses on bundling data and methods together within a class, hiding the internal details and providing a public interface to interact with the object" 477 | ] 478 | }, 479 | { 480 | "cell_type": "markdown", 481 | "id": "679aaa65", 482 | "metadata": {}, 483 | "source": [ 484 | " It promotes the concept of data hiding and information hiding." 485 | ] 486 | }, 487 | { 488 | "cell_type": "raw", 489 | "id": "dc79b183", 490 | "metadata": {}, 491 | "source": [ 492 | "In Python, encapsulation is achieved through the use of access modifiers and property methods.\n", 493 | "\n", 494 | "Access modifiers:\n", 495 | "Python does not have strict access modifiers like some other languages (e.g., Java). However, there are conventions to indicate the level of access to class members.\n", 496 | "\n", 497 | "Public: Class members are public by default and can be accessed from anywhere. They are denoted by regular variable or method names.\n", 498 | "Protected: Class members that are intended to be accessed within the class or its subclasses are denoted by a single underscore prefix (e.g., _variable, _method()). Although they can be accessed from outside the class, it's considered a convention not to do so.\n", 499 | "Private: Class members that are intended to be accessed only within the class itself are denoted by a double underscore prefix (e.g., __variable, __method()). They are name-mangled to avoid naming conflicts in subclasses." 500 | ] 501 | }, 502 | { 503 | "cell_type": "code", 504 | "execution_count": 22, 505 | "id": "15877632", 506 | "metadata": {}, 507 | "outputs": [ 508 | { 509 | "name": "stdout", 510 | "output_type": "stream", 511 | "text": [ 512 | "John\n", 513 | "30\n", 514 | "30\n", 515 | "Name: John, Age: 35\n" 516 | ] 517 | } 518 | ], 519 | "source": [ 520 | "class Person:\n", 521 | " def __init__(self, name, age):\n", 522 | " self._name = name\n", 523 | " self.__age = age\n", 524 | "\n", 525 | " def get_age(self):\n", 526 | " return self.__age\n", 527 | "\n", 528 | " def set_age(self, age):\n", 529 | " if age > 0:\n", 530 | " self.__age = age\n", 531 | "\n", 532 | " def display_info(self):\n", 533 | " print(f\"Name: {self._name}, Age: {self.__age}\")\n", 534 | "\n", 535 | "\n", 536 | "# Create an instance of the Person class\n", 537 | "person = Person(\"John\", 30)\n", 538 | "\n", 539 | "# Access the public attribute directly\n", 540 | "print(person._name) # Output: John\n", 541 | "\n", 542 | "# Access the protected attribute (convention, not enforced)\n", 543 | "print(person._Person__age) # Output: 30\n", 544 | "\n", 545 | "# Access the private attribute (name-mangled)\n", 546 | "# print(person.__age) # Raises an AttributeError\n", 547 | "\n", 548 | "# Access the attribute using a getter method\n", 549 | "print(person.get_age()) # Output: 30\n", 550 | "\n", 551 | "# Modify the attribute using a setter method\n", 552 | "person.set_age(35)\n", 553 | "\n", 554 | "# Call a method that displays the person's information\n", 555 | "person.display_info() # Output: Name: John, Age: 35\n" 556 | ] 557 | }, 558 | { 559 | "cell_type": "markdown", 560 | "id": "c07fb79c", 561 | "metadata": {}, 562 | "source": [ 563 | "# ABSTRACTION" 564 | ] 565 | }, 566 | { 567 | "cell_type": "markdown", 568 | "id": "9c6e3fc3", 569 | "metadata": {}, 570 | "source": [ 571 | "Abstraction is a fundamental concept in object-oriented programming that focuses on creating abstract classes or interfaces to define common behaviors and characteristics of related objects. It allows us to define a blueprint or contract for objects without specifying the implementation details. In Python, abstraction is typically achieved using abstract base classes (ABCs) and abstract methods." 572 | ] 573 | }, 574 | { 575 | "cell_type": "markdown", 576 | "id": "77a775ce", 577 | "metadata": {}, 578 | "source": [ 579 | "Abstract Base Classes (ABCs):\n", 580 | "Python provides the abc module to define abstract base classes. An abstract base class cannot be instantiated directly; instead, it serves as a blueprint for subclasses to inherit from. It defines a set of methods and attributes that subclasses must implement." 581 | ] 582 | }, 583 | { 584 | "cell_type": "markdown", 585 | "id": "2bee616c", 586 | "metadata": {}, 587 | "source": [ 588 | "To create an abstract base class, you can use the ABC class as a base and decorate abstract methods using the @abstractmethod decorator. Subclasses are then required to provide implementations for these abstract methods." 589 | ] 590 | }, 591 | { 592 | "cell_type": "code", 593 | "execution_count": 24, 594 | "id": "7f9c5d59", 595 | "metadata": {}, 596 | "outputs": [ 597 | { 598 | "name": "stdout", 599 | "output_type": "stream", 600 | "text": [ 601 | "15\n", 602 | "16\n", 603 | "50.24\n", 604 | "25.12\n" 605 | ] 606 | } 607 | ], 608 | "source": [ 609 | "from abc import ABC, abstractmethod\n", 610 | "\n", 611 | "class Shape(ABC):\n", 612 | " @abstractmethod\n", 613 | " def area(self):\n", 614 | " pass\n", 615 | "\n", 616 | " @abstractmethod\n", 617 | " def perimeter(self):\n", 618 | " pass\n", 619 | "\n", 620 | "class Rectangle(Shape):\n", 621 | " def __init__(self, length, width):\n", 622 | " self.length = length\n", 623 | " self.width = width\n", 624 | "\n", 625 | " def area(self):\n", 626 | " return self.length * self.width\n", 627 | "\n", 628 | " def perimeter(self):\n", 629 | " return 2 * (self.length + self.width)\n", 630 | "\n", 631 | "class Circle(Shape):\n", 632 | " def __init__(self, radius):\n", 633 | " self.radius = radius\n", 634 | "\n", 635 | " def area(self):\n", 636 | " return 3.14 * self.radius * self.radius\n", 637 | "\n", 638 | " def perimeter(self):\n", 639 | " return 2 * 3.14 * self.radius\n", 640 | "\n", 641 | "\n", 642 | "# Uncommenting the following line will raise an error,\n", 643 | "# as Shape is an abstract base class and cannot be instantiated directly.\n", 644 | "# shape = Shape()\n", 645 | "\n", 646 | "rectangle = Rectangle(5, 3)\n", 647 | "print(rectangle.area()) # Output: 15\n", 648 | "print(rectangle.perimeter()) # Output: 16\n", 649 | "\n", 650 | "circle = Circle(4)\n", 651 | "print(circle.area()) # Output: 50.24\n", 652 | "print(circle.perimeter()) # Output: 25.12\n" 653 | ] 654 | }, 655 | { 656 | "cell_type": "code", 657 | "execution_count": null, 658 | "id": "e8ea17f1", 659 | "metadata": {}, 660 | "outputs": [], 661 | "source": [] 662 | } 663 | ], 664 | "metadata": { 665 | "kernelspec": { 666 | "display_name": "Python 3 (ipykernel)", 667 | "language": "python", 668 | "name": "python3" 669 | }, 670 | "language_info": { 671 | "codemirror_mode": { 672 | "name": "ipython", 673 | "version": 3 674 | }, 675 | "file_extension": ".py", 676 | "mimetype": "text/x-python", 677 | "name": "python", 678 | "nbconvert_exporter": "python", 679 | "pygments_lexer": "ipython3", 680 | "version": "3.9.12" 681 | } 682 | }, 683 | "nbformat": 4, 684 | "nbformat_minor": 5 685 | } 686 | --------------------------------------------------------------------------------