Class in Python

Introductions of Class:

Class:
  • Class is used to hold data members and member function i.e. variables and functions.
  • It can be easily accessed anywhere and used to reduce the reusability of the code.
  • The class name is a user-defined word.
For example,
  • Let’s take student,
  • Student properties are all called variables and student performance or student-related calculations(i.e. student result based on total and it may be pass or fail) is called as a function.
Where,
The student is a class.
Student properties (variables) : Roll_num,name,age,ect.
Student behaviours(function)  : total,average,result,fees details ect.
Note:
  • If one or more variables declared within the class, called data members.
  • If one or more functions declared within the class, called member function.
Syntax:
  1. import module_name#optional

    class class_name:

        def function_name(list_of_patameters):

            #Statements

        function_name(list_of_patameters)#Function calling

Example program:

  1. class calculator:

        def add(x,y):

            return x+y

        def sub(x,y):

            return x-y

        def mul(x,y):

            return x*y

        def div(x,y):

            return x/y

        a,b=input("Enter two values: ").split()

        print("Add value:",add(int(a),int(b)))

        print("Sub value:",sub(int(a),int(b)))

        print("Mul value:",mul(int(a),int(b)))

        print("Div value:",div(int(a),int(b)))

Output:

Share:

No comments:

Post a Comment

Popular Posts

Search This Blog

Powered by Blogger.

Service Support

Need our help to Learn or Post New Concepts Contact me

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me