From 63ec128de8086f5758bb0913f8ef051d801f9182 Mon Sep 17 00:00:00 2001 From: Jose Mora <109150320+josemoracard@users.noreply.github.com> Date: Tue, 2 Jan 2024 03:29:53 +0000 Subject: [PATCH 1/6] =?UTF-8?q?A=C3=B1adir=20ejercicio=20042-understanding?= =?UTF-8?q?=5Fclasses?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../042-understanding_classes/README.es.md | 44 +++++++++++++++++++ exercises/042-understanding_classes/README.md | 44 +++++++++++++++++++ exercises/042-understanding_classes/app.py | 1 + 3 files changed, 89 insertions(+) create mode 100644 exercises/042-understanding_classes/README.es.md create mode 100644 exercises/042-understanding_classes/README.md create mode 100644 exercises/042-understanding_classes/app.py diff --git a/exercises/042-understanding_classes/README.es.md b/exercises/042-understanding_classes/README.es.md new file mode 100644 index 00000000..12e522af --- /dev/null +++ b/exercises/042-understanding_classes/README.es.md @@ -0,0 +1,44 @@ +# `042` understanding classes + +En Python, una clase es una estructura que te permite organizar y encapsular datos y funcionalidades relacionadas. Las clases son una característica fundamental de la programación orientada a objetos (OOP), un paradigma de programación que utiliza objetos para modelar y organizar el código. + +En términos simples, una clase es como un plano o un molde para crear objetos. Un objeto es una instancia específica de una clase que tiene atributos (datos) y métodos (funciones) asociados. Los atributos representan características del objeto, y los métodos representan las acciones que el objeto puede realizar. + +## 📎 Ejemplo: + +```py +class Student: + def __init__(self, name, age, grade): # Estos son sus atributos + self.name = name + self.age = age + self.grade = grade + + def introduce(self): # Esto es un método + print(f"Hello! I am {self.name}, I am {self.age} years old, and my current grade is {self.grade}.") + + def study(self, hours): # Esto es otro método + print(f"{self.name} is studying for {hours} hours.") + self.grade += hours * 0.5 + print(f"After studying, {self.name}'s new grade is {self.grade}.") + +student1 = Student("Ana", 20, 80) + +student1.introduce() +student1.study(3) +``` + +En este código: + ++ La clase `Student` tiene un método `__init__` para inicializar los atributos *name*, *age* y *grade* del estudiante. ++ `introduce` es un método que imprime un mensaje presentando al estudiante. ++ `study` es un método que simula el acto de estudiar y actualiza la nota del estudiante. + +## 📝 Instrucciones: + +1. Para completar este ejercicio, copia el código proporcionado en el ejemplo y pégalo en tu archivo `app.py`. Ejecuta el código y prueba su funcionalidad. Experimenta con modificar diferentes aspectos del código para observar cómo se comporta. Este enfoque práctico te ayudará a comprender la estructura y el comportamiento de la clase `Student`. Una vez que te hayas familiarizado con el código y sus efectos, siéntete libre de pasar al siguiente ejercicio. + +## 💡 Pistas: + ++ Lee un poco sobre la función interna `__init__`: https://www.w3schools.com/python/gloss_python_class_init.asp + ++ Si no comprendes la funcionalidad del parámetro `self` en el código que acabas de copiar, tómate un momento para visitar el siguiente enlace donde encontrarás una explicación detallada: [Entendiendo el parámetro 'self'](https://www.geeksforgeeks.org/self-in-python-class/) \ No newline at end of file diff --git a/exercises/042-understanding_classes/README.md b/exercises/042-understanding_classes/README.md new file mode 100644 index 00000000..6cdb70dd --- /dev/null +++ b/exercises/042-understanding_classes/README.md @@ -0,0 +1,44 @@ +# `042` understanding classes + +In Python, a class is a structure that allows you to organize and encapsulate related data and functionalities. Classes are a fundamental feature of object-oriented programming (OOP), a programming paradigm that uses objects to model and organize code. + +In simple terms, a class is like a blueprint or a template for creating objects. An object is a specific instance of a class that has associated attributes (data) and methods (functions). Attributes represent the characteristics of the object, and methods represent the actions that the object can perform. + +## 📎 Example: + +```py +class Student: + def __init__(self, name, age, grade): # These are its attributes + self.name = name + self.age = age + self.grade = grade + + def introduce(self): # This is a method + print(f"Hello! I am {self.name}, I am {self.age} years old, and my current grade is {self.grade}.") + + def study(self, hours): # This is another method + print(f"{self.name} is studying for {hours} hours.") + self.grade += hours * 0.5 + print(f"After studying, {self.name}'s new grade is {self.grade}.") + +student1 = Student("Ana", 20, 80) + +student1.introduce() +student1.study(3) +``` + +In this code: + ++ The `Student` class has an `__init__` method to initialize the attributes *name*, *age*, and *grade* of the student. ++ `introduce` is a method that prints a message introducing the student. ++ `study` is a method that simulates the act of studying and updates the student's grade. + +## 📝 Instructions: + +1. To complete this exercise, copy the provided code from the example and paste it into your `app.py` file. Execute the code and test its functionality. Experiment with modifying different aspects of the code to observe how it behaves. This hands-on approach will help you understand the structure and behavior of the `Student` class. Once you have familiarized yourself with the code and its effects, feel free to proceed to the next exercise. + +## 💡 Hints: + ++ Read about the `__init__` built-in function: https://www.w3schools.com/python/gloss_python_class_init.asp + ++ If you find yourself wondering about the role of the `self` keyword in Python classes, take a moment to visit the following link for a detailed explanation: [Understanding the 'self' Parameter](https://www.geeksforgeeks.org/self-in-python-class/) \ No newline at end of file diff --git a/exercises/042-understanding_classes/app.py b/exercises/042-understanding_classes/app.py new file mode 100644 index 00000000..fce62c1d --- /dev/null +++ b/exercises/042-understanding_classes/app.py @@ -0,0 +1 @@ +# Your code here From 00f59a1c373f71acc0e1c759275c58283e9e1e5c Mon Sep 17 00:00:00 2001 From: Jose Mora <109150320+josemoracard@users.noreply.github.com> Date: Wed, 24 Jan 2024 00:18:14 +0100 Subject: [PATCH 2/6] Update README.es.md --- exercises/042-understanding_classes/README.es.md | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/exercises/042-understanding_classes/README.es.md b/exercises/042-understanding_classes/README.es.md index 12e522af..44f72670 100644 --- a/exercises/042-understanding_classes/README.es.md +++ b/exercises/042-understanding_classes/README.es.md @@ -14,17 +14,16 @@ class Student: self.grade = grade def introduce(self): # Esto es un método - print(f"Hello! I am {self.name}, I am {self.age} years old, and my current grade is {self.grade}.") + return f"Hello! I am {self.name}, I am {self.age} years old, and my current grade is {self.grade}." def study(self, hours): # Esto es otro método - print(f"{self.name} is studying for {hours} hours.") self.grade += hours * 0.5 - print(f"After studying, {self.name}'s new grade is {self.grade}.") + return f"After studying for {hours} hours, {self.name}'s new grade is {self.grade}." student1 = Student("Ana", 20, 80) -student1.introduce() -student1.study(3) +print(student1.introduce()) +print(student1.study(3)) ``` En este código: @@ -41,4 +40,4 @@ En este código: + Lee un poco sobre la función interna `__init__`: https://www.w3schools.com/python/gloss_python_class_init.asp -+ Si no comprendes la funcionalidad del parámetro `self` en el código que acabas de copiar, tómate un momento para visitar el siguiente enlace donde encontrarás una explicación detallada: [Entendiendo el parámetro 'self'](https://www.geeksforgeeks.org/self-in-python-class/) \ No newline at end of file ++ Si no comprendes la funcionalidad del parámetro `self` en el código que acabas de copiar, tómate un momento para visitar el siguiente enlace donde encontrarás una explicación detallada: [Entendiendo el parámetro 'self'](https://www.geeksforgeeks.org/self-in-python-class/) From d380b932adbffcc5c140dd2a0323a4d86a8c574c Mon Sep 17 00:00:00 2001 From: Jose Mora <109150320+josemoracard@users.noreply.github.com> Date: Wed, 24 Jan 2024 00:20:08 +0100 Subject: [PATCH 3/6] Update README.md --- exercises/042-understanding_classes/README.md | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/exercises/042-understanding_classes/README.md b/exercises/042-understanding_classes/README.md index 6cdb70dd..d28cf9cc 100644 --- a/exercises/042-understanding_classes/README.md +++ b/exercises/042-understanding_classes/README.md @@ -14,17 +14,16 @@ class Student: self.grade = grade def introduce(self): # This is a method - print(f"Hello! I am {self.name}, I am {self.age} years old, and my current grade is {self.grade}.") + return f"Hello! I am {self.name}, I am {self.age} years old, and my current grade is {self.grade}." def study(self, hours): # This is another method - print(f"{self.name} is studying for {hours} hours.") self.grade += hours * 0.5 - print(f"After studying, {self.name}'s new grade is {self.grade}.") + return f"After studying for {hours} hours, {self.name}'s new grade is {self.grade}." student1 = Student("Ana", 20, 80) -student1.introduce() -student1.study(3) +print(student1.introduce()) +print(student1.study(3)) ``` In this code: @@ -41,4 +40,4 @@ In this code: + Read about the `__init__` built-in function: https://www.w3schools.com/python/gloss_python_class_init.asp -+ If you find yourself wondering about the role of the `self` keyword in Python classes, take a moment to visit the following link for a detailed explanation: [Understanding the 'self' Parameter](https://www.geeksforgeeks.org/self-in-python-class/) \ No newline at end of file ++ If you find yourself wondering about the role of the `self` keyword in Python classes, take a moment to visit the following link for a detailed explanation: [Understanding the 'self' Parameter](https://www.geeksforgeeks.org/self-in-python-class/) From 1fa3ff17f4c185bbf2c7bd27aee1fec88c57b917 Mon Sep 17 00:00:00 2001 From: Jose Mora <109150320+josemoracard@users.noreply.github.com> Date: Wed, 24 Jan 2024 00:20:47 +0100 Subject: [PATCH 4/6] Create solution.hide.py --- .../042-understanding_classes/solution.hide.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 exercises/042-understanding_classes/solution.hide.py diff --git a/exercises/042-understanding_classes/solution.hide.py b/exercises/042-understanding_classes/solution.hide.py new file mode 100644 index 00000000..25deec23 --- /dev/null +++ b/exercises/042-understanding_classes/solution.hide.py @@ -0,0 +1,18 @@ +# Your code here +class Student: + def __init__(self, name, age, grade): # These are its attributes + self.name = name + self.age = age + self.grade = grade + + def introduce(self): # This is a method + return f"Hello! I am {self.name}, I am {self.age} years old, and my current grade is {self.grade}." + + def study(self, hours): # This is another method + self.grade += hours * 0.5 + return f"After studying for {hours} hours, {self.name}'s new grade is {self.grade}." + +student1 = Student("Ana", 20, 80) + +print(student1.introduce()) +print(student1.study(3)) From 1d7bcbd931a37a51e4df404cb543893cbc926b8d Mon Sep 17 00:00:00 2001 From: Jose Mora <109150320+josemoracard@users.noreply.github.com> Date: Wed, 24 Jan 2024 00:42:44 +0100 Subject: [PATCH 5/6] Create test.py --- exercises/042-understanding_classes/test.py | 48 +++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 exercises/042-understanding_classes/test.py diff --git a/exercises/042-understanding_classes/test.py b/exercises/042-understanding_classes/test.py new file mode 100644 index 00000000..c855a1c7 --- /dev/null +++ b/exercises/042-understanding_classes/test.py @@ -0,0 +1,48 @@ +import pytest +from app import Student + +@pytest.mark.it("The Student class should exist") +def test_student_class_exists(): + try: + assert Student + except AttributeError: + raise AttributeError("The class 'Student' should exist in app.py") + +@pytest.mark.it("The Student class includes the 'name' attribute") +def test_student_has_name_attribute(): + student = Student("John", 21, 75) + assert hasattr(student, "name") + +@pytest.mark.it("The Student class includes the 'age' attribute") +def test_student_has_age_attribute(): + student = Student("John", 21, 75) + assert hasattr(student, "age") + +@pytest.mark.it("The Student class includes the 'grade' attribute") +def test_student_has_grade_attribute(): + student = Student("John", 21, 75) + assert hasattr(student, "grade") + +@pytest.mark.it("The Student class includes the 'introduce' method") +def test_student_has_introduce_method(): + student = Student("Alice", 22, 90) + assert hasattr(student, "introduce") + +@pytest.mark.it("The introduce method should return the expected string. Testing with different values") +def test_student_introduce_method_returns_expected_string(): + student1 = Student("Alice", 22, 90) + student2 = Student("Bob", 19, 85) + assert student1.introduce() == "Hello! I am Alice, I am 22 years old, and my current grade is 90" + assert student2.introduce() == "Hello! I am Bob, I am 19 years old, and my current grade is 85" + +@pytest.mark.it("The Student class includes the 'study' method") +def test_student_has_study_method(): + student = Student("John", 21, 75) + assert hasattr(student, "study") + +@pytest.mark.it("The study method should return the expected string. Testing with different values") +def test_student_study_method_returns_expected_string(): + student1 = Student("Eve", 20, 78) + student2 = Student("Charlie", 23, 88) + assert student1.study(3) == "After studying for 3 hours, Eve's new grade is 79.5." + assert student2.study(2) == "After studying for 2 hours, Charlie's new grade is 89.0." From 207690999b2a5ca6f29f15d796d88b74aa5235d6 Mon Sep 17 00:00:00 2001 From: Jose Mora <109150320+josemoracard@users.noreply.github.com> Date: Wed, 24 Jan 2024 00:43:08 +0100 Subject: [PATCH 6/6] Update test.py --- exercises/042-understanding_classes/test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/exercises/042-understanding_classes/test.py b/exercises/042-understanding_classes/test.py index c855a1c7..ec183078 100644 --- a/exercises/042-understanding_classes/test.py +++ b/exercises/042-understanding_classes/test.py @@ -32,8 +32,8 @@ def test_student_has_introduce_method(): def test_student_introduce_method_returns_expected_string(): student1 = Student("Alice", 22, 90) student2 = Student("Bob", 19, 85) - assert student1.introduce() == "Hello! I am Alice, I am 22 years old, and my current grade is 90" - assert student2.introduce() == "Hello! I am Bob, I am 19 years old, and my current grade is 85" + assert student1.introduce() == "Hello! I am Alice, I am 22 years old, and my current grade is 90." + assert student2.introduce() == "Hello! I am Bob, I am 19 years old, and my current grade is 85." @pytest.mark.it("The Student class includes the 'study' method") def test_student_has_study_method():