Introduction to Python OOP

Tutorial 1 of 5

Python OOP: An Introduction

1. Introduction

1.1 Goal of the tutorial

This tutorial aims to introduce you to the basics of Object-Oriented Programming (OOP) in Python. OOP is a programming paradigm that provides a means of structuring programs so that properties and behaviors are bundled into individual objects.

1.2 Learning outcomes

By the end of this tutorial, you'll be able to:
- Understand what OOP is
- Create classes and objects in Python
- Understand and implement methods in Python

1.3 Prerequisites

A basic understanding of Python is required to follow this tutorial.

2. Step-by-Step Guide

2.1 Classes and Objects

In Python, everything is an object. Objects in Python are created using classes. A class is like a blueprint for creating objects.

# Defining a class
class MyClass:
    x = 5

# Creating an object
p1 = MyClass()
print(p1.x)  # Output: 5

2.2 Methods

Methods are functions defined inside the body of a class. They are used to define the behaviors of an object.

# Defining a class with a method
class MyClass:
    def greet(self):
        print("Hello, world!")

# Creating an object and calling a method
p1 = MyClass()
p1.greet()  # Output: Hello, world!

In the example above, self is a reference to the current instance of the class, and is used to access variables that belong to the class.

3. Code Examples

3.1 Example 1: A Simple Class with a Method

class Dog:
    # Class attribute
    species = "Canis familiaris"

    # Instance method
    def __init__(self, name, age):
        self.name = name
        self.age = age

    # Another instance method
    def description(self):
        return f"{self.name} is {self.age} years old"

    # Another instance method
    def speak(self, sound):
        return f"{self.name} says {sound}"

# Instantiate the Dog object
miles = Dog("Miles", 4)

# Access the instance attributes
print(miles.name)  # Output: Miles
print(miles.age)   # Output: 4

# Call our instance methods
print(miles.description())  # Output: Miles is 4 years old
print(miles.speak("Woof Woof"))  # Output: Miles says Woof Woof

In this example, Dog is a class with two class attributes species, and four instance methods: __init__, description, and speak.

3.2 Example 2: Inheritance in Python

# Parent class
class Bird:
    def __init__(self):
        print("Bird is ready")

    def whoisThis(self):
        print("Bird")

    def swim(self):
        print("Swim faster")

# Child class
class Penguin(Bird):
    def __init__(self):
        super().__init__()
        print("Penguin is ready")

    def whoisThis(self):
        print("Penguin")

    def run(self):
        print("Run faster")

peggy = Penguin()  # Output: Bird is ready
                   #         Penguin is ready
peggy.whoisThis()  # Output: Penguin
peggy.swim()       # Output: Swim faster
peggy.run()        # Output: Run faster

In this example, Penguin is a subclass (or child class) of the Bird superclass (or parent class). The Penguin class inherits methods from the Bird class.

4. Summary

In this tutorial, you've learned about classes and objects in Python, how to define methods within a class, and how to apply inheritance in Python OOP.

5. Practice Exercises

5.1 Exercise 1

Create a Person class with name and age attributes, and a method greet that prints a greeting including their name.

5.2 Exercise 2

Create a Student class that inherits from the Person class, includes a grade attribute, and overrides the greet method to also include their grade in the greeting.

5.3 Exercise 3

Create an Employee class that also inherits from Person, includes a job attribute, and also overrides the greet method to include their job.

5.4 Solutions

# Solution to Exercise 1
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def greet(self):
        print(f"Hello, my name is {self.name} and I'm {self.age} years old.")

# Solution for Exercise 2
class Student(Person):
    def __init__(self, name, age, grade):
        super().__init__(name, age)
        self.grade = grade

    def greet(self):
        print(f"Hello, my name is {self.name}, I'm {self.age} years old, and I'm in grade {self.grade}.")

# Solution for Exercise 3
class Employee(Person):
    def __init__(self, name, age, job):
        super().__init__(name, age)
        self.job = job

    def greet(self):
        print(f"Hello, my name is {self.name}, I'm {self.age} years old, and I'm a {self.job}.")

For further practice, try creating more complex classes that inherit from these, or adding more methods.