Python Compute the Square of a Number n
This simple Python program computes the square of a number n. Squaring means multiplying a number by itself.
Example
def
calculate_square
(
n
)
:
return
n * n
Example usage
number
=
4
result
=
calculate_square
(
number
)
(
f
"The square of {number} is {result}"
)
Code analysis:
def calculate_square(n):defines a class namedcalculate_squarefunction, which takes a parametern。return n * nthe function returnsnthe squared value of .number = 4Define a variablenumberand assign it the value 4.result = calculate_square(number)calledcalculate_squarefunction and store the result in the variableresultit.print(f"The square of {number} is {result}")Print the result using a formatted string.
Output result:
The square of 4 is 16
Python3 Examples