Editor

Absolute Value

#6 · Python Basics · Beginner · 35 XP · about 4 min

Write a function called absolute(n) that returns the absolute value of a number (without using Python's built-in abs() function).

The absolute value of a number is its distance from zero, always positive.

Example

absolute(5)    # Returns 5
absolute(-5)   # Returns 5
absolute(0)    # Returns 0
absolute(-100) # Returns 100

Requirements

  • Do NOT use the built-in abs() function
  • Return the absolute value as a number
  • Handle both positive and negative inputs

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.