Part of the series

Several example codes

~1 min read • Updated Sep 30, 2025

Program Overview

This Python program reads a number from the user and calculates two values:
- The square of the number
- The inverse (reciprocal) of the number
It then prints both results clearly.


Python Code:


# Get input from user
num = float(input("Enter a number: "))

# Calculate square and inverse
square = num ** 2
inverse = 1 / num if num != 0 else "Undefined (division by zero)"

# Display results
print("\n--- Result ---")
print(f"Square: {square}")
print(f"Inverse: {inverse}")

Sample Output:


Enter a number: 4  

--- Result ---  
Square: 16.0  
Inverse: 0.25

Explanation:

- The number is converted to float to support decimals
- The square is calculated using exponentiation (** 2)
- The inverse is calculated as 1 / num, with a check to avoid division by zero
- Results are printed with clear labels


Written & researched by Dr. Shahin Siami