Untitled Document
OOP PART - 1 ( CLASSES )
1. Creating Simple class Click Here
#********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ ***** 
# Creating Simple class
       
# Here we will create simple class and its instance 
# we cannot keep class declaration blank ; hence we have used pass keyword

class Employee:
	pass

emp_1 = Employee()
emp_2 = Employee()

print(emp_1)
print(emp_2)
       
2. Program - Creating Instance Variable Click Here
#********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
# Program - Creating Instance Variable  

# This is manual approach ; which has lot of repitative code 

class Employee:
	pass

emp_1 = Employee()
emp_2 = Employee()

print(emp_1)
print(emp_2)

emp_1.first = "Mangesh"
emp_1.last = "Pande"
emp_1.email = "Mangesh.Pande@company.com"
emp_1.pay = 50000

emp_2.first = "Test"
emp_2.last = "User"
emp_2.email = "Test.User@company.com"
emp_2.pay = 60000

print(emp_1.email)
print(emp_2.email)
       
3. Program - Creating Instance Varibale using init method Click Here
#********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
# Program - Creating Instance Varibale using init method  

# __init__ is a special method called as initialise or in other programming language called as Constructor
# In Python whenever we create a method in a class by default first argument is passed as instance(object) of class
# Now to read that instance (Object) we use self ( keyword) in method
       
class Employee:

	def __init__(self, first,last,pay):
		self.first = first
		self.last = last 
		self.pay = pay
		self.email = first + '.' + last +'@company.com'

emp_1 = Employee('Mangesh','Pande',50000)
emp_2 = Employee('Test','User',60000)

print(emp_1.email)
print(emp_2.email)
       
4. Program - Creating Your Own Methods inside a class Click Here
#********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
# Program - Creating Your Own Methods inside a class
# Here we have created fullname() method inside a class which will return fullname   
       
class Employee:

	def __init__(self,first,last,pay):
		self.first = first
		self.last = last
		self.pay = pay
		self.email = first + '.' + last +'@company.com'

	def fullname(self):
		return '{} {}'.format(self.first, self.last)

emp_1 = Employee('Mangesh','Pande',50000)
emp_2 = Employee('Test','User',60000)

print(emp_1.email)
print(emp_2.email)
print(emp_1.fullname())
       
5. Program - Executing Example - 4 Using class name Click Here
#********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
# Program - Executing Example - 4 Using class name
class Employee:

	def __init__(self,first,last,pay):
		self.first = first
		self.last = last
		self.pay = pay
		self.email = first + '.' + last +'@company.com'

	def fullname(self):
		return '{} {}'.format(self.first, self.last)

emp_1 = Employee('Mangesh','Pande',50000)
emp_2 = Employee('Test','User',60000)

print(emp_1.email)
print(emp_2.email)
print(emp_1.fullname())
# Now we will print emp_2 instance full nme using class name 
# Here we explicitly need to pass the instance ( object) to the method

print(Employee.fullname(emp_2))
       
6. Hint Click Here
# In our fullname method we have passed self so that instance of the class or object 
# can be read by the method and its a default argument in Python 
# Now if you remove self keyword from the method you will get the error 
# Try to execute below code and you will get the error messsage 

class Employee:

	def __init__(self,first,last,pay):
		self.first = first
		self.last = last
		self.pay = pay
		self.email = first + '.' + last +'@company.com'

	def fullname():
		return '{} {}'.format(self.first, self.last)

emp_1 = Employee('Mangesh','Pande',50000)
emp_2 = Employee('Test','User',60000)

print(emp_1.email)
print(emp_2.email)
print(emp_1.fullname())
# Now we will print emp_2 instance full nme using class name 
# Here we explicitly need to pass the instance ( object) to the method

print(Employee.fullname(emp_2))
       
Untitled Document