Functions
Built-in functions
Use built-in functions like max(), sum() and divmod().
Python's standard library gives us built-in functions, more functions in modules, and we can add our own user defined functions.
Built-in functions are ready to use, with no import needed: input(), print(), int(), float(), str(), list(), tuple(), dict(), set(), bool(), chr(), ord(), len(), range(), type(), abs(), divmod(), max(), min(), pow() and sum().
Some examples:
- abs(-8.5) is 8.5 (the value without its sign).
- divmod(17, 5) is (3, 2): the quotient and the remainder together.
- max([4, 9, 2]) is 9. For a string, max() compares character codes: max("Apple") is 'p', because small letters have bigger codes than capitals.
- pow(3, 2) is 9, and pow(3, 3, 5) is 27 % 5 = 2.
- sum([1, 2, 3]) is 6, and sum([1, 2, 3], 10) is 16 (it starts counting from 10).
- len("Patience") is 8.
Every function accepts some values and returns a result. input() accepts a prompt and returns a string; int() accepts a value and returns an integer; print() accepts values, displays them, and returns None.
Practise this lesson
Questions that get harder as you improve, a Tutor that helps without giving the answer away, and progress you can see. Free for every learner.
No account, no email address. Opens the real product.