My second week at DataraFlow.
Learning Data Science at DataraFlow: My Journey with Core Concepts and Projects.
Table of Contents
Introduction
Object-Oriented Programming (OOP) and Inheritance
Polymorphism
Iterators
Python Scope
Conclusion
1. Introduction
As part of my Data Science internship at Dataraflow, I dedicated my first week to learning Python, focusing on basic concepts of python. The second week, I focused on core programming concepts such as classes, objects, inheritance, polymorphism, iterators, and variable scope. This article documents my journey and includes practical examples I implemented during the week.
2. Object-Oriented Programming (OOP) and Inheritance
OOP allowing one to structure your code using classes and object for better organization and reusability. It’s like learning to organize your code the way the real-world data works-using object. OOP allows us to model real-world entities in Python using classes and objects.
A class defines the blueprint, a class defines what object should look like and an object is created based on that class. When you create an object from a class, it inherits all the variables and functions defined inside that class, and an object is an instance of that class. Inheritance enables a class (child) to inherit properties and methods from another class (parent).
Classes and Instances
Why do we use classes? Classes allow us to logically group our data and functions in a way that is easy to (reuse) and also easy to build upon if needed.
Methods: Methods are function that is associated with a class.
Instance Variable: contain data that is unique to each instance. When you create methods within a class, they (receive) the instance as the first argument. After self, you can create any argument you want.
Class Variables: these are variables that are shared among all the instances of a class. So, while instance can be unique for each instance like our name and email and pay, class variables should be same for each instance.
Python Inheritance
Just like it sounds, python inheritance allows us to inherit attributes ad methods from a parent class. This is useful because we can create subclasses and get all of the functionality of our parent class and then we can overwrite or add completely new functionality without affecting the parent class in any way.
Example: Creating a Book class with subclasses EBook and PrintedBook.
class Book:
def __init__(self, title, author):
self.title = title
self.author = author
def get_description(self):
return f"{self.title} by {self.author}"
class EBook(Book):
def __init__(self, title, author, file_size):
super().__init__(title, author)
self.file_size = file_size
def get_description(self):
return f"E-Book: {self.title}, {self.file_size}MB"
3. Polymorphism
Polymorphism is an ability to behave differently in response to the same input messages. Polymorphism essentially means they all have the same methods but that doesn’t mean they provide the same response (i.e. polymorphism allows the same method to behave differently depending on the object calling it.)
Example: borrow_book method for different types of library members.
class Member:
def borrow_book(self, book):
print(f"{self.__class__.__name__} borrowed {book.title}")
class StudentMember(Member):
def borrow_book(self, book):
print(f"Student borrowed {book.title} (limit 3 books)")
class TeacherMember(Member):
def borrow_book(self, book):
print(f"Teacher borrowed {book.title} (limit 5 books)")
4. Iterators
Iterators allow sequential access to elements without exposing the underlying representation. You implement the iterator protocol using __iter__() and __next__().
An iterator is an object that can be iterated upon, meaning that you can traverse through all the values.
Create an Iterator
To create an object/class as an iterator you have to implement the methods __iter__() and __next__() to your object.
The __iter__() method allows you to do operations (initializing etc.), but must always return the iterator object itself.
The __next__() method also allows you to do operations, and must return the next item in the sequence.
StopIteration
To prevent the iteration from going on forever, we can use the StopIteration statement.
In the __next__() method, we can add a terminating condition to raise an error if the iteration is done a specified number of time
Example: Countdown iterator
class CountDown:
def __init__(self, n):
self.n = n
def __iter__(self):
return self
def __next__(self):
if self.n > 0:
current = self.n
self.n -= 1
return current
else:
raise StopIteration
for num in CountDown(5):
print(num)
5. Python Scope
Scope defines the visibility of variables in Python.
Global: accessible anywhere in the module.
Nonlocal: allows modifying variables in an enclosing function.
Private/Protected: using _ or __ to control access inside classes.
Example:
total = 0 #Global variable
def add_to_total(n):
global total
total += n
add_to_total(5)
print(total) # 5
def outer():
message = "Hi"
def inner():
nonlocal message
message = "Hello"
inner()
print(message)
outer() # Hello
In Conclusion
In week two at DataraFlow, I have:
Learned Python object-oriented programming fundamentals
Practiced iterators, polymorphism, and variable scopes
Applied my knowledge in a mini Library Management System project
Python’s readability, flexibility, and object-oriented capabilities make it a great language for both learning and real-world data science applications.
