Tutorials References Exercises Certificates Search...
For Teachers Upgrade Get Certified Sign In
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANG
›
Python RegEx
Python PIP
REMOVE ADS
Python Try...Except
Python String Formatting
Python None
Python User Input
Python VirtualEnv
Click on ► to watch the video
Python Classes
Python OOP
Python Classes/Objects
Python __init__ Method
Python self Parameter
Python Class Properties Python __init__() Method
Python Class Methods COLOR PICKER
Python Inheritance ‹ Previous Next ›
Python Polymorphism
Python Encapsulation
Python Inner Classes
The __init__() Method
File Handling
All classes have a built-in method called __init__() , which is always executed when the class is being initiated.
Python File Handling
Python Read Files The __init__() method is used to assign values to object properties, or to perform operations that are necessary when
Python Write/Create Files the object is being created.
Python Delete Files
Python Modules Example Get your own Python Server
NumPy Tutorial
Pandas Tutorial Create a class named Person, use the __init__() method to assign values for name and age:
SciPy Tutorial
class Person:
Django Tutorial
def __init__(self, name, age):
[Link] = name
Python Matplotlib [Link] = age
Matplotlib Intro
Matplotlib Get Started p1 = Person("Emil", 36)
Matplotlib Pyplot
print([Link])
print([Link])
Try it Yourself »
Note: The __init__() method is called automatically every time the class is being used to create a new object.
Why Use __init__()?
Without the __init__() method, you would need to set properties manually for each object:
Example
Create a class without __init__() :
class Person:
pass
p1 = Person()
[Link] = "Tobias"
[Link] = 25
print([Link])
print([Link])
Try it Yourself »
Using __init__() makes it easier to create objects with initial values:
Example
With __init__() , you can set initial values when creating the object:
class Person:
def __init__(self, name, age):
[Link] = name
[Link] = age
p1 = Person("Linus", 28)
print([Link])
print([Link])
Try it Yourself »
ADVERTISEMENT
REMOVE ADS
Default Values in __init__()
You can also set default values for parameters in the __init__() method:
Example
Set a default value for the age parameter:
class Person:
def __init__(self, name, age=18):
[Link] = name
[Link] = age
p1 = Person("Emil")
p2 = Person("Tobias", 25)
print([Link], [Link])
print([Link], [Link])
Try it Yourself »
Multiple Parameters
The __init__() method can have as many parameters as you need:
Example
Create a Person class with multiple parameters:
class Person:
def __init__(self, name, age, city, country):
[Link] = name
[Link] = age
[Link] = city
[Link] = country
p1 = Person("Linus", 30, "Oslo", "Norway")
print([Link])
print([Link])
print([Link])
print([Link])
Try it Yourself »
?
Exercise
What is the purpose of the __init__() method in Python classes?
To delete an object
To assign initial values to object properties
To print object information
Submit Answer »
‹ Previous Sign in to track progress Next ›
PLUS SPACES GET CERTIFIED FOR TEACHERS FOR BUSINESS CONTACT US
Top Tutorials Top References Top Examples Get Certified
HTML Tutorial HTML Reference HTML Examples HTML Certificate
CSS Tutorial CSS Reference CSS Examples CSS Certificate
JavaScript Tutorial JavaScript Reference JavaScript Examples JavaScript Certificate
How To Tutorial SQL Reference How To Examples Front End Certificate
SQL Tutorial Python Reference SQL Examples SQL Certificate
Python Tutorial [Link] Reference Python Examples Python Certificate
[Link] Tutorial Bootstrap Reference [Link] Examples PHP Certificate
Bootstrap Tutorial PHP Reference Bootstrap Examples jQuery Certificate
PHP Tutorial HTML Colors PHP Examples Java Certificate
Java Tutorial Java Reference Java Examples C++ Certificate
C++ Tutorial AngularJS Reference XML Examples C# Certificate
jQuery Tutorial jQuery Reference jQuery Examples XML Certificate
FORUM ABOUT ACADEMY
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly
reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use,
cookies and privacy policy.
Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by [Link].