Learn Python variables with this beginner-friendly guide. Understand variable naming rules, assignments, and operations with examples and exercises. Perfect for students and professionals starting their Python journey.
Encapsulation is the concept of hiding the internal details of a class and providing methods to interact with the data. This is often achieved using private and public attributes.
Attributes that start with an underscore (e.g., _age) are conventionally considered private.
Example:
class Person: def init(self, name, age): self.name = name self._age = age # _age is considered private
def get_age(self):
return self._age
def set_age(self, age):
if age > 0:
self._age = age
person = Person(“Alice”, 30) person.set_age(35) print(person.get_age()) # Output: 35