Learn about functions in Python programming language.
Totting up the bill is basically calculating the sum of a list of numbers. This is such a common need that Python already provides a function for that, appropriately called sum. A function takes some data (the function’s input) and returns some other data (the function’s output). In this case, the sum function takes a list of numbers as input and returns a single number as output.
To apply a function to some data (whether it’s a variable, a number or a string), just write the name of the function followed by the data in parenthesis. The computer will calculate the function’s output, which can be used in further calculations or assigned to a variable.
As you can see, using the sum function shortens our code and makes it easier to understand, because the function’s name explicitly states what the code is doing. Good programmers don’t just write code, they write readable code. They know that code always changes to accommodate further customer requests, and trying to modify cryptic code you wrote some weeks or months ago is no fun. Using comments and descriptive names for variables and functions helps making code readable.
The length function
Counting the number of items in a list is also so common, that Python has a function for that too, called len, which returns the length of the list.
Change the code above to show the number of ordered items, using the len function.