# My second week at DataraFlow.

**Learning Data Science at DataraFlow: My Journey with Core Concepts and Projects.**

**Table of Contents**

1. Introduction
    
2. Object-Oriented Programming (OOP) and Inheritance
    
3. Polymorphism
    
4. Iterators
    
5. Python Scope
    
6. 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](http://self.author) = author

def get\_description(self):

return f"{self.title} by {[self.author](http://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.

```python
class Member:
```

```python
    def borrow_book(self, book):
```

```python
        print(f"{self.__class__.__name__} borrowed {book.title}")
```

```python
class StudentMember(Member):
```

```python
    def borrow_book(self, book):
```

```python
        print(f"Student borrowed {book.title} (limit 3 books)")
```

```python
class TeacherMember(Member):
```

```python
    def borrow_book(self, book):
```

```python
        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

```python
class CountDown:
```

```python
    def __init__(self, n):
```

```python
        self.n = n
```

```python
    def __iter__(self):
```

```python
        return self
```

```python
    def __next__(self):
```

```python
        if self.n > 0:
```

```python
            current = self.n
```

```python
            self.n -= 1
```

```python
            return current
```

```python
        else:
```

```python
            raise StopIteration
```

```python
for num in CountDown(5):
```

```python
    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:**

```python
total = 0  #Global variable
```

```python
def add_to_total(n):
```

```python
    global total
```

```python
    total += n
```

```python
add_to_total(5)
```

```python
print(total)  # 5
```

```python
def outer():
```

```python
    message = "Hi"
```

```python
    def inner():
```

```python
        nonlocal message
```

```python
        message = "Hello"
```

```python
    inner()
```

```python
    print(message)
```

```python
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.
