Quick Reference Pages
 Operators
 Builtin Functions (you are here)
 Common Methods
 Builtin Modules
 Turtle graphics
 Music synthesis
Builtin Functions Reference
Besides operators, some of the most important buildingblocks
of Python programs are builtin functions. These represent important ways
to convert or deduce information and a few of them, like
print
are going to appear in almost every program you write.
To use a builtin function, we write the function name followed by parentheses. Any inputs that the function needs are placed in the parentheses, separated by commas. This notation is called a "function call" and it looks like this:
len("hello")
In this example we've used the len
function, and the result
will be the integer 5
. When used as part of a larger program, if the
function has a result (which most do) then we'd usually store that result
in a variable, like this:
myString = "What a lovely day!"
howLong = len(myString)
Here we also used a variable as the input, rather than a fixed value. You could also use a function as part of a larger expression: the place where you write the function call will eventually be simplified to the result value from that function call. Here's an example of a program that uses several builtin functions:
x = 5
y = 4
z = 10
print(max(x, y, z) + min(x, y, z))
The last expression simplifies as follows:
print(max(x, y, z) + min(x, y, z))
print(max(5, y, z) + min(x, y, z)) # variable substitution for x
print(max(5, 4, z) + min(x, y, z)) # variable substitution for y
print(max(5, 4, 10) + min(x, y, z)) # variable substitution for z
print(10 + min(x, y, z)) # max function result
print(10 + min(5, y, z)) # variable substitution for x
print(10 + min(5, 4, z)) # variable substitution for y
print(10 + min(5, 4, 10)) # variable substitution for z
print(10 + 4) # min function result
print(14) # + operator
None # print function result; this result won't show up
Note that as a side effect of this simplification, the text "14" will be printed.
The table below includes very brief reference explanations for the
builtin functions that we'll use in this class, along with tiny examples
of how they can be used correctly (feel free to paste those examples into
your Python shell to see what the results are and doublecheck your
understanding). This is meant as a quick reference guide, not a thorough
explanation of all of the details. Remember you can always use the
help
function to get more information about another function.
Reference
Help Functions  
Function  Effect  Example(s) 

help

This function is designed to accept another function as an argument, although you can also give it a string and it will tell you about the thing that you named. It brings up documentation for whatever function you give it, or if you call it without an argument, it starts an interactive help system. Use it to quickly remind yourself of how a particular function works. 
help() help(len) help('len')

dir

Returns a list of strings showing all of the attributes that a particular value (or type) has available. These attributes include any methods the value might have, which are functions that can be called using that value with a '.' after it, e.g., 'ABC'.lower() . You can use this to help remind you what's possible with a particular type of object, like a string or a list.

dir(str)

Input/Output Functions  
Function  Effect  Example(s) 
print

Displays text in the program output area. If we were to publish our programs for nonprogrammers to use, they would only see what the program prints. Conversely, printed values are just displayed as text, they do not become a string value that Python can use for further computation. print is a special function that accepts any number of arguments. With zero arguments, it just prints a blank line. With one or more arguments, it converts each argument to a string, and then displays each of those strings, with a space in between. Advanced: you can use the sep= keyword argument to control what gets printed in between, and the end= keyword argument to control what gets printed at the end (default is a new line character which is why each print call displays text on a new line).

print(1, 2.0, 'three') print(1, 2, 3, sep='')

input

Displays a prompt (just like print displays text) and then waits for whoever is running the program to type in some text, which becomes the result of the input call within Python. This allows us to have interactive programs that respond to the user's input. Since the function has a result (a string) you'll want to store that result in a variable. You might also want to convert it from a string to some other type, for example if you're asking for a number. Note: leaving a space at the end of your prompt helps ensure that the user won't type a space at the start of their input.

name = input('What is your name? ') fav = float(input('What's your favorite number? '))

Numerical Functions  
Function  Effect  Example(s) 
max

Returns the largest from among the values it was given as arguments, or if there's just one argument and it's a sequence, returns the largest value in that sequence. When dealing with strings, alphabetic ordering is used to determine the 'largest' value. 
larger = max(x, y) last = max('Zebra', 'Aardvark') largest = max([123, 12, 1234])

min

Works just like max , except it returns the smallest value instead of the largest one.

smaller = min(x, y) first = min('Zebra', 'Aardvark') smallest = min([123, 12, 1234])

round

Returns a roundedoff version of the number you give it. With just one argument, it will return the nearest integer. With two, the second argument is an integer that specifies how many decimal places to round to (and the result will always be a floatingpoint number). 
show = round(x, 2)

math.ceil

Returns the smallest integer that's greater than or equal to the number you give it. You'll need to include the line import math to use this function. Whereas round rounds to the nearest integer and int chops off the decimal part, math.ceil always rounds up.

four = math.ceil(3.01)

math.floor

Works like math.ceil but rounds down (towards negative infinity) instead of up. Note that for positive numbers, int does the same thing, but for negative numbers, they're different.

four = math.floor(4.99)

Type Functions  
Function  Effect  Example(s) 
Note 
The type conversion functions do not change the value of a variable. Instead, you give them one value, and they give back a converted value as a result. So for example, you would write code like number = int('123') to capture the result of the type conversion function and store it in a new variable.


int

Converts to an integer. Can accept floatingpoint numbers (chops off the decimal part; does not round) or strings that are composed of just digits, like '123' . Special strings 'inf' , 'inf' , and 'NaN' represent special numbers.

num = int('32') ∞ = float('inf')

float

Converts to a floatingpoint number. Works on integers as well as strings that represent floatingpoint numbers. Accepts scientific notation in strings using the letter 'e' to stand for 'times 10 to the...'. 
fl = float(3) fl = float('1.2e3')

str

Converts to a string. Works on literally every kind of Python value, up to and including other functions. 
text = str(123)

repr

Converts to a string, and that string will match what you'd type in Python to get the same value. So for example, str('hello') is just the string 'hello' , but repr('hello') includes the quotation marks you'd need to type in Python to represent that string: "'hello'" . Helpful when you're debugging and want to see what a value is, since printing the number 123 vs. the string '123' will show the same thing, but if you use repr you can tell them apart.

desc = repr('Hello')

list

Converts to a list. Works on any kind of sequence, including existing lists, and always creates a new list, so you can use it to copy a list as well as converting. When used on a string, the individual characters become the entries of the resulting list. If no argument is given, an empty list is created, but you could also just write [] .

vowels = list('aeiou') empty = list()

tuple

Converts to a tuple. Works on any kind of sequence, just like list , and can also create an empty tuple if not given an argument (but you could just write () ).

fixed = tuple([1, 2, 3])

dict

Converts to a dictionary. Requires a sequence of 2element sequences as input, so it's usually not that useful. Can be used without an argument to create an empty dictionary, but you could also just write {} .

points = dict([('a', 3), ('b', 1)])

set

Converts to a set. Accepts any sequence, although each element of that sequence has to be "hashable," i.e., immutable. Can also be used without an argument to create an empty set (and there's no other way to do so). Note that because of the nature of a set, duplicate items from the sequence will be dropped, and ordering is ignored. 
unique = set([1, 2, 3, 2, 4]) letrs = set('letters')

type

Not a type conversion function, but tells you the type of a value. It will return one of the type conversion functions above as a result (actually, they're something called a "class", but we won't discuss the difference in this course). 
type(3) type(len)

Sequence Functions  
Function  Effect  Example(s) 
len

Measures the length of the sequence you give it, returning an integer. For empty sequences, this will be zero. For a list or tuple, this counts the number of entries, for a string, it counts the number of characters, and for a dictionary, it counts the number of key/value pairs. 
length = len('abc')

range

Creates a special range sequence that holds an arithmetic sequence of numbers. The sequence has a start and a stop, and changes by a certain step value in between. For example, the numbers 2, 4, 6, 8 start at 2, stop before 9 (or 10), and step by 2 each time. If only one argument is provided, the start will be 0, the step will be 1, and the argument will be used as the stop (range never includes the stop value itself, but stops just before it gets there). If two arguments are provided, they will be the start and stop and the step will be 1. If three arguments are provided, they will be the start, stop, and step in that order. Note that if you provide a negative step value, you'll have to make sure that the start value is larger than the stop value, or an empty range will result. Note: use this with len to get a range that includes all of the indices of another sequence.

evens = range(2, 10, 2) odds = range(9, 1, 1) indices = range(len([10, 20, 30]))

reversed

Returns a special reversed object that represents the items from another sequence in reverse order. You can use a for loop to directly iterate over this special sequence, or if you want to do more with it, convert it to some other kind of sequence like a list .

countdown = reversed([1, 2, 3])
