Python for AI 105 Warm-up
Acknowledgement: This post is based mainly on TA Dang Nha’s presentation and slides and partly on TA, STA, and peers’ responses from the course, AIO2025, offered by AI VIETNAM.
Relevance: The audience of this lecture were those who have read Python for AI 104, had some coding experience, and known a bit of AI knowledge. Absolute beginners are encouraged to read the material and repeatedly practice coding without AI assistance to strengthen their coding and code-reading skills. Note that only necessary Python knowledge is introduced here.
In today’s post, we’ll talk about classes and objects.
OOP
Object-Oriented Programming (OOP) “bring” the real world into digital world, abstracting real-life concepts and problems into a digital representation that a computer can process.
OOP has four characteristics with the first two introduced later.
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction.
Before OOP, we had procedural programming utilizing functions and basic data structures discussed previously. Functions must be defined in the order that they are used. This leads to its drawback which is the ability to manage a large interconnecting system like social media platforms with so many users and relationships among them.
OOP addresses this issue by grouping objects (subjects) with similar attributes (properties) and methods (behaviors). We name such a group a class. We’ll discuss these concepts in detail in the following sections.
Take library management as an example. Classes can be books, readers, and librarians; each book, reader, and librarian is an object of its class. Each reader should have attributes like name, ID, and contact method. In addition, we need a process of borrowing and returning books. This process is associated with methods of a class. A librarian can lend books and take returns from readers.
Classes and Objects
class ClassName:
def constructor(self, attribute_value_1, ..., attribute_value_n):
self.attribute_name_1 = attribute_value_1
...
self.attribute_name_n = attribute_value_n
def method_name_1(self, attribute_name_1, ..., attribute_name_m):
# function body
...
def method_name_k(self, attribute_name_1, ..., attribute_name_p):
# function body
This theme supports creating diagrams directly in markdown using Mermaid. Mermaid enables users to render flowcharts, sequence diagrams, class diagrams, Gantt charts, and more. Simply embed the diagram syntax within a mermaid code block.
Abstraction: Constructor, Attribute, and Method
ClassName is PascalCase, noun attribute name is noun, snake_case method is verb, snake_case
Practice Exercises
Additional details, where math \(2x - 1\) and code is rendered correctly.
Practice Exercises
animal = ['e', 'l', 'e', 'p', 'h', 'a', 'n', 't']. Use slicing in different ways if possible to answer the following. - Get the first four letters.
- Get the last three letters.
- Get the every second letter starting from index 1.
- Get the all letters except the first and last.
- Get the middle three letters.
- Get an empty list.
- Reverse the letters.
- Get the letters h, p, e .
- What does
animal[1:-1:2]return?
Inheritance
Why Inheritance?
Child classes inherit
Why do We Need Inheritance?
Practice Exercises
Point out the differences between the functions below. Is the original list updated in both? Explain why or why not. How is it related to immutable and mutable here? What are pros and cons of each? In which cases should we use them?| Immutable | Mutable |
|---|---|
def square(data): result = [] for value in data: result.append(value * value) return result # test data = [6, 5, 7, 1, 9, 2] print(data) data_s = square(data) print(data_s) | def square(data): length = len(data) for i in length: value = data[i] * data[i] data[i] = value # test data = [6, 5, 7, 1, 9, 2] print(data) data_s = square(data) print(data_s) |
Pros:
| Pros:
|
| Methods | Purposes | Returned Value | List Updated | Examples |
|---|---|---|---|---|
| append() | Add a given value to the end | None | a_list.append(10) a_list >>> [4, 5, 6, 7, 4, 7, 10] | |
| insert() | Add a given value to the position of a given index | None | a_list.insert(2,10) a_list >>> [4, 5, 10, 6, 7, 4, 7] | |
| extend() | Concatenate a given list to the original | None | a_list.extend([10, 8]) a_list >>> [4, 5, 6, 7, 4, 7, 10, 8] | |
| pop() | Delete the last element by default or delete by index | Value | a_list.pop(1) >>> 5 a_list >>> [4, 6, 7, 4, 7] | |
| remove() | Delete the first element of a given value | None | a_list.remove(7) a_list >>> [4, 5, 6, 4, 7] | |
| del | Delete a given list of elements | N/A | del a_list[1:4] a_list >>> [4, 7, 7] | |
| clear() | Delete all elements | None | a_list.clear() a_list >>> [] | |
| index() | Return the index of the first occurence of a given value | Index | a_list.index(7) >>> 3 a_list >>> [4, 5, 6, 7, 4, 7] | |
| reverse() | Reverse elements | None | a_list.reverse() a_list >>> [7, 4, 7, 6, 5, 4] | |
| sort() | Arrange elements in an increasing order by default | None | a_list.sort() a_list >>> [4, 4, 5, 6, 7, 7] | |
| count() | Return the number of a given value | Number of times an element appears | a_list.count(7) >>> 2 a_list ` »> [4, 5, 6, 7, 4, 7]` | |
| copy() | Make a copy of the list | New list that has the same value as the original | a_list.copy() >>> [4, 5, 6, 7, 4, 7] a_list >>> [4, 5, 6, 7, 4, 7] |
| Functions | Purposes | Returned Value | List Updated | Examples |
|---|---|---|---|---|
| len() | Return the number of elements | Quantity | len(a_list) >>> 6 | |
| min() | Return the minimum element | Quantity | min(a_list) >>> 4 | |
| max() | Return the maximum element | Quantity | max(a_list) >>> 7 | |
| sorted() | Arrange elements in an ascending order by default | New List | sorted_a_list = sorted(a_list) sorted_a_list >>> [4, 4, 5, 6, 7, 7] | |
| sum() | Return the sum of all elements | Quantity | sum(a_list) >>> 33 | |
| reversed() | Reverse elements | A reverse iterator object | ||
| enumerate() | Return a sequence of tuples of matching index and value | An enumerate object | ||
| zip() | Take two lists and return an iterator of tuples of values with matching index | A zip object |
«hr>
From here on in, the material is
Bonus
FAQ
Here are some good questions asked during class. The answers to these questions came from either some admin, TA, STA, or members of AIO2025.
| Questions | Answers |
|---|---|
| Above some class is @get or @function. What are they? | They are decorators https://www.geeksforgeeks.org/python/decorators-in-python/ |
| What is pipeline? | You can think of it as workflow or procedure. |
| A resource for mutable and immutable. | https://www.geeksforgeeks.org/mutable-vs-immutable-objects-in-python/ |
| A resource for pass by reference and pass by value | https://www.tutorialspoint.com/python/python_functions.htm |
| How do I tell when two variables point to the same object in memory? | You either need to understand what’s going on very well or check with the id() function. Each object has one and only one identity. |
| Can I use list comprehension to create a completely new list? | Yes. It all depends on how you choose your values inside the branch condition. |
| Visualization of Sorted List | https://www.cs.usfca.edu/~galles/visualization/Search.html |
| Are integral image and box filtering similar? | Yes. Box filtering is in graphics. |
Check List
- Exercise 1 (date)
- Exercise 2 (date)
- Put on left sock
- Put on right sock
- Go to school
Enjoy Reading This Article?
Here are some more articles you might like to read next: