- Notifications
You must be signed in to change notification settings - Fork 180
Añadir ejercicio 042-understanding_classes #61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
63ec128
00f59a1
d380b93
1fa3ff1
1d7bcbd
2076909
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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/) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agregar los cambios que se pidieron en el readme en español There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done | ||
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/) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Your code here |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Quita todos los print, y reemplazalo por un unico return que retorne:
return f"After studying for {hours} hours, {self.name}'s new grade is {self.grade}."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done