Thinking Like a Programmer•Interactive notebook lesson•Free
Classes, self, other
No account is required. Learn the material, try the examples, and mark it complete locally when you are ready to move on.
Classes
Classes can be understood as a data type. It is not a data type, but for intuition can be assumed as a custom data type.
Like for example, here we will create a custom data type Animal which will have features just like an animal does.
Codepython
"ASHISH".lower()Output
'ashish'Codepython
class Animal:
"""A class representing an animal with a strength attribute."""
strength = 10 # Default strength value for all animals
def strength_plus(self, strength_points):
"""
Increases the animal's strength by a given amount.
Parameters:
strength_points (int or float): The amount to increase strength by.
"""
self.strength = self.strength + strength_points # Update the strength attribute