Monday, 6 July 2026

Python Coding challenge - Day 1194| What is the output of the following Python Code?

 



Code Explanation:

๐Ÿ”น 1. Creating the Class
class Student:
✅ Explanation:
A class named Student is created.
It acts as a blueprint for creating student objects.

Current structure:

Student Class


├── __init__()

└── marks()

At this point, no object exists.

๐Ÿ”น 2. Defining the Constructor
def __init__(self):
✅ Explanation:
__init__() is the constructor of the class.
It is automatically called whenever a new object is created.

Its job is to initialize the object's data.

Visual:

Object Created


__init__()


Initialize Variables

๐Ÿ”น 3. Creating an Instance Variable
self._marks = 80
✅ Explanation:

A variable named _marks is created for the current object.

Current object:

Student Object

_marks = 80
Why _marks?

The single underscore (_) is a Python naming convention that indicates:

"This is an internal (protected) variable.
It should not be accessed directly."

Although you can access it, it's recommended to use a property instead.

๐Ÿ”น 4. Using the @property Decorator
@property
✅ Explanation:

@property converts the next method into a property.

Normally, you would call a method like this:

obj.marks()

With @property, you can access it like an attribute:

obj.marks

without parentheses.

Visual:

Without @property

marks()


With @property

marks

๐Ÿ”น 5. Defining the Property Method
def marks(self):
✅ Explanation:

This method is responsible for returning the student's marks.

Because of @property, Python treats it like an attribute.

Current structure:

Student


_marks = 80


marks


Returns _marks

๐Ÿ”น 6. Returning the Value

return self._marks
✅ Explanation:

The method returns the value stored in:

self._marks

Current value:

80

So whenever someone accesses:

s.marks

Python actually executes:

marks()

behind the scenes and returns:

80

๐Ÿ”น 7. Creating an Object
s = Student()
✅ Explanation:

A new object of the Student class is created.

Execution flow:

Student()


__init__()


_marks = 80

Current object:

s


_marks = 80

๐Ÿ”น 8. Accessing the Property
print(s.marks)
✅ Explanation:

Here, it looks like we're accessing an attribute.

s.marks

But because marks is decorated with @property, Python internally calls:

s.marks()

and gets:

80

๐Ÿ”น 9. Printing the Result
print(s.marks)
✅ Explanation:

Python prints the value returned by the property.

Output:

80

๐ŸŽฏ Final Output
80

Book: 100 Python Programs for Beginner with explanation

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (301) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (30) Azure (12) BI (10) Books (272) Bootcamp (12) C (78) C# (12) C++ (83) cloud (1) Course (87) Coursera (300) Cybersecurity (32) data (7) Data Analysis (38) Data Analytics (26) data management (16) Data Science (384) Data Strucures (23) Deep Learning (188) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (21) Finance (10) flask (4) flutter (1) FPL (17) Generative AI (74) Git (12) Google (53) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (43) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (337) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (14) PHP (20) Projects (34) Python (1397) Python Coding Challenge (1183) Python Mathematics (4) Python Mistakes (51) Python Quiz (560) Python Tips (22) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (20) SQL (52) Udemy (18) UX Research (1) web application (11) Web development (9) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)