Sunday, November 18, 2018

Python Ninja Bootcamp 26-Oop Class

Learn Python like a Professional! Take You from 0 to Hero




In this Python lecture, we will learn about the Oop Class.

Python is the object oriented programming, consist class. Class is a blue-print to create the object.
 

Let's see the syntax of class


class Class_name:
   def __init__(self,attr,...):
       self.attr = attr

Classes are created by using class keyword

Class name starts with uppercase.

init is special method, called whenever the object is created. 

*Function define inside the class are called as method 

 

Let's see an example of class 


class Student:
    
    def __init__(self,sname,srollno,percentage):
        self.name = sname
        self.rollno = srollno
        self.percentage = percentage
        
    def result(self):
        if self.percentage > 30:
            print('Pass')
        else:
            print('Fail')


Now create the object to access the field


ob1 = Student('Tamta',1,90)

print(ob1.name)
print(ob1.rollno)
print(ob1.percentage) 
>>  Tamta
   1
   90


Now access the Result method


ob1.result()
>> Pass



Re-usability

ob2 =Student('Veeru',2,29)

print(ob2.name) 
print(ob2.rollno) 
print(ob2.percentage) 
>> Veeru
      2
      29

 

ob2.result()
>> Fail


EDITS ARE WELCOMED!!

In the next Blog, we will discuss inheritance. 

https://sngurukuls247.blogspot.com/2018/11/python-ninja-bootcamp-27-inheritance.html

......................................................................................................................................

Follow the link below to access Free Python Lectures-
https://www.youtube.com/channel/UCENc9qI7_r8KMf6-_1R1xnw

Instagram
https://www.instagram.com/python.india/
 

View the Jupyter Notebook for this lecture

Download the Jupyter Notebook for this lecture 




Feel free contact me on-


No comments:

Post a Comment