🎓 EASYTUTORGUIDE · Complete Python Course

Very beginner → professional Python · 60 chapters · 900 topics

EasyTutorGuide

Chapter 23: Encapsulation, Properties and Special Methods

Complete Python lesson for very beginners. Technical words are explained in simple language, and every outline topic includes a practical example, expected output, steps, and practice.

Python CourseVery Beginner Friendly15 TopicsExamples + Output20 Q&A
Chapter 23 · 15 topics
100%

Chapter Overview

This Python tutorial chapter covers Encapsulation, Properties and Special Methods through 15 connected topics. Work through the examples in order, check the expected output, and complete the practice after each topic.

  • 23.1 Encapsulation
  • 23.2 Public Members
  • 23.3 Protected Convention
  • 23.4 Name Mangling
  • 23.5 Getters
  • Plus 10 additional Python topics in this chapter.

23.1 Encapsulation

Encapsulation is part of Encapsulation, Properties and Special Methods. In simple language, it means a Python idea used while learning encapsulation, properties and special methods.

It is one building block of encapsulation, properties and special methods and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.

Technical words in simple language: Encapsulation (a Python idea used while learning encapsulation, properties and special methods).

Python / Practical Example

class Temperature:
    def __init__(self, c):
        self._c = c

    @property
    def celsius(self):
        return self._c

print(Temperature(20).celsius)

Expected Output

20

Step-by-Step Explanation

  1. Use an internal attribute by convention.
  2. @property exposes controlled read access using attribute syntax.
  3. Properties can later add validation without changing calling code.
Practice: Re-type the example for “Encapsulation”, change one safe input, predict the result before running it, and explain in one sentence why the result changed.

23.2 Public Members

Public Members is part of Encapsulation, Properties and Special Methods. In simple language, it means a Python idea used while learning encapsulation, properties and special methods.

It is one building block of encapsulation, properties and special methods and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.

Technical words in simple language: Public Members (a Python idea used while learning encapsulation, properties and special methods).

Python / Practical Example

class A:
    def __str__(self): return "A object"
print(A())

Expected Output

A object

Step-by-Step Explanation

  1. Read the example from top to bottom.
  2. Identify the value, object, or operation related to this topic.
  3. Change one small input and predict the result before running it.
Practice: Re-type the example for “Public Members”, change one safe input, predict the result before running it, and explain in one sentence why the result changed.

23.3 Protected Convention

Protected Convention is part of Encapsulation, Properties and Special Methods. In simple language, it means a Python idea used while learning encapsulation, properties and special methods.

It is one building block of encapsulation, properties and special methods and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.

Technical words in simple language: Protected Convention (a Python idea used while learning encapsulation, properties and special methods).

Python / Practical Example

class A:
    def __str__(self): return "A object"
print(A())

Expected Output

A object

Step-by-Step Explanation

  1. Read the example from top to bottom.
  2. Identify the value, object, or operation related to this topic.
  3. Change one small input and predict the result before running it.
Practice: Re-type the example for “Protected Convention”, change one safe input, predict the result before running it, and explain in one sentence why the result changed.

23.4 Name Mangling

Name Mangling is part of Encapsulation, Properties and Special Methods. In simple language, it means a Python idea used while learning encapsulation, properties and special methods.

It is one building block of encapsulation, properties and special methods and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.

Technical words in simple language: Name Mangling (a Python idea used while learning encapsulation, properties and special methods).

Python / Practical Example

class A:
    def __str__(self): return "A object"
print(A())

Expected Output

A object

Step-by-Step Explanation

  1. Read the example from top to bottom.
  2. Identify the value, object, or operation related to this topic.
  3. Change one small input and predict the result before running it.
Practice: Re-type the example for “Name Mangling”, change one safe input, predict the result before running it, and explain in one sentence why the result changed.

23.5 Getters

Getters is part of Encapsulation, Properties and Special Methods. In simple language, it means a Python idea used while learning encapsulation, properties and special methods.

It is one building block of encapsulation, properties and special methods and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.

Technical words in simple language: Getters (a Python idea used while learning encapsulation, properties and special methods).

Python / Practical Example

class Temperature:
    def __init__(self, c):
        self._c = c

    @property
    def celsius(self):
        return self._c

print(Temperature(20).celsius)

Expected Output

20

Step-by-Step Explanation

  1. Use an internal attribute by convention.
  2. @property exposes controlled read access using attribute syntax.
  3. Properties can later add validation without changing calling code.
Practice: Re-type the example for “Getters”, change one safe input, predict the result before running it, and explain in one sentence why the result changed.

23.6 Setters

Setters is part of Encapsulation, Properties and Special Methods. In simple language, it means a collection of unique values.

It is one building block of encapsulation, properties and special methods and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.

Technical words in simple language: Setters (a collection of unique values).

Python / Practical Example

class Temperature:
    def __init__(self, c):
        self._c = c

    @property
    def celsius(self):
        return self._c

print(Temperature(20).celsius)

Expected Output

20

Step-by-Step Explanation

  1. Use an internal attribute by convention.
  2. @property exposes controlled read access using attribute syntax.
  3. Properties can later add validation without changing calling code.
Practice: Re-type the example for “Setters”, change one safe input, predict the result before running it, and explain in one sentence why the result changed.

23.7 property()

property() is part of Encapsulation, Properties and Special Methods. In simple language, it means a Python idea used while learning encapsulation, properties and special methods.

It is one building block of encapsulation, properties and special methods and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.

Technical words in simple language: property() (a Python idea used while learning encapsulation, properties and special methods).

Python / Practical Example

class Temperature:
    def __init__(self, c):
        self._c = c

    @property
    def celsius(self):
        return self._c

print(Temperature(20).celsius)

Expected Output

20

Step-by-Step Explanation

  1. Use an internal attribute by convention.
  2. @property exposes controlled read access using attribute syntax.
  3. Properties can later add validation without changing calling code.
Practice: Re-type the example for “property()”, change one safe input, predict the result before running it, and explain in one sentence why the result changed.

23.8 @property

@property is part of Encapsulation, Properties and Special Methods. In simple language, it means a Python idea used while learning encapsulation, properties and special methods.

It is one building block of encapsulation, properties and special methods and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.

Technical words in simple language: @property (a Python idea used while learning encapsulation, properties and special methods).

Python / Practical Example

class Temperature:
    def __init__(self, c):
        self._c = c

    @property
    def celsius(self):
        return self._c

print(Temperature(20).celsius)

Expected Output

20

Step-by-Step Explanation

  1. Use an internal attribute by convention.
  2. @property exposes controlled read access using attribute syntax.
  3. Properties can later add validation without changing calling code.
Practice: Re-type the example for “@property”, change one safe input, predict the result before running it, and explain in one sentence why the result changed.

23.9 Validation

Validation is part of Encapsulation, Properties and Special Methods. In simple language, it means a Python idea used while learning encapsulation, properties and special methods.

It is one building block of encapsulation, properties and special methods and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.

Technical words in simple language: Validation (a Python idea used while learning encapsulation, properties and special methods).

Python / Practical Example

text = "123"
if text.isdigit():
    value = int(text)
    print(value)

Expected Output

123

Step-by-Step Explanation

  1. Check the input before converting it.
  2. isdigit() confirms these characters are digits.
  3. Convert only after the check passes.
Practice: Re-type the example for “Validation”, change one safe input, predict the result before running it, and explain in one sentence why the result changed.

23.10 __str__()

__str__() is part of Encapsulation, Properties and Special Methods. In simple language, it means a Python idea used while learning encapsulation, properties and special methods.

It is one building block of encapsulation, properties and special methods and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.

Technical words in simple language: __str__() (a Python idea used while learning encapsulation, properties and special methods).

Python / Practical Example

class Point:
    def __init__(self, x, y):
        self.x, self.y = x, y
    def __repr__(self):
        return f"Point({self.x}, {self.y})"

print(repr(Point(2, 3)))

Expected Output

Point(2, 3)

Step-by-Step Explanation

  1. Create a class with state.
  2. __repr__() returns a useful developer-facing representation.
  3. repr() asks the object for that representation.
Practice: Re-type the example for “__str__()”, change one safe input, predict the result before running it, and explain in one sentence why the result changed.

23.11 __repr__()

__repr__() is part of Encapsulation, Properties and Special Methods. In simple language, it means a Python idea used while learning encapsulation, properties and special methods.

It is one building block of encapsulation, properties and special methods and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.

Technical words in simple language: __repr__() (a Python idea used while learning encapsulation, properties and special methods).

Python / Practical Example

class Point:
    def __init__(self, x, y):
        self.x, self.y = x, y
    def __repr__(self):
        return f"Point({self.x}, {self.y})"

print(repr(Point(2, 3)))

Expected Output

Point(2, 3)

Step-by-Step Explanation

  1. Create a class with state.
  2. __repr__() returns a useful developer-facing representation.
  3. repr() asks the object for that representation.
Practice: Re-type the example for “__repr__()”, change one safe input, predict the result before running it, and explain in one sentence why the result changed.

23.12 Comparison Methods

Comparison Methods is part of Encapsulation, Properties and Special Methods. In simple language, it means a function associated with an object or class.

It is one building block of encapsulation, properties and special methods and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.

Technical words in simple language: Comparison Methods (a function associated with an object or class).

Python / Practical Example

age = 20
print(age >= 18)
print(18 <= age < 65)

Expected Output

True
True

Step-by-Step Explanation

  1. Comparisons produce booleans.
  2. Python supports chained comparisons.
  3. The full chain must be true for the result to be True.
Practice: Re-type the example for “Comparison Methods”, change one safe input, predict the result before running it, and explain in one sentence why the result changed.

23.13 Arithmetic Special Methods

Arithmetic Special Methods is part of Encapsulation, Properties and Special Methods. In simple language, it means a function associated with an object or class.

It is one building block of encapsulation, properties and special methods and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.

Technical words in simple language: Arithmetic Special Methods (a function associated with an object or class).

Python / Practical Example

a = 10
b = 3
print(a + b, a - b, a * b, a / b)

Expected Output

13 7 30 3.3333333333333335

Step-by-Step Explanation

  1. Create two numbers.
  2. Apply arithmetic operators.
  3. Notice that / produces a floating-point result.
Practice: Re-type the example for “Arithmetic Special Methods”, change one safe input, predict the result before running it, and explain in one sentence why the result changed.

23.14 Container Special Methods

Container Special Methods is part of Encapsulation, Properties and Special Methods. In simple language, it means a function associated with an object or class.

It is one building block of encapsulation, properties and special methods and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.

Technical words in simple language: Container Special Methods (a function associated with an object or class).

Python / Practical Example

class A:
    def __str__(self): return "A object"
print(A())

Expected Output

A object

Step-by-Step Explanation

  1. Read the example from top to bottom.
  2. Identify the value, object, or operation related to this topic.
  3. Change one small input and predict the result before running it.
Practice: Re-type the example for “Container Special Methods”, change one safe input, predict the result before running it, and explain in one sentence why the result changed.

23.15 Python Data Model

Python Data Model is part of Encapsulation, Properties and Special Methods. In simple language, it means a Python idea used while learning encapsulation, properties and special methods.

It helps Python turn data into calculations, summaries, visual explanations, or predictions. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.

Technical words in simple language: Python Data Model (a Python idea used while learning encapsulation, properties and special methods).

Python / Practical Example

class A:
    def __str__(self): return "A object"
print(A())

Expected Output

A object

Step-by-Step Explanation

  1. Read the example from top to bottom.
  2. Identify the value, object, or operation related to this topic.
  3. Change one small input and predict the result before running it.
Practice: Re-type the example for “Python Data Model”, change one safe input, predict the result before running it, and explain in one sentence why the result changed.

Common Beginner Mistakes

  • Copying code without predicting what each line does.
  • Ignoring the first useful error message or traceback location.
  • Mixing tabs/spaces or changing indentation accidentally.
  • Using data of the wrong type for an operation.
  • Trying to learn many advanced variations before mastering one small working example.

Chapter Practice

  1. Choose three topics from this chapter and re-type their examples without copying and pasting.
  2. For each example, change one input and predict the output first.
  3. Explain five technical terms from this chapter in your own beginner-friendly words.
  4. Create one small program that combines at least two chapter topics.
  5. Keep notes about errors you made and what fixed them.

Mini Project / Challenge

Create a small Python exercise that combines at least three ideas from Encapsulation, Properties and Special Methods. Start with a tiny working version, test it, then improve it one step at a time.

  1. Choose three topics from this chapter.
  2. Write or adapt a small Python example using those topics.
  3. Predict the output before running the code.
  4. Test at least one different input.
  5. Write two sentences explaining what the program does and what you learned.

20 Questions & Answers

1. What is the main goal of Chapter 23?

The goal is to understand encapsulation, properties and special methods through small explanations, examples, and practice.

2. Should I memorize every command or method?

No. Understand the pattern, practice the common form, and learn how to read documentation when you need exact details.

3. Why are the examples small?

Small examples isolate one idea at a time, which makes errors easier to understand and fix.

4. What should I do when an example gives an error?

Read the last part of the traceback, check spelling and indentation, confirm the required package or file exists, and compare the input types with what the operation expects.

5. Why should I predict output before running code?

Prediction forces you to reason about the program instead of only copying it.

6. What should I remember about Encapsulation?

Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.

7. What should I remember about Public Members?

Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.

8. What should I remember about Protected Convention?

Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.

9. What should I remember about Name Mangling?

Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.

10. What should I remember about Getters?

Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.

11. What should I remember about Setters?

Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.

12. What should I remember about property()?

Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.

13. What should I remember about @property?

Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.

14. What should I remember about Validation?

Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.

15. What should I remember about __str__()?

Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.

16. What should I remember about __repr__()?

Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.

17. What should I remember about Comparison Methods?

Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.

18. What should I remember about Arithmetic Special Methods?

Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.

19. What should I remember about Container Special Methods?

Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.

20. What should I remember about Python Data Model?

Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.