Python Simple program of a class (Input and print a number)

Here, we are composing a straightforward class program in Python that will include a number and print the number utilizing class.

We need to characterize a class that will start a number, input a number and print the number in Python.

Here, we are characterizing a class named Number – which has an open variable named num, in the class, there are 3 strategies:

init :

To instate the variable, it functions as development in C++, java.

inputNum() :

This technique will ask the client to enter the worth.

printNum() :

This strategy will print number (num).

Program:

# class definition 
class Number:
	# __init__ method just like a constructor 
	def __init__(self, num):
		self.num = num;

	# method to take input from user 
	def inputNum(self):
		self.num = int(input("Enter an integer number: "))

	# method to print the number
	def printNum(self):
		print "num:", self.num

# main code 
# declare object of the class 'Number'
objN = Number(100);
objN.printNum()

# input from user 
objN.inputNum()
objN.printNum()

Output:

num: 100
Enter an integer number: 200
num: 200

Leave a Comment

error: Alert: Content is protected!!