Friday, April 16, 2021

Python Program to create a Static Method - with sample code

 

Python Program to create a Static Method

 

# A Python Program can be used to create a static method which can be used to calculate the square root value of a given number

static method for finding square root value

 

import math

class Sample:

@staticmethod

def calculate(x):

result = math.sqrt(x)

return result

 

# accept a number from keyboard

num = float(input('Enter a number:'))

# call the static method and pass the number

res = Sample.calculate(num)

print(' The square root of {} is {:.2f}'.format(num,res))

 

Output

Enter a number : 49

The square root of 49 is 7

 

No comments:

Post a Comment