diff options
| author | Christian Kolset <christian.kolset@gmail.com> | 2025-11-12 17:40:04 -0700 |
|---|---|---|
| committer | Christian Kolset <christian.kolset@gmail.com> | 2025-11-12 17:40:04 -0700 |
| commit | bda9939b6e93e06f61d5a51d90d6fb4a75d4aab8 (patch) | |
| tree | b754f0782713b7c285696f28a662dd384b51bdc6 /tutorials | |
| parent | 01b70c6416783dd24b1154fba8a39fd1b127faf8 (diff) | |
Added body for OOP tutorial
Diffstat (limited to 'tutorials')
| -rw-r--r-- | tutorials/module_1/classes_and_objects.md | 131 | ||||
| -rw-r--r-- | tutorials/module_1/spyder_getting_started.md | 2 | ||||
| -rw-r--r-- | tutorials/module_4/Spectroscopy problem.md | 3 |
3 files changed, 131 insertions, 5 deletions
diff --git a/tutorials/module_1/classes_and_objects.md b/tutorials/module_1/classes_and_objects.md index c2f7457..8a0be85 100644 --- a/tutorials/module_1/classes_and_objects.md +++ b/tutorials/module_1/classes_and_objects.md @@ -1,7 +1,5 @@ # Modular Programming -^70725c - ## 1. Introduction - A. What is Object-Oriented Programming? @@ -43,3 +41,132 @@ - C. Extend with inheritance (e.g., `ServoMotor`) - D. Bonus: Integrate two objects to simulate interaction +--- +# Modular Programming +OOP +What is it? +Modular programming or better known as Object-Oriented Programming (OOP) is a way we can structure programs so that properties and behaviors of objects are grouped together. It allows us to re-use code which simplifies the code for better readability in larger programs and reduces the potential for bugs. You're probably familiar with the saying "Don't re-invent the wheel". OOP is the programmers solution to this. Python allows us to import libraries so that we don't have to write everything from scratch. The libraries used in this course provide us with algorithms to calculate a solution, structure data and plot data. When looking at the source code of these library you may see keywords, such as `class`. This tutorial will delve into the fundamental concepts of OOP. + +One analogy for OOP goes as follows. If we were to build a house we need to follow a common structure or blueprint which defines what a house should have, this is the *class*. The class may be thought of as the blueprint for a house, defining what a house should include (rooms, doors and windows). Then we have *objects* which is the actual house that is built from the blueprint. You can build multiple objects from the same plan. + +Imagine you are an urban planner in the town of Wellington, the town is rapidly expanding and housing is in high demand. You are tasked with developing a new housing area. Designing 50 different houses will take too much, but building the same house 50 times will make the neighborhood look like a subdivision from the 1950s. + +Class: Is the blueprint of the house +Object: Is a specific house in the neighborhood with defined attributes +Method: Attached garage + + +Let's take a look at an example. Do not worry if you don't understand the syntax below, we will explain this later. Let's define the class: +```python +class People(): + def __init__(self, name, age): + self.name = name + self.age = age + + def greet(self): + print("Greetings, " + self.name) +``` +Now we can use our class to define new objects: +```python +person1 = People(name = 'Iron Man', age = 35) +person1.greet() +print(person1.name,person1.age) + +person2 = People(name = 'Batman', age = 33) +person2.greet() +print(person2.name,person2.age) +``` + +In the example above we first defined the class `People` with `name` and `age` as the data and the `greet` as the method. Once we initialized an object `person1` we can see that the class defines the entire structure, whilst the object is just an instance of the class. It is clear that `person1` and `person2` are independent with each other, though they are all instantiated from the same class. + + 3. Why use it? +You may wonder why you should even bother with classes and object. Why not store data as arrays? +```python +kirk = ["James Kirk", 34, "Captain", 2265] +spock = ["Spock", 35, "Science Officer", 2254] +mccoy = ["Leonard McCoy", "Chief Medical Officer", 2266] +``` + + 1. Simplifies the code for better readability. + 2. reduces the number of potential bugs. + 4. Real world uses + 1. Python Libraries/Packages +2. Core concepts + 1. Class (Blueprints) + A class is used to define logical grouping of data and functions and objects. + 2. Objects () + 3. Attributes + 4. Methods - A **method** is a **function that lives inside a class** and has access to the data stored inside the object. + 5. The four pillars: + 1. Encapsulation + 2. Inheritance + 3. Abstraction + 4. Polymorphism +3. Syntax and Examples + + +```mermaid +flowchart TD + + A[OOP] --> B[Class] + B --> C[Attributes: variables / data] + B --> D[Methods: functions / behaviors] + + C -->|define structure| E[Class Definition] + D -->|define behavior| E + + E -->|instantiation| F[Object] + F --> G[Object has its own<br>attribute values] + F --> H[Object can call methods] + +``` + + + +Starting a class is done with the `class` keyword followed by the name of the class. +```python +class Dog: + pass +``` +In this example `pass` is used a placeholder to indicate where code will eventually go. This prevents python to output any errors. Next, give our class some properties and behaviors. For the purpose of this demonstration we will keep it small but in reality the class may be significantly more detailed. + +The properties are defined inside the method called `.__init__()`. It runs automatically when a new object of a class is created. It's main purpose is to initialize the object attributes and set up its initial state. When an object is created, memory is allocated for it, and `__init__` helps organize that memory by assigning values to attributes. + +Let's add a name, breed and age attibute to our + + +```python +class Dog: + def __init__(self, name, breed="Mixed", age=1): + self.name = name + self.breed = breed + self.age = age + +dog1 = Dog("Buddy") +dog2 = Dog("Bolt", "German Sheppard", 5) + +print(dog1.name, dog1.breed, dog1.age) +print(dog2.name, dog2.breed, dog2.age) +``` + +Notice how the `__init__` method defaults the inputs`breed="Mixed"` and `age=1` when no parameters are given. + + +```python + +``` + + +```python + +``` + + +```python + +``` + + +```python + +```
\ No newline at end of file diff --git a/tutorials/module_1/spyder_getting_started.md b/tutorials/module_1/spyder_getting_started.md index b6a40ee..a23f0c6 100644 --- a/tutorials/module_1/spyder_getting_started.md +++ b/tutorials/module_1/spyder_getting_started.md @@ -26,7 +26,7 @@ This pane is used to write your scripts. The 6. The tab bar displays the names of all opened files. It also has a Browse tabs button (at left) to show every open tab and switch between them—which comes in handy if many are open. ### IPython Console -This pane allows you to interactively run functions, do math computations, assign and modify variables. +The "I" stands for interactive. This pane allows you to interactively run functions, perform math computations, assign and modify variables.  diff --git a/tutorials/module_4/Spectroscopy problem.md b/tutorials/module_4/Spectroscopy problem.md index 0e9af66..429d387 100644 --- a/tutorials/module_4/Spectroscopy problem.md +++ b/tutorials/module_4/Spectroscopy problem.md @@ -108,7 +108,6 @@ $$ I_{\lambda,\Omega}(T)= \epsilon (\frac{2hc^2}{\lambda^5}\frac{1}{e^{hc/kT}-1}) $$ - T=1800K - - Use $R(\lambda)$ to correct spectra for plasma $$ I_{measure}^W = R(\lambda) * I_{true}^W(\lambda) @@ -121,7 +120,7 @@ I_{meas}^{plasma}(\lambda) = \frac{I_{meas}^{W}(\lambda)}{I_{true}^{W}(\lambda)} $$ -Measure the densities of an excited state of oxygen using $I(\lambda)$ + Measure the densities of an excited state of oxygen using $I(\lambda)$ $$ I(\lambda)=\frac{1}{4\pi}hvAnl\phi(\lambda-\lambda_0) $$ |
