Class Introduction.py

# A Class is like an object constructor
# A class is a user-defined blueprint for Creating Object
# class (Class Name) :
# Statement or Attributes

# Attributes --->
# 1) Properties
# 2) Method/Constructor

# Class Create and Operation
class student :
name = "Aubdur Rob Anik"
roll = 18101073

def write(self): # Here self means object name.
print("Student Name is :: ", self.name)
print("Student Roll is :: ", self.roll)

#When an object of a class is created, the class is said to be instantiated
std1 = student()
std1.write() #-----> It's Call Like : student.write(std1)

# Method Use For Initialization Properties
# By built-in Function
class student :
def __init__(self, name, semester) :
self.name = name #properties
self.semester = semester #properties

def write(self):
print("Student Name is :: ", self.name)
print("Student Semester is :: ", self.semester)

st1 = student("Aubdur Rob Anik", "1-1") #Constructor
st1.write()

# Another Way
# Method Use For Initialization
# By User Defined Function
class person :
def set_value(p1, name, age):
p1.name = name # Not Properties
p1.age = age # Not Properties

def write(p1):
print("Person Name :: ", p1.name)
print("Person Age :: ", p1.age)

person1 = person()
person1.set_value("Aubdur Rob Anik", 14)
person1.write()

#Class Variable
class person :
occupation = "Student" #Class Variable

def __init__(self, name, age):
self.name = name
self.age = age

p1 = person("Aubdur Rob Anik", 14)

print("Person 1 is a :: ", p1.occupation)
print("Person 1 Name :: ", p1.name)
print("Person 1 Age :: ", p1.age)

#Return Value From A Class method
class dress :
def __init__(self, size):
self.size = size

def setcolor(self, color):
self.color = color

def getcolor(self):
return self.color

pant = dress("M")
pant.setcolor("Black")
print("Pant Color is :: ", pant.getcolor())

# __dict__ is a built in function
# __dict__ function is a Dictionary of class and object #attribute save as a dictionary.
class person :
def __init__(self, name, age):
self.name = name
self.age = age

def write(self):
print("Person Name :: ", self.name)
print("Person Age :: ", self.age)

p1 = person(
"Aubdur Rob Anik", 14)
p2 = person(
"Jafrin", 13)

print()
print(person.__dict__)
print(p1.__dict__)
print(p2.__dict__)

#object property add
class person :
occupation =
"Student"

def __init__(self, name, age):
self.name = name
self.age = age

def write(self):
print("Name :: ", self.name)
print("Occupation :: ", self.occupation)
print("Age :: ", self.age)

p1 = person(
"Aubdur Rob Anik", 14)
p1.roll =
18101073
p1.email = "anik13331@gmail.com"

p1.write()
print("Roll :: ", p1.roll)
print("Email :: ", p1.email)


#Attribute Input
class information :
university =
"UAP"

def __init__(self, name, id, department):
self.name = name
self.id = id
self.department = department

def write(self):
print("Name :: ", self.name)
print("University :: ", self.university)
print("ID :: ", self.id)
print("Department :: ", self.department)


std1 = information(
input("\nName :: "), input("ID :: "), input("Department :: "))
std2 = information(
input("\nName :: "), input("ID :: "), input("Department :: "))
std3 = information(
input("\nName :: "), input("ID :: "), input("Department :: "))

std1.write()
std2.write()
std3.write()


#__str__ method
# The __str__ method in Python classes is used to define the string
# representation of an object. When you call the built-in str() 
# function or use print() on an object, Python internally invokes 
# the __str__ method of that object to determine how it should 
# be represented as a string
class information:
    university = "UAP"

    def __init__(self, name, age, department):
self.name = name
self.age = age
self.department = department def __str__(self): return f"{self.name}-{self.age}-{self.department}-{self.university}" p = information(input("Name : "), input("Age: "), input("Department: ")) print("Student Information : ", p)

Comments