Skip to content

Commit

Permalink
python fundamentals 2
Browse files Browse the repository at this point in the history
  • Loading branch information
ariefrahmansyah committed Aug 7, 2024
1 parent 39ab174 commit a6e1b7f
Show file tree
Hide file tree
Showing 22 changed files with 247 additions and 16 deletions.
33 changes: 20 additions & 13 deletions docs/_toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,29 @@ parts:

- caption: Python
chapters:
- file: python/python_fundamentals/index
- file: python/python_fundamentals_1/index
sections:
- file: python/python_fundamentals/variables
- file: python/python_fundamentals/data_types
- file: python/python_fundamentals_1/variables
- file: python/python_fundamentals_1/data_types
sections:
- file: python/python_fundamentals/strings
- file: python/python_fundamentals/lists
- file: python/python_fundamentals/tuples
- file: python/python_fundamentals/sets
- file: python/python_fundamentals/dictionaries
- file: python/python_fundamentals/operators
- file: python/python_fundamentals/conditionals
- file: python/python_fundamentals/loops
- file: python/python_fundamentals/functions
- file: python/python_fundamentals/modules_packages
- file: python/python_fundamentals_1/strings
- file: python/python_fundamentals_1/lists
- file: python/python_fundamentals_1/tuples
- file: python/python_fundamentals_1/sets
- file: python/python_fundamentals_1/dictionaries
- file: python/python_fundamentals_1/operators
- file: python/python_fundamentals_1/conditionals
- file: python/python_fundamentals_1/loops
- file: python/python_fundamentals_1/functions
- file: python/python_fundamentals_1/modules_packages
- file: python/python_fundamentals_2/index
sections:
- file: python/python_fundamentals_2/list_comprehensions
- file: python/oop/index
sections:
- file: python/oop/classes
- file: python/oop/inheritance
- file: python/oop/polymorphism

- caption: Data Science
chapters:
Expand Down
52 changes: 52 additions & 0 deletions docs/python/oop/classes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Classes

**Example of creating a simple class and an object:**

```{code-block} python
# Define a simple class
class Dog:
def __init__(self, name):
self.name = name
def bark(self):
print(f"{self.name} says woof!")
# Create an object (instance) of the Dog class
my_dog = Dog("Buddy")
my_dog.bark() # Call the bark method on the object
```

## Class Attributes and Methods

Classes can have attributes and methods that define their behavior.

- **Class Attributes**:
- Class attributes are shared among all instances of the class.
- **Instance Attributes**:
- Instance attributes are specific to individual objects.
- **Methods**:
- Methods are functions defined within a class.

**Example of class attributes and methods:**

```{code-block} python
class Circle:
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14159 * self.radius**2
def circumference(self):
return 2 * 3.14159 * self.radius
my_circle = Circle(5)
print(f"Area: {my_circle.area()}")
print(f"Circumference: {my_circle.circumference()}")
```

## Python Documentation

- [Classes](https://docs.python.org/3/tutorial/classes.html)
10 changes: 9 additions & 1 deletion docs/python/oop/index.md
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
# 🚧 Object-Oriented Programming in Python
# Object-Oriented Programming in Python

Object-oriented programming (OOP) is a programming paradigm that uses objects to represent real-world entities.

**In this chapter, you’ll learn about:**

```{tableofcontents}
```
79 changes: 79 additions & 0 deletions docs/python/oop/inheritance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Inheritance

Inheritance allows you to create new classes that inherit attributes and methods from existing classes.

- **Parent Class (Superclass)**:
- The parent class defines common attributes and methods.
- **Child Class (Subclass)**:
- The child class inherits from the parent class and can have additional attributes and methods.

**Example of inheritance**:

```{code-block} python
# Parent class
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
pass
# Child class inheriting from Animal
class Dog(Animal):
def speak(self):
return f"{self.name} says woof!"
my_dog = Dog("Buddy")
print(my_dog.speak()) # Calls the speak method of the Dog class
```

## Mixins (multiple inheritance)

Python allows you to inherit from multiple classes. While the technical term for this is multiple inheritance, many developers refer to the use of more than one base class adding a mixin. These are commonly used in frameworks such as [Django](https://www.djangoproject.com).

- [Multiple Inheritance](https://docs.python.org/3/tutorial/classes.html#multiple-inheritance)
- [super](https://docs.python.org/3/library/functions.html#super) is used to give access to methods and properties of a parent class

**Example of multiple inheritance**:

```{code-block} python
class Loggable:
def __init__(self):
self.title = "
def log(self):
print("Log message from " + self.title)
class Connection:
def __init__(self):
self.server = "
def connect(self):
print("Connecting to database on " + self.server)
class SqlDatabase(Connection, Loggable):
def __init__(self):
super().__init__()
self.title = "Sql Connection Demo"
self.server = "Some_Server"
def framework(item):
if isinstance(item, Connection):
item.connect()
if isinstance(item, Loggable):
item.log()
sql_connection = SqlDatabase()
framework(sql_connection)
```

## Python Documentation

- [Inheritance](https://docs.python.org/3/tutorial/classes.html#inheritance)
- [Multiple Inheritance](https://docs.python.org/3/tutorial/classes.html#multiple-inheritance)
41 changes: 41 additions & 0 deletions docs/python/oop/polymorphism.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Polymorphism

Polymorphism allows objects of different classes to be treated as objects of a common superclass.

- **Common Superclass**:
- Create a common superclass that defines shared methods or attributes.
- **Subclasses with Different Implementations**:
- Subclasses provide their own implementations of methods.

**Example of polymorphism**:

```{code-block} python
# Common superclass
class Shape:
def area(self):
pass
# Subclasses with different implementations of area
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14159 * self.radius**2
class Rectangle(Shape):
def __init__(self, width, height):
self.width = width
self.height = height
def area(self):
return self.width * self.height
shapes = [Circle(5), Rectangle(4, 6)]
for shape in shapes:
print(f"Area: {shape.area()}")
```
1 change: 0 additions & 1 deletion docs/python/python_fundamentals/index.md

This file was deleted.

1 change: 0 additions & 1 deletion docs/python/python_fundamentals/modules_packages.md

This file was deleted.

7 changes: 7 additions & 0 deletions docs/python/python_fundamentals_1/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Python Fundamentals 1

**In this chapter, you’ll learn about:**

```{tableofcontents}
```
File renamed without changes.
File renamed without changes.
31 changes: 31 additions & 0 deletions docs/python/python_fundamentals_1/modules_packages.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Modules & Packages

## Modules

[Modules](https://docs.python.org/3/tutorial/modules.html) allow you to store reusable blocks of code, such as functions, in separate files. They're referenced by using the `import` statement.

```python
# helpers.py
def display(message, is_warning=False):
if is_warning:
print('Warning!!')
print(message)
```

```python
# import module as namespace
import helpers
helpers.display('Not a warning')

# import all into current namespace
from helpers import *
display('Not a warning')

# import specific items into current namespace
from helpers import display
display('Not a warning')
```

## Packages

[Distribution packages](https://packaging.python.org/glossary/#term-distribution-package) are external archive files which contain resources such as classes and functions. Most every application you create will make use of one or more packages. Imports from packages follow the same syntax as modules you've created. The [Python Package index](https://pypi.org/) contains a full list of packages you can install using [pip](https://pip.pypa.io/en/stable/) or [Poetry](../../getting_started/setup_python/setup_poetry.md).
File renamed without changes.
7 changes: 7 additions & 0 deletions docs/python/python_fundamentals_2/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# 🚧 Python Fundamentals 2

**In this chapter, you’ll learn about:**

```{tableofcontents}
```
1 change: 1 addition & 0 deletions docs/python/python_fundamentals_2/list_comprehensions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# 🚧 List Comprenhensions

0 comments on commit a6e1b7f

Please sign in to comment.