abs() and round() in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time it takes to run abs() and round() changes as the input size changes.
Specifically, how does the cost grow when we use these functions on numbers or lists?
Analyze the time complexity of using abs() and round() on a list of numbers.
numbers = [3.14, -2.71, 0, 7.5, -1.2]
results = []
for num in numbers:
a = abs(num)
r = round(num)
results.append((a, r))
This code takes each number, finds its absolute value and rounds it, then stores the results.
Look at what repeats as the input grows.
- Primary operation: Looping through each number in the list.
- How many times: Once for each number in the list (n times).
As the list gets bigger, the number of times we call abs() and round() grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 calls (2 per number) |
| 100 | About 200 calls |
| 1000 | About 2000 calls |
Pattern observation: The total work grows directly with the number of items.
Time Complexity: O(n)
This means the time to finish grows in a straight line as the list gets longer.
[X] Wrong: "abs() and round() take longer on bigger numbers, so time grows with number size."
[OK] Correct: These functions run in constant time regardless of the number's value; time depends on how many numbers you process, not their size.
Understanding how simple built-in functions behave with input size helps you explain your code clearly and think about efficiency in real tasks.
What if we applied abs() and round() to a nested list of numbers instead of a flat list? How would the time complexity change?
Practice
abs() do when given a number?Solution
Step 1: Understand the purpose of
Theabs()abs()function returns how far a number is from zero without considering its sign.Step 2: Compare with other options
Rounding, string conversion, or negation are different functions; onlyabs()returns the positive distance.Final Answer:
Returns the positive distance of the number from zero -> Option DQuick Check:
abs(-5) = 5 [OK]
- Confusing abs() with round()
- Thinking abs() changes the number's sign
- Assuming abs() converts to string
Solution
Step 1: Check the syntax of round()
Theround()function takes a number and an integer for decimal places, likeround(number, digits).Step 2: Validate each option
round(3.14159, 2) uses correct syntax with an integer 2. round(3.14159.2) has a dot instead of a comma. round(3.14159, '2') uses a string '2' which is invalid. round(3.14159, 2.0) uses a float 2.0 which is accepted as decimal places.Final Answer:
round(3.14159, 2) -> Option CQuick Check:
round(3.14159, 2) = 3.14 [OK]
- Using dot instead of comma between arguments
- Passing decimal places as string
- Passing decimal places as float
print(abs(-7.8)) print(round(2.675, 2))
Solution
Step 1: Calculate abs(-7.8)
The absolute value of -7.8 is 7.8, so the first print outputs 7.8.Step 2: Calculate round(2.675, 2)
Due to floating-point rounding, 2.675 rounds to 2.67 (not 2.68) in Python.Final Answer:
7.8 and 2.67 -> Option BQuick Check:
abs(-7.8) = 7.8, round(2.675, 2) = 2.67 [OK]
- Expecting round(2.675, 2) to be 2.68
- Forgetting abs() returns positive value
- Mixing signs in output
num = -4.5 print(round(abs(num), 1.0))
Solution
Step 1: Analyze abs(num)
Theabs()function works fine with negative numbers, so no error here.Step 2: Check round() arguments
The second argument toround()must be an integer, but 1.0 is a float, causing a TypeError.Final Answer:
round() decimal places must be an integer, not float -> Option AQuick Check:
round(..., 1.0) causes error [OK]
- Thinking abs() can't take negatives
- Passing float instead of int for decimals
- Assuming print() syntax error
nums = [-3.1415, 2.718, -1.414, 0]. You want to create a new list with the absolute values rounded to 2 decimal places. Which code does this correctly?Solution
Step 1: Understand the goal
We want absolute values first, then round them to 2 decimals.Step 2: Check each option
rounded = [round(abs(x), 2) for x in nums] correctly applies abs() then round() with integer 2. rounded = [round(x, 2) for x in nums] rounds but does not apply abs(), leaving negative values negative. rounded = [round(abs(x), '2') for x in nums] uses string '2' which is invalid. rounded = [round(abs(x), 2.0) for x in nums] uses float 2.0 which is accepted as decimal places.Final Answer:
rounded = [round(abs(x), 2) for x in nums] -> Option AQuick Check:
abs then round with int decimals [OK]
- Rounding before abs() changes results
- Using string or float for decimal places
- Confusing order of functions
