Home » Python

class keyword with example in Python

Python class keyword: Here, we are going to learn about the class keyword with example.
Submitted by IncludeHelp, on April 14, 2019

Python class keyword

class is a keyword (case-sensitive) in python, it is used to define a class, when we need to define a class, before the class name - we must have to use class keyword.

Syntax of class keyword

    class class_name:
	    class definition statements;

Example:

    Input:
    # an empty class
    class sample:
        pass

    # printing its type
    print("type of class: ", type(sample))

    Output:
    type of class:  <class 'type'>

Python examples of class keyword

Example 1: Define an empty class using pass statement and print its type

# python code to demonstrate example of 
# class keyword 

# an empty class
class sample:
    pass

# main code
# printing it's type 
print("type of class: ", type(sample))

Output

type of class:  <class 'type'>

Example 2: Define a class student and assign values and print

# python code to demonstrate example of 
# class keyword 

# student class code in Python

# class definition
class Student:
    __id=0
    __name=""
    __course=""
	
	# function to set data 
    def setData(self,id,name,course):
        self.__id=id
        self.__name = name
        self.__course = course
	
	# function to get/print data
    def showData(self):
        print("Id\t:",self.__id)
        print("Name\t:", self.__name)
        print("Course\t:", self.__course)

# main function definition
def main():
    #Student class Object
    std=Student()
    std.setData(1,'Manju','M. Com.')
    std.showData()
	
if __name__=="__main__":
    main()

Output

Id      : 1
Name    : Manju
Course  : M. Com.



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.