Python Calculate the Square of a Number n

Document 对象参考手册Python3 Examples

This simple Python program will calculate the square of a number n. The square refers to a number multiplied by itself.

Example

def calculate_square(n):
    return n * n

# Example usage
number = 4
result = calculate_square(number)
print(f"The square of {number} is {result}")

Code analysis:

  • def calculate_square(n):Defines a function namedcalculate_squarethat accepts a parametern。
  • return n * nThe function returnsnthe square value of
  • number = 4Defines a variablenumberand assigns it a value of 4.
  • result = calculate_square(number)Calls thecalculate_squarefunction and stores the result in the variableresult.
  • print(f"The square of {number} is {result}")Uses a formatted string to print the result.

Output result:

The square of 4 is 16

Document 对象参考手册Python3 Examples

Other extensions