Fundamentos de Programaci贸n y Python
驴Por qu茅 aprender Python?
Introducci贸n a Python
Conceptos B谩sicos de Programaci贸n
Pr谩ctica: Te doy la bienvenida a los ejercicios interactivos
Manipulaci贸n de Cadenas de Texto en Python
Enteros, Flotantes y Booleanos
Todo lo que Debes Saber sobre print en Python
Operaciones Matem谩ticas en Python
Operaciones de Entrada/Salida en Consola
Colecci贸n y Procesamiento de Datos en Python
Listas
M茅todo slice
Listas de m谩s dimensiones y Tuplas
Aplicaci贸n de Matrices
Diccionarios
Comprehension Lists en Python (CLASE NUEVA)
Control de Flujo en Python
Estructuras condicionales
Bucles y Control de Iteraciones
Generadores e Iteradores
Funciones y Manejo de Excepciones en Python
Uso de Funciones en Python
Funciones Lambda y Programaci贸n Funcional en Python
驴C贸mo realizar una funci贸n recursiva en Python?
Manejo de Excepciones y Uso de Pass (CLASE NUEVA)
Programaci贸n Orientada a Objetos en Python
Fundamentos de Programaci贸n Orientada a Objetos en Python
Ejercicio Biblioteca con POO
Herencia en POO con Python
Objetos heredados
Los 4 pilares de la programacion orientada a objetos
Uso de super() en Python (CLASE NUEVA)
Superando los Fundamentos de Programaci贸n Orientada a Objetos en Python
Lectura y escritura de archivos
Manejo de Archivos .TXT (CLASE NUEVA)
Manejo de Archivos CSV (CLASE NUEVA)
Manejo de Archivos JSON (CLASE NUEVA)
Biblioteca est谩ndar de Python
Biblioteca est谩ndar en Python (CLASE NUEVA)
Librer铆a Os, Math y Random (CLASE NUEVA)
Librer铆a Statistics y An谩lisis Estad铆stico (CLASE NUEVA)
Proyecto final: Guerra naval
Conceptos avanzados de Python
Recapitulaci贸n de lo aprendido hasta ahora
Escribir c贸digo Pythonico y profesional
Comentarios y Docstrings en Python
Scope y closures: variables locales y globales
Anotaciones de tipo
Validaci贸n de tipos en m茅todos
Librer铆a Collections y Enumeraciones
Decoradores
Decoradores en Python
Decoradores anidados y con par谩metros
Uso de Decoradores en clases y m茅todos
M茅todos y estructura de clases en Python
M茅todos m谩gicos
Sobrecarga de operadores
Implementaci贸n de `if __name__ == "__main__":`
Metaprogramaci贸n en Python
Uso de *args y **kwargs
M茅todos privados y protegidos
Gesti贸n avanzada de propiedades
M茅todos est谩ticos y de clase avanzados
Programaci贸n concurrente y as铆ncrona
Introducci贸n a la concurrencia y paralelismo
Threading y multiprocessing en Python
Asincronismo con asyncio
Asincronismo y concurrencia
Creaci贸n de m贸dulos y paquetes
Creaci贸n de m贸dulos en Python
Gesti贸n de paquetes
Publicaci贸n de paquetes en PyPI
Proyecto final
Implementaci贸n de un sistema completo
Implementaci贸n de un Sistema Completo
You don't have access to this class
Keep learning! Join and start boosting your career
Object-oriented programming (OOP) is a programming paradigm based on organizing software into objects, which are instances of classes. Classes act as generic templates that define attributes and behaviors. For example, a "Person" class can have attributes such as first name, last name and date of birth.
To create a class in Python, you use the reserved word class
followed by the class name with the first letter capitalized. Within the class, a constructor is defined with the __init__
function. This function initializes the attributes of the object.
class Person: def __init__(self, name, age):self.name = name self.age = age def greet(self):print(f"Hello, my name is {self.name} and I am {self.age} years old")# Create objects of the class Person Person person1 = Person("Ana", 30) person2 = Person("Luis", 25) person1.greet() person2.greet()
Methods are functions defined within a class that operate on the objects of the class. In the example above, greet
is a method of the Person
class.
A practical example of OOP is the management of a bank account. We create a BankAccount
class with methods to deposit and withdraw money, as well as to activate and deactivate the account.
class BankAccount: def __init__(self, account_holder, balance):self.account_holder = account_holder self.balance = balance self.is_active = True def deposit(self, amount): if self.is_active: self.balance += amount print(f"Deposited {amount}. Current balance: {self.balance}") else: print("Cannot deposit, account inactive") def withdraw(self, amount): if self.is_active: if amount <= self.balance: self.balance -= amount print(f"Withdrawn {amount}. Current balance: {self.balance}") else: print("Insufficient funds") else: print("Cannot withdraw, account inactive") def deactivate(self):self.is_active = Falseprint("Account has been deactivated") def activate(self):self.is_active = Trueprint("The account has been activated")# Create objects of the BankAccount classaccount1 = BankAccount("Anna", 500) account2 = BankAccount("Luis", 1000) account1.deposit(500) account2.withdraw(100) account1.deactivate() account1.deposit(200) account1.activate() account1.deposit(200)
The creation of objects follows a syntax similar to the creation of variables, but using the name of the class followed by the necessary parameters for the constructor.
# Account creation account1= BankAccount("Ana", 500) account2 = BankAccount("Luis", 1000)# Performing operationsaccount1.deposit(500) account2.withdraw(100) account1.deactivate() account1.deposit(200) # Cannot deposit, account inactive account1.activate() account1.deposit(200) # Deposit successful
Contributions 57
Questions 3
Want to see more contributions, questions and answers from the community?