-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
39ab174
commit a6e1b7f
Showing
22 changed files
with
247 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()}") | ||
``` |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# 🚧 List Comprenhensions |