🌐
GeeksforGeeks
geeksforgeeks.org › python › __init__-in-python
__init__ in Python - GeeksforGeeks
Python Tutorial · Data Types · ... Flask · Last Updated : 12 Sep, 2025 · __init__ method in Python is a constructor. It runs automatically when a new object of a class is created....
Published   September 12, 2025
🌐
Python
docs.python.org › 3 › c-api › init.html
Initialization, finalization, and threads — Python 3.14.3 documentation
This page has been split up into the following: Interpreter initialization and finalization, Thread states and the global interpreter lock, Synchronization primitives, Thread-local storage support,...
🌐
Great Learning
mygreatlearning.com › blog › it/software development › python __init__: an overview
Python __init__: An Overview
October 14, 2024 - In Python, __init__ is a special method known as the constructor. It is automatically called when a new instance (object) of a class is created. The __init__ method allows you to initialize the attributes (variables) of an object.
🌐
LabEx
labex.io › tutorials › python-how-to-initialize-data-in-a-python-class-417302
How to initialize data in a Python class | LabEx
Learn how to properly initialize data in a Python class, including class attributes and instance variables. Discover techniques for effective class initialization and data management.
🌐
Real Python
realpython.com › python-class-constructor
Python Class Constructors: Control Your Object Instantiation – Real Python
January 19, 2025 - In this tutorial, you'll learn how class constructors work in Python. You'll also explore Python's instantiation process, which has two main steps: instance creation and instance initialization.
🌐
Real Python
realpython.com › videos › obj-declaration-initilization
Declaration and Initialization (Video) – Real Python
00:46 The constructor takes three ... 01:09 In Python, the declaration and initialization happen simultaneously in an .__init__() method....
Published   June 4, 2021
🌐
Python Tutorial
pythontutorial.net › home › python oop › python __init__
Python __init__
March 31, 2025 - In this tutorial, you'll learn how to use the Python __init__() method to initialize objects.
🌐
W3Schools
w3schools.com › python › gloss_python_class_init.asp
Python __init__() Function
To understand the meaning of classes we have to understand the built-in __init__() method. All classes have a method called __init__(), which is always executed when the class is being initiated.
Find elsewhere
🌐
Real Python
realpython.com › lessons › object-initialization-init
Initializing Objects With .__init__() (Video) – Real Python
Object Initialization With .__init__(). In Python, the .__init__() method is probably the most common special method you’ll override in your custom classes. Almost all your classes will need a custom implementation of .__init__() to allow you to…
Published   May 17, 2022
🌐
Edureka
edureka.co › blog › init-in-python
Init In Python | Python Init Class | What is Init Function | Edureka
November 27, 2024 - This article will introduce you to a simple yet important concept that is Init In Python with a thorough practical demonstration.
Top answer
1 of 7
23

There are several ways to assign the equal variables.

The easiest one:

grade_1 = grade_2 = grade_3 = average = 0.0

With unpacking:

grade_1, grade_2, grade_3, average = 0.0, 0.0, 0.0, 0.0

With list comprehension and unpacking:

>>> grade_1, grade_2, grade_3, average = [0.0 for _ in range(4)]
>>> print(grade_1, grade_2, grade_3, average)
0.0 0.0 0.0 0.0
2 of 7
21

I know you have already accepted another answer, but I think the broader issue needs to addressed - programming style that is suitable to the current language.

Yes, 'initialization' isn't needed in Python, but what you are doing isn't initialization. It is just an incomplete and erroneous imitation of initialization as practiced in other languages. The important thing about initialization in static typed languages is that you specify the nature of the variables.

In Python, as in other languages, you do need to give variables values before you use them. But giving them values at the start of the function isn't important, and even wrong if the values you give have nothing to do with values they receive later. That isn't 'initialization', it's 'reuse'.

I'll make some notes and corrections to your code:

def main():
   # doc to define the function
   # proper Python indentation
   # document significant variables, especially inputs and outputs
   # grade_1, grade_2, grade_3, average - id these
   # year - id this
   # fName, lName, ID, converted_ID 

   infile = open("studentinfo.txt", "r") 
   # you didn't 'intialize' this variable

   data = infile.read()  
   # nor this  

   fName, lName, ID, year = data.split(",")
   # this will produce an error if the file does not have the right number of strings
   # 'year' is now a string, even though you 'initialized' it as 0

   year = int(year)
   # now 'year' is an integer
   # a language that requires initialization would have raised an error
   # over this switch in type of this variable.

   # Prompt the user for three test scores
   grades = eval(input("Enter the three test scores separated by a comma: "))
   # 'eval' ouch!
   # you could have handled the input just like you did the file input.

   grade_1, grade_2, grade_3 = grades   
   # this would work only if the user gave you an 'iterable' with 3 values
   # eval() doesn't ensure that it is an iterable
   # and it does not ensure that the values are numbers. 
   # What would happen with this user input: "'one','two','three',4"?

   # Create a username 
   uName = (lName[:4] + fName[:2] + str(year)).lower()

   converted_id = ID[:3] + "-" + ID[3:5] + "-" + ID[5:]
   # earlier you 'initialized' converted_ID
   # initialization in a static typed language would have caught this typo
   # pseudo-initialization in Python does not catch typos
   ....
🌐
Copahost
copahost.com › home › init python: learn how to use __init__ to initialize objects in python
init python: Learn how to use __init__ to initialize objects in python - Copahost
August 29, 2023 - We’ll discuss how to create a ... efficiently and use both knowledge to improve your code. ... We use the method __init__ in Python to initialize a class....
🌐
Udacity
udacity.com › blog › 2021 › 11 › __init__-in-python-an-overview.html
__init__ in Python: An Overview | Udacity
May 10, 2022 - First, we declare the class Dog using the keyword class. We use the keyword def to define a function or method, such as the __init__ method. As you can see, the __init__ method initializes two attributes: breed and eyeColor.
🌐
Analytics Vidhya
analyticsvidhya.com › home › all about init in python
All About init in Python - Analytics Vidhya
February 1, 2024 - Discover the pivotal role of the init in Python. Explore its syntax and usage & delve into its crucial role during object creation & more.
🌐
docs.python.org
docs.python.org › 3 › tutorial › classes.html
9. Classes — Python 3.14.3 documentation
When a class defines an __init__() method, class instantiation automatically invokes __init__() for the newly created class instance. So in this example, a new, initialized instance can be obtained by:
🌐
Python
docs.python.org › 3 › c-api › init_config.html
Python Initialization Configuration — Python 3.14.4 documentation
The new module can be imported by the name name, and uses the function initfunc as the initialization function called on the first attempted import. Return 0 on success. ... If Python is initialized multiple times, PyInitConfig_AddModule() must ...
🌐
Built In
builtin.com › data-science › new-python
__new__ vs. __init__ Methods in Python | Built In
It receives the class itself as its first argument. __init__ is an instance method that initializes a newly created instance (object). It takes the object as its first argument followed by additional arguments.
🌐
Medium
ishanjainoffical.medium.com › understanding-pythons-init-method-object-initialization-in-depth-cc16f6e1e322
Understanding Python’s init Method: Object Initialization in Depth | by Data Science & Beyond | Medium
October 1, 2023 - Understanding Python’s init Method: Object Initialization in Depth The __init__ method is a special function in Python that's used to initialize objects when they are created. It allows you to set …