×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Python class Keyword

By IncludeHelp Last updated : December 07, 2024

Description and Usage

The 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

Syntax of class keyword:

class class_name:
  class definition statements;

Sample Input/Output

Input:
# an empty class
class sample:
  pass

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

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

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.