From a6e1b7fb66bcdd8cf1dcd08ffa6276192224e8df Mon Sep 17 00:00:00 2001 From: Arief Rahmansyah Date: Thu, 8 Aug 2024 00:45:08 +0700 Subject: [PATCH] python fundamentals 2 --- docs/_toc.yml | 33 +++++--- docs/python/oop/classes.md | 52 ++++++++++++ docs/python/oop/index.md | 10 ++- docs/python/oop/inheritance.md | 79 +++++++++++++++++++ docs/python/oop/polymorphism.md | 41 ++++++++++ docs/python/python_fundamentals/index.md | 1 - .../python_fundamentals/modules_packages.md | 1 - .../conditionals.ipynb | 0 .../data_types.ipynb | 0 .../dictionaries.ipynb | 0 .../functions.ipynb | 0 docs/python/python_fundamentals_1/index.md | 7 ++ .../lists.ipynb | 0 .../loops.ipynb | 0 .../python_fundamentals_1/modules_packages.md | 31 ++++++++ .../operators.md | 0 .../sets.ipynb | 0 .../strings.ipynb | 0 .../tuples.ipynb | 0 .../variables.ipynb | 0 docs/python/python_fundamentals_2/index.md | 7 ++ .../list_comprehensions.md | 1 + 22 files changed, 247 insertions(+), 16 deletions(-) create mode 100644 docs/python/oop/classes.md create mode 100644 docs/python/oop/inheritance.md create mode 100644 docs/python/oop/polymorphism.md delete mode 100644 docs/python/python_fundamentals/index.md delete mode 100644 docs/python/python_fundamentals/modules_packages.md rename docs/python/{python_fundamentals => python_fundamentals_1}/conditionals.ipynb (100%) rename docs/python/{python_fundamentals => python_fundamentals_1}/data_types.ipynb (100%) rename docs/python/{python_fundamentals => python_fundamentals_1}/dictionaries.ipynb (100%) rename docs/python/{python_fundamentals => python_fundamentals_1}/functions.ipynb (100%) create mode 100644 docs/python/python_fundamentals_1/index.md rename docs/python/{python_fundamentals => python_fundamentals_1}/lists.ipynb (100%) rename docs/python/{python_fundamentals => python_fundamentals_1}/loops.ipynb (100%) create mode 100644 docs/python/python_fundamentals_1/modules_packages.md rename docs/python/{python_fundamentals => python_fundamentals_1}/operators.md (100%) rename docs/python/{python_fundamentals => python_fundamentals_1}/sets.ipynb (100%) rename docs/python/{python_fundamentals => python_fundamentals_1}/strings.ipynb (100%) rename docs/python/{python_fundamentals => python_fundamentals_1}/tuples.ipynb (100%) rename docs/python/{python_fundamentals => python_fundamentals_1}/variables.ipynb (100%) create mode 100644 docs/python/python_fundamentals_2/index.md create mode 100644 docs/python/python_fundamentals_2/list_comprehensions.md diff --git a/docs/_toc.yml b/docs/_toc.yml index c6c9e23..cd9d482 100644 --- a/docs/_toc.yml +++ b/docs/_toc.yml @@ -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: diff --git a/docs/python/oop/classes.md b/docs/python/oop/classes.md new file mode 100644 index 0000000..9a4dd91 --- /dev/null +++ b/docs/python/oop/classes.md @@ -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) diff --git a/docs/python/oop/index.md b/docs/python/oop/index.md index 0726f93..2dbde48 100644 --- a/docs/python/oop/index.md +++ b/docs/python/oop/index.md @@ -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} + +``` diff --git a/docs/python/oop/inheritance.md b/docs/python/oop/inheritance.md new file mode 100644 index 0000000..1a92300 --- /dev/null +++ b/docs/python/oop/inheritance.md @@ -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) diff --git a/docs/python/oop/polymorphism.md b/docs/python/oop/polymorphism.md new file mode 100644 index 0000000..8e9e20e --- /dev/null +++ b/docs/python/oop/polymorphism.md @@ -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()}") +``` diff --git a/docs/python/python_fundamentals/index.md b/docs/python/python_fundamentals/index.md deleted file mode 100644 index ed66e96..0000000 --- a/docs/python/python_fundamentals/index.md +++ /dev/null @@ -1 +0,0 @@ -# Python Fundamentals diff --git a/docs/python/python_fundamentals/modules_packages.md b/docs/python/python_fundamentals/modules_packages.md deleted file mode 100644 index a8bb44f..0000000 --- a/docs/python/python_fundamentals/modules_packages.md +++ /dev/null @@ -1 +0,0 @@ -# 🚧 Modules & Packages diff --git a/docs/python/python_fundamentals/conditionals.ipynb b/docs/python/python_fundamentals_1/conditionals.ipynb similarity index 100% rename from docs/python/python_fundamentals/conditionals.ipynb rename to docs/python/python_fundamentals_1/conditionals.ipynb diff --git a/docs/python/python_fundamentals/data_types.ipynb b/docs/python/python_fundamentals_1/data_types.ipynb similarity index 100% rename from docs/python/python_fundamentals/data_types.ipynb rename to docs/python/python_fundamentals_1/data_types.ipynb diff --git a/docs/python/python_fundamentals/dictionaries.ipynb b/docs/python/python_fundamentals_1/dictionaries.ipynb similarity index 100% rename from docs/python/python_fundamentals/dictionaries.ipynb rename to docs/python/python_fundamentals_1/dictionaries.ipynb diff --git a/docs/python/python_fundamentals/functions.ipynb b/docs/python/python_fundamentals_1/functions.ipynb similarity index 100% rename from docs/python/python_fundamentals/functions.ipynb rename to docs/python/python_fundamentals_1/functions.ipynb diff --git a/docs/python/python_fundamentals_1/index.md b/docs/python/python_fundamentals_1/index.md new file mode 100644 index 0000000..7df4ed9 --- /dev/null +++ b/docs/python/python_fundamentals_1/index.md @@ -0,0 +1,7 @@ +# Python Fundamentals 1 + +**In this chapter, you’ll learn about:** + +```{tableofcontents} + +``` diff --git a/docs/python/python_fundamentals/lists.ipynb b/docs/python/python_fundamentals_1/lists.ipynb similarity index 100% rename from docs/python/python_fundamentals/lists.ipynb rename to docs/python/python_fundamentals_1/lists.ipynb diff --git a/docs/python/python_fundamentals/loops.ipynb b/docs/python/python_fundamentals_1/loops.ipynb similarity index 100% rename from docs/python/python_fundamentals/loops.ipynb rename to docs/python/python_fundamentals_1/loops.ipynb diff --git a/docs/python/python_fundamentals_1/modules_packages.md b/docs/python/python_fundamentals_1/modules_packages.md new file mode 100644 index 0000000..d808aa0 --- /dev/null +++ b/docs/python/python_fundamentals_1/modules_packages.md @@ -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). diff --git a/docs/python/python_fundamentals/operators.md b/docs/python/python_fundamentals_1/operators.md similarity index 100% rename from docs/python/python_fundamentals/operators.md rename to docs/python/python_fundamentals_1/operators.md diff --git a/docs/python/python_fundamentals/sets.ipynb b/docs/python/python_fundamentals_1/sets.ipynb similarity index 100% rename from docs/python/python_fundamentals/sets.ipynb rename to docs/python/python_fundamentals_1/sets.ipynb diff --git a/docs/python/python_fundamentals/strings.ipynb b/docs/python/python_fundamentals_1/strings.ipynb similarity index 100% rename from docs/python/python_fundamentals/strings.ipynb rename to docs/python/python_fundamentals_1/strings.ipynb diff --git a/docs/python/python_fundamentals/tuples.ipynb b/docs/python/python_fundamentals_1/tuples.ipynb similarity index 100% rename from docs/python/python_fundamentals/tuples.ipynb rename to docs/python/python_fundamentals_1/tuples.ipynb diff --git a/docs/python/python_fundamentals/variables.ipynb b/docs/python/python_fundamentals_1/variables.ipynb similarity index 100% rename from docs/python/python_fundamentals/variables.ipynb rename to docs/python/python_fundamentals_1/variables.ipynb diff --git a/docs/python/python_fundamentals_2/index.md b/docs/python/python_fundamentals_2/index.md new file mode 100644 index 0000000..d2cdaf1 --- /dev/null +++ b/docs/python/python_fundamentals_2/index.md @@ -0,0 +1,7 @@ +# 🚧 Python Fundamentals 2 + +**In this chapter, you’ll learn about:** + +```{tableofcontents} + +``` diff --git a/docs/python/python_fundamentals_2/list_comprehensions.md b/docs/python/python_fundamentals_2/list_comprehensions.md new file mode 100644 index 0000000..3830ce3 --- /dev/null +++ b/docs/python/python_fundamentals_2/list_comprehensions.md @@ -0,0 +1 @@ +# 🚧 List Comprenhensions