The Fundamentals of OOP in Python: A Beginner's Guide

winstonmhango23

4 min read

Views: 204

Before diving into the advanced concepts of Object-Oriented Programming (OOP), it's essential to understand the basics. This guide will introduce you to the fundamental building blocks of OOP, starting with what objects are and how they relate to OOP

What are Objects?

In programming, an object is a self-contained entity that consists of both data and procedures to manipulate that data. Objects represent real-world entities, such as a bank account, a car, or a person.

What is Object-Oriented Programming (OOP)?

Object-Oriented Programming is a programming paradigm centered around objects. It organizes software design around data, or objects, rather than functions and logic. The key principles of OOP are encapsulation, abstraction, inheritance, and polymorphism, which we will explore in detail in later sections

Fundamental Building Blocks

Variables

Variables are the simplest building blocks in programming. They store data that can be used and manipulated.

# Example of variables
account_number = "001"
balance = 1000

Lists

Lists are ordered collections of items that can be of different types. They are mutable, meaning their elements can be changed.

# Example of a list
transactions = [100, -50, 200, -30]

Tuples

Tuples are similar to lists, but they are immutable, meaning their elements cannot be changed once assigned.

# Example of a tuple
account_info = ("001", "John Doe", 1000)

Dictionaries

Dictionaries are collections of key-value pairs. They are useful for storing related data and providing fast access to values based on their keys.

# Example of a dictionary
account_details = {
    "account_number": "001",
    "name": "John Doe",
    "balance": 1000
}

Arrays

Arrays are similar to lists but are more efficient for numerical operations. They require importing the array module.

import array

# Example of an array
balances = array.array('i', [1000, 2000, 3000])

Building Objects in Python

To build objects, we start by grouping related data and behaviors. Without using OOP principles, we can use the built-in data structures to model objects.

Example: Bank Account without OOP

Using Dictionaries

# Representing a bank account using a dictionary
bank_account = {
    "account_number": "001",
    "name": "John Doe",
    "balance": 1000,
    "transactions": [100, -50, 200, -30]
}

# Functions to operate on the bank account
def deposit(account, amount):
    account["balance"] += amount
    account["transactions"].append(amount)

def withdraw(account, amount):
    if amount <= account["balance"]:
        account["balance"] -= amount
        account["transactions"].append(-amount)
    else:
        print("Insufficient funds")

# Using the functions
deposit(bank_account, 500)
withdraw(bank_account, 200)

print(bank_account["balance"])  # Output: 1300
print(bank_account["transactions"])  # Output: [100, -50, 200, -30, 500, -200]

Using Lists and Tuples

# Representing a bank account using lists and tuples
account_number = "001"
name = "John Doe"
balance = 1000
transactions = [100, -50, 200, -30]

# Functions to operate on the bank account
def deposit(account_number, balance, transactions, amount):
    balance += amount
    transactions.append(amount)
    return balance

def withdraw(account_number, balance, transactions, amount):
    if amount <= balance:
        balance -= amount
        transactions.append(-amount)
    else:
        print("Insufficient funds")
    return balance

# Using the functions
balance = deposit(account_number, balance, transactions, 500)
balance = withdraw(account_number, balance, transactions, 200)

print(balance)  # Output: 1300
print(transactions)  # Output: [100, -50, 200, -30, 500, -200]

Limitations without Using OOP

Using dictionaries, lists, and tuples to model objects can become cumbersome as the complexity of the program increases. It is harder to maintain and understand the relationships between different entities. This is where OOP principles come into play, offering a more structured and intuitive approach to organizing code.

Transitioning to OOP

By encapsulating related data and behaviors into classes, we can create more organized and maintainable code. Classes provide a blueprint for creating objects, enabling better abstraction, inheritance, encapsulation, and polymorphism.

In the next guide, we will delve into the advanced concepts of OOP in Python, exploring how these principles can be applied to create robust and scalable software solutions. Stay tuned!

Recent Related Posts

Related Posts

Creating and uploading a Python package to the PyPi: Part 3 Creating The package and uploading to pypi.org

In this part of the series, we will walk you through the final steps of creating a Python package and uploading it to PyPi. We will use the example package bank_creator that we discussed in Part 2. The full code for this package can be found on GitHub.

Read More

Creating and uploading a Python package to the PyPi: Part 2 Creating Git repository and uploading to github

Read More

Creating and uploading a Python package to the PyPi: Part 1 Basics

Read More

© 2024 .