# 30 seconds of python

## Math

Click on a snippet's name to view its code.

#### average

Returns the average of two or more numbers.

Use `sum()`

to sum all of the `args`

provided, divide by `len(args)`

.

#### average_by

Returns the average of a list, after mapping each element to a value using the provided function.

Use `map()`

to map each element to the value returned by `fn`

.
Use `sum()`

to sum all of the mapped values, divide by `len(lst)`

.

#### clamp_number

Clamps `num`

within the inclusive range specified by the boundary values `a`

and `b`

.

If `num`

falls within the range, return `num`

.
Otherwise, return the nearest number in the range.

#### degrees_to_rads

Converts an angle from degrees to radians.

Use `math.pi`

and the degrees to radians formula to convert the angle from degrees to radians.

#### digitize

Converts a number to an array of digits.

Use `map()`

combined with `int`

on the string representation of `n`

and return a list from the result.

#### factorial

Calculates the factorial of a number.

Use recursion.
If `num`

is less than or equal to `1`

, return `1`

.
Otherwise, return the product of `num`

and the factorial of `num - 1`

.
Throws an exception if `num`

is a negative or a floating point number.

#### fibonacci

Generates an array, containing the Fibonacci sequence, up until the nth term.

Starting with `0`

and `1`

, use `list.append() to add the sum of the last two numbers of the list to the end of the list, until the length of the list reaches`

n```
.
If
```

n`is less or equal to`

0`, return a list containing`

0`.

#### gcd

Calculates the greatest common divisor of a list of numbers.

Use `reduce()`

and `math.gcd`

over the given list.

#### in_range

Checks if the given number falls within the given range.

Use arithmetic comparison to check if the given number is in the specified range.
If the second parameter, `end`

, is not specified, the range is considered to be from `0`

to `start`

.

#### is_divisible

Checks if the first numeric argument is divisible by the second one.

Use the modulo operator (`%`

) to check if the remainder is equal to `0`

.

#### is_even

Returns `True`

if the given number is even, `False`

otherwise.

Checks whether a number is odd or even using the modulo (`%`

) operator.
Returns `True`

if the number is even, `False`

if the number is odd.

#### is_odd

Returns `True`

if the given number is odd, `False`

otherwise.

Checks whether a number is even or odd using the modulo (`%`

) operator.
Returns `True`

if the number is odd, `False`

if the number is even.

#### lcm

Returns the least common multiple of two or more numbers.

Define a function, `spread`

, that uses either `list.extend()`

or `list.append()`

on each element in a list to flatten it.
Use `math.gcd()`

and `lcm(x,y) = x * y / gcd(x,y)`

to determine the least common multiple.

#### max_by

Returns the maximum value of a list, after mapping each element to a value using the provided function.

Use `map()`

with `fn`

to map each element to a value using the provided function, use `max()`

to return the maximum value.

#### median

Finds the median of a list of numbers.

Sort the numbers of the list using `list.sort()`

and find the median, which is either the middle element of the list if the list length is odd or the average of the two middle elements if the list length is even.

#### min_by

Returns the minimum value of a list, after mapping each element to a value using the provided function.

Use `map()`

with `fn`

to map each element to a value using the provided function, use `min()`

to return the minimum value.

#### rads_to_degrees

Converts an angle from radians to degrees.

Use `math.pi`

and the radian to degree formula to convert the angle from radians to degrees.

#### sum_by

Returns the sum of a list, after mapping each element to a value using the provided function.

Use `map()`

with `fn`

to map each element to a value using the provided function, use `sum()`

to return the sum of the values.