Embarking on your journey of learning Object Oriented Programming (OOP) in Python? You've landed at the perfect spot. Python, renowned for its simplicity and readability, empowers beginners to grasp complex programming concepts with ease. Among these, OOP stands as a cornerstone, fostering code reusability and aiding comprehension through real-world analogies. Let's dive into this engaging and hands-on tutorial from Tutorialspoint, tailored to guide you through Python's OOP prowess.

Python's versatility shines when it comes to OOP, providing several features that facilitate its implementation. This tutorial walks you through these features, from the fundamentals to advanced concepts, ensuring a robust understanding of the subject. Let's explore the key elements that await you within this comprehensive guide.

Basics of Object Oriented Programming in Python
The journey begins with the fundamentals. Python's OOP foundation resides in classes and objects, the cornerstones of this paradigm. By delving into this section, you'll learn how classes act as blueprints for creating objects and how instances, or objects, interact with these classes. You'll also explore the significance of attributes and methods in OOP.

Classes and Objects
Suppose you're designing a game featuring several player characters. Each character possesses unique attributes like name, health, and abilities. In OOP, you'd create a 'Character' class containing these attributes and methods (like 'attack' or 'heal'). Objects, then, are individual instances of this class, each with distinct attribute values.

Consider the following simple example: ```python class Character: def __init__(self, name, health): self.name = name self.health = health # Create objects (instances) of the Character class hero = Character("Batman", 100) enemy = Character("Joker", 80) ``` Here, `hero` and `enemy` are objects created from the `Character` class.
Attributes and Methods
Attributes constitute the data associated with an object. In our previous example, 'name' and 'health' are attributes of the `Character` class. Methods, on the other hand, define the actions an object can perform. They often influence attribute values. For instance, a 'take_damage' method could decrease the 'health' attribute.

Here's how you might implement this in Python: ```python class Character: def __init__(self, name, health): self.name = name self.health = health def take_damage(self, damage): self.health -= damage hero = Character("Batman", 100) hero.take_damage(10) print(hero.health) # Outputs: 90 ```
Intermediate OOP Concepts in Python
Once you've grasped the basics, you're ready to delve into more intricate OOP concepts. This section of the tutorial explores inheritance, polymorphism, and encapsulation - the core principles that empower OOP in Python.
Inheritance

Inheritance allows a new class to share code and attributes of an existing class, promoting code reusability. In our game analogy, we could create a 'Superhero' class that inherits from 'Character'. This 'Superhero' class could include additional attributes or methods specific to superheroes. Here's a simple example: ```python class Superhero(Character): # 'Superhero' inherits from 'Character' def __init__(self, name, health, is_flying): super().__init__(name, health) # 'super' calls the parent class's constructor self.is_flying = is_flying Universitas = Superhero("Flash", 100, True) print(Universitas.health) # Accessing inherited attribute ```
Polymorphism









Polymorphism enables methods to act differently based on the object that they are acting upon. It's achieved through method overriding (implementing a method in a child class with the same name but different functionality). By employing polymorphism, you can create more versatile and adaptable code. For instance, the 'attack' method in 'Superhero' and 'Character' classes could behave differently:
```python class Character: def attack(self): return "Basic attack" class Superhero(Character): def attack(self): return "Superpower attack!" hero = Character() superhero = Superhero() print(hero.attack()) # Outputs: Basic attack print(superhero.attack()) # Outputs: Superpower attack! ```
Encapsulation
Encapsulation shields the data (attributes) of an object from external interference. It enforces constraints on attribute access and modification, ensuring data integrity and security. In Python, encapsulation is achieved through private variables (prefixed with '__') and getter and setter methods. Here's a simple example:
```python class Character: def __init__(self, name, health): self._health = health # Using private variable def get_health(self): return self._health def set_health(self, health): if health >= 0: # Applying constraint self._health = health else: print("Health cannot be negative") hero = Character(100) hero.set_health(120) print(hero.get_health()) # Outputs: 120 ```
Finally, embrace your newfound knowledge of OOP in Python. Whether you're building complex systems, games, or simple scripts, OOP equips you with an arsenal of tools to keep your code organized, readable, and maintainable. So go forth, create, and innovate!