Unlock the Potential of Programming: Coding for Solutions

Tuesday, January 18, 2022

01lecture

Installation of jupyter via anaconda by the following link
https://www.anaconda.com/products/individual

Three important terms in OOPs
1.  Class
2.  Object
3.  Reference Variable

Class

i. In Python every thing is an object. To create objects we required some Model or Plan or Blue print.
   which is nothing but class. 
ii. We can write a class to represent properties (attributes) and actions (behaviour) of object.
iii. Properties can be represented by variables 
iv. Actions can be represented by Methods.
Hence class contains both variables and methods.

Object:

Pysical existence of a class is nothing but object. We can create any number of objects for a class.
Syntax to Create Object: referencevariable = classname()
Example: s = Student()


Reference Variable

The variable which can be used to refer object is called reference variable. By using reference variable, we can 
access properties and methods of object.

Constructor Concept:

1-Constructor is a special method in python.
2-The name of the constructor should be __init__(self) 
3-Constructor will be executed automatically at the time of object creation. 
4-The main purpose of constructor is to declare and initialize instance variables. 
5-Constructor can take atleast one argument(atleast self) 
6-Constructor is optional and if we are not providing any constructor then python will provide default constructor.

Self Variable:

self is the default variable which is always pointing to current object 
By using self we can access instance variables and instance methods of object.
self should be first parameter inside constructor def __init__(self): 
self should be first parameter inside instance methods def talk(self):

Methods vs Constructor

           Methods                                                       Constructor

1) Name of method can be any name                               1) Constructor name should be always __init__ 
2) Method will be executed if we call that method               2) Constructor will be executed automatically at 
                                                                   the time of object creation.

3) Per object, method can be called any number  of times.       3) Per object, Constructor will be executed only 
                                                                   once.

4) Inside method we can write business logic                    4) Inside Constructor we have to declare and 
                                                                   initialize instance variables 
from IPython import display 
display.Image("01.png")
There are many real-world examples of classes as explained below -
1-Recipe of Omelette is a class. Omelette is an object.

2-Bank Account Holder is a class. Attributes are First Name, Last Name, Date of Birth, Profession, Address etc. 
Methods can be "Change of address", "Change of Profession", " Change of last name" etc. "Change of last name" 
is generally applicable to women when they change their last name after marriage.

3-Dog is a class. Attributes are Breed, Number of legs, Size, Age, Color etc. Methods can be Eat, Sleep, Sit, 
Bark, Run etc.

Print the name of student,age of student and mark of the student by using OOP

class Student(): # name of class is Student
    def __init__(self):  # actions/method/constructor (four times space)
        self.name = "Zain"  #(8 times space) attributes ---->> name
        self.age = 19    #(8 times space)  attributes ---->> age
        self.marks = 95   #(8 times space) attributes ---->> marks
        
# print(self.name)  we can not use this syntax to print the name in OOP  
# print(self.age)  we can not use this syntax to print the name in OOP 
# print(self.marks)  we can not use this syntax to print the name in OOP 

s = Student()   # s is the reference variable  and s = Student() is the object
print(s.name)  # recall the name  or recall the 1st attribute
print(s.age)  # recall the age  or recall the 2nd attribute
print(s.marks)  # recall the marks  or recall the 3rd attribute      
Zain
19
95
class Student(): # name of class is Student
    def __init__(self):  # actions/method/constructor (four times space)
        self.name = "Zain"  #(8 times space) attributes ---->> name
        self.age = 19    #(8 times space)  attributes ---->> age
        self.marks = 95   #(8 times space) attributes ---->> marks
        self.gender = "male"
        

s = Student()   # s is the reference variable  and s = Student() is the object
print(s.name)  # recall the name  or recall the 1st attribute
print(s.age)  # recall the age  or recall the 2nd attribute
print(s.marks)  # recall the marks  or recall the 3rd attribute
print(s.gender)  # recall the marks  or recall the 3rd attribute
Zain
19
95
male

class Student():  # class--->> name of class is Student
    def __init__(self):       # actions/behaviour/method --- but this special type of method known as constructor
        self.name = "zain"   # properties/instance variable/attributes --->> name
        self.age = 19        # properties/instance variable/attributes --->> age
        self.marks = 95     # properties/instance variable/attributes --->> marks
        
    def display(self):      # actions/behaviour/method/instance method
        print("Hello my name: ",self.name)
        print("My age: ",self.age)
        print("My marks: ",self.marks)
        
s=Student()  # s is the reference variable and s=Student() is the object
print(s.name)  # recall the "name" attribute
print(s.age)  # recall the "age" attribute
print(s.marks) # recall the marks attribute

print("=========================================")

s.display()   # recall the 2nd method i.e display   

#NOTE: This is not recommended because we used hard coding.
zain
19
95
=========================================
Hello my name:  zain
My age:  19
My marks:  95
class Student():  # class--->> name of class is Student
    def __init__(self,name,age,marks): # actions/behaviour/method --- but this special type of method known as constructor
        self.name = name   # properties/instance variable/attributes --->> name
        self.age = age        # properties/instance variable/attributes --->> age
        self.marks = marks     # properties/instance variable/attributes --->> marks
        
    def display(self):      # actions/behaviour/method/instance method
        print("Hello my name: ",self.name)
        print("My age: ",self.age)
        print("My marks: ",self.marks)
        
s1=Student("Zain",19,95)  # s1 is the reference variable and s1=Student() is the  1st object
print(s1.name)  # recall the "name" attribute
print(s1.age)  # recall the "age" attribute
print(s1.marks) # recall the marks attribute

print("=========================================")

s1.display()   # recall the 2nd method i.e display 

print("\n")

s2=Student("Mike",21,96)  # s2 is the reference variable and s2=Student() is the  2nd object
print(s2.name)  # recall the "name" attribute
print(s2.age)  # recall the "age" attribute
print(s2.marks) # recall the marks attribute

print("=========================================")

s2.display()   # recall the 2nd method i.e display   
Zain
19
95
=========================================
Hello my name:  Zain
My age:  19
My marks:  95


Mike
21
96
=========================================
Hello my name:  Mike
My age:  21
My marks:  96

class Student():  # class--->> name of class is Student
    def __init__(self,name,age,marks): # actions/behaviour/method --- but this special type of method known as constructor
        self.name = name   # properties/instance variable/attributes --->> name
        self.age = age        # properties/instance variable/attributes --->> age
        self.marks = marks     # properties/instance variable/attributes --->> marks
        
    def display(self):      # actions/behaviour/method/instance method
        print("Hello my name: ",self.name)
        print("My age: ",self.age)
        print("My marks: ",self.marks)
        
s1=Student("Zain",19,95)  # s1 is the reference variable and s1=Student() is the  1st object
print(s1.name,s1.age,s1.marks)  # recall the  attributes


print("=========================================")

s1.display()   # recall the 2nd method i.e display 

print("\n")

s2=Student("Mike",21,96)  # s2 is the reference variable and s2=Student() is the  2nd object
print(s2.name,s2.age,s2.marks)  # recall the attribute


print("=========================================")

s2.display()   # recall the 2nd method i.e display   
Zain 19 95
=========================================
Hello my name:  Zain
My age:  19
My marks:  95


Mike 21 96
=========================================
Hello my name:  Mike
My age:  21
My marks:  96
class Student():  # class--->> name of class is Student
    def __init__(self,name,age,marks): # actions/behaviour/method --- but this special type of method known as constructor
        self.name = name   # properties/instance variable/attributes --->> name
        self.age = age        # properties/instance variable/attributes --->> age
        self.marks = marks     # properties/instance variable/attributes --->> marks
        
    def display(self):      # actions/behaviour/method/instance method
        print("Hello my name:{},My marks:{}".format(self.name,self.marks))
      
        
s1=Student("Zain",19,95)  # s1 is the reference variable and s1=Student() is the  1st object
s1.display()   # recall the 2nd method i.e display 

print("\n")

s2=Student("Mike",21,96)  # s2 is the reference variable and s2=Student() is the  2nd object
s2.display()   # recall the 2nd method i.e display   
Hello my name:Zain,My marks:95


Hello my name:Mike,My marks:96
class Student():  # class--->> name of class is Student
    def __init__(self,name,age,marks): # actions/behaviour/method --- but this special type of method known as constructor
        self.name = name   # properties/instance variable/attributes --->> name
        self.age = age        # properties/instance variable/attributes --->> age
        self.marks = marks     # properties/instance variable/attributes --->> marks
        
    def display(self):      # actions/behaviour/method/instance method
        print("Hello my name:{}\nMy marks:{}".format(self.name,self.marks))
      
        
s1=Student("Zain",19,95)  # s1 is the reference variable and s1=Student() is the  1st object
s1.display()   # recall the 2nd method i.e display 

print("\n")

s2=Student("Mike",21,96)  # s2 is the reference variable and s2=Student() is the  2nd object
s2.display()   # recall the 2nd method i.e display 
Hello my name:Zain
My marks:95


Hello my name:Mike
My marks:96
class Student():  # class--->> name of class is Student
    def __init__(self,name,age,marks): # actions/behaviour/method --- but this special type of method known as constructor
        self.name = name   # properties/instance variable/attributes --->> name
        self.age = age        # properties/instance variable/attributes --->> age
        self.marks = marks     # properties/instance variable/attributes --->> marks
        
    def display(self):      # actions/behaviour/method/instance method
        print("Name:{}\nAge:{}\nMarks:{}".format(self.name,self.age,self.marks))
      
        
s1=Student("Zain",19,95)  # s1 is the reference variable and s1=Student() is the  1st object
s1.display()   # recall the 2nd method i.e display 

print("\n")

s2=Student("Mike",21,96)  # s2 is the reference variable and s2=Student() is the  2nd object
s2.display()   # recall the 2nd method i.e display 
Name:Zain
Age:19
Marks:95


Name:Mike
Age:21
Marks:96
 

No comments:

Post a Comment