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

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL







Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.