_init_()method in python

Use the init() method to assign values to object properties, or other operations that are necessary to do when the object is being created.its a in built method.

Example:
class Person:
def init(self, name, age):
self.name = name
self.age=age
p1 = Person(“ABC”, 25)
print(p1.name)
print(p1.age)

2 Likes

Very good , keep it up.

class Person:

# init method or constructor

def __init__( self , name):

self .name = name

# Sample Method

def say_hi( self ):

print ( 'Hello, my name is' , self .name)

p = Person( 'Nikhil' )

p.say_hi()

3 Likes

[quote=“mayurpuranik9, post:1, topic:1720”]
Example:
class Person:
def init(self, name, age):
self.name = name
self.age=age
p1 = Person(“ABC”, 25)
print(p1.name)
print(p1.age) ```
class Student:
def init(self, name, age):
self.name = name
self.age = age
def displayInfo(self): # class method
print('Student Name: ‘, self.name,’, Age: ', self.age)

2 Likes

Amazing @prabhudevaraj11 . Keep practicing

@mayurpuranik9
Yes it’s a built in method and you can call it
Magic method
Dunder method
Constructor

1 Like

good @prabhudevaraj11 , keep it up.

my_list = [‘Siya’, ‘Tiya’, ‘Guru’, ‘Daksh’, ‘Riya’, ‘Guru’]

for i in range(len(my_list)):
print(my_list[i])
if my_list[i] == ‘Guru’:
print(‘Found the name Guru’)
break
print(‘After break statement’)

print(‘Loop is Terminated’)

1 Like

@prabhudevaraj11

Correct, keep it up.

1 Like