How to Create Class in Python - learnit

Home Top Ad

Post Top Ad

Thursday, July 23, 2020

How to Create Class in Python

How to Create Class in Python

How to Create Class in Python

    Python is a general-purpose, versatile, and powerful programming language. It is a great first language because it’s concise and easy to read. Whatever you want to do, Python can do it. From web development to machine learning to data science, Python is the language for you.


    Python is a general-purpose, versatile, and powerful programming language. It is a great first language because it is concise and easy to read. Whatever you want to do, Python can do it. From web development to machine learning to data science, Python is the language for you.


    Classes provide a means of bundling data and functionality together. Creating a new class creates a new type of object, allowing new instances of that type to be made. Each class instance can have attributes attached to it for maintaining its state. Class instances can also have methods (defined by its class) for modifying its state.


    Compared with other programming languages, Python’s class mechanism adds classes with a minimum of new syntax and semantics. It is a mixture of the class mechanisms found in C++ and Modula-3. Python classes provide all the standard features of Object-Oriented Programming: the class inheritance mechanism allows multiple base classes, a derived class can override any methods of its base class or classes, and a method can call the method of a base class with the same name. Objects can contain arbitrary amounts and kinds of data. As is true for modules, classes partake of the dynamic nature of Python: they are created at runtime and can be modified further after creation.


Python Objects and Classes


Python is an object oriented programming language. Unlike procedure-oriented programming, where the main emphasis is on functions, object oriented programming stresses on objects.

An object is simply a collection of data (variables) and methods (functions) that act on those data. Similarly, a class is a blueprint for that object.

Defining a Class in Python

Function definitions begin with the def keyword in Python, class definitions begin with a class keyword.


Class MyTest:

    '''This is My Created a New Class'''
        pass:


Constructors in Python

    Class functions that begin with double underscore __ are called special functions as they have special meaning.Of one particular interest is the __init()__ function. This special function is called whenever a new object of that class is instantiated.


    This type of function is also called constructors in Object Oriented Programming (OOP). We normally use it to initialize all the variables.


Example
class customer:
    def __init__ (customers, name,gender,age,position):
        customers.name=name
        customers.gender=gender
        customers.age=age
        customers.position=position
    def information(self):
        print("Your name is "+self.name)
        print("Gender=" +self.gender)
        print(self.age)
        print(self.position)
object1=customer("Vannaro","Male",40,"Teacher")
object1.information()

Please Watching My Video is Below

No comments:

Post a Comment

Post Top Ad