Quick Overview
`minutes * 60` returns an `int` when given an `int`, while `/` always produces a float regardless of the operands. Multiplication preserves the type; division does not.
Write a Python function convert(minutes) that takes an integer number of minutes and converts it into total seconds.
Unit conversions are common in application development, such as calculating session timeouts, game timers, and API rate limits.
Since there are 60 seconds in 1 minute, multiply the input minutes by 60:
def convert(minutes):
return minutes * 60
# Testing our function
print(convert(5)) # Output: 300
print(convert(3)) # Output: 180
print(convert(2)) # Output: 120
In Python, multiplying an integer by an integer yields an int (e.g. 5 * 60 == 300). If you multiply by a float (60.0), Python returns a float (300.0).
if minutes < 0: return 0.Use simple arithmetic for unit conversions. Next, let's calculate the area of a triangle!
A function with no `return` hands back `None`, so a forgotten one surfaces as a `TypeError` at the call site rather than at the line that actually caused it.
`base * height / 2` always yields a float in Python 3 because `/` never returns an int, even for whole numbers. `//` floors instead, which is almost never what an area wants.
Python integers have no fixed width, so `n + 1` never overflows the way a 32-bit `int` would. The value simply grows to whatever memory allows.
Average 5.0 by 1 learner