Py-Tut-007

Back to Learning Python

Built-in Functions
We have already seen some examples of built-in functions in Python such as "int", "float", and "raw_input". For now, we will introduce the following new functions using lists only.
 * len, returns how many items in a list
 * max, returns the largest item in a list
 * min, returns the smallest item in a list
 * sorted, returns the same list sorted from smallest to largest

Let's create a new list of numbers from 1 to 12 in random order. Let's also use our list of the months from our Loops example to test out these new built-in functions.

code format="python" myNumbers = [3, 12, 1, 10, 11, 8, 4, 9, 7, 6, 2, 5] months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]

print "The len of myNumbers is:", len(myNumbers) print "The len of months is:", len(months) print print "The min of myNumbers is:", min(myNumbers) print "The min of months is:", min(months) print print "The max of myNumbers is:", max(myNumbers) print "The max of months is:", max(months) print print "The sorted of myNumbers is:", sorted(myNumbers) print "The sorted of months is:", sorted(months)

code



Assignment 7a
Explain the results of each len, min, max, and sorted from the example above. Why is the result of "min(months)" equal to "April"? Why is the result of "max(months)" equal to "September"? Explain relation between the len of a list and the index values for a list.

Assignment 7b
Write a program that inputs a list of items from a user. Use each of the len, min, max, sorted functions to display information from the list.

Back to Learning Python