Python Programs | IT Developer
IT Developer

Python Programs



Share with a Friend

Python Programs - Conditional Statements

Find the absolute value of a number - Python Program

Example 1 :

n = float(input("Enter a number: ")) if n < 0: print("Absolute value:", -n) else: print("Absolute value:", n)

Output

 
OUTPUT 1 :
Enter a number: -4.5
Absolute value: 4.5 

OUTPUT 2 :
Enter a number: 5
Absolute value: 5.0

Example 2 :

# Get input from the user number = float(input("Enter a number: ")) # Calculate the absolute value using abs() absolute_value = abs(number) # Print the result print(f"The absolute value of {number} is: {absolute_value}")

Output

 
OUTPUT 1 :
Enter a number: -12.5
The absolute value of -12.5 is: 12.5

OUTPUT 2 :
Enter a number: 7
The absolute value of 7.0 is: 7.0