A Guide to Understanding Functions in Python
Beginner
Python Reference
This Python reference offers programmers a quick way to learn Python
and also serves as a source for reminders.
While the version documented here is Python 3.5.3, most of this is
suitable for other versions of Python 3, however check your version for
details.
1. The Challenge
There are 3 types of functions in Python, built-in functions, those
imported from other modules and user-defined functions.
The official Python.org website lists
68 built-in functions in one of the first reference
documents in The Python Standard Library.
The challenge, for beginners especially, is understanding which
functions to start with, so here we show you that list categorized and
prioritized.
2. Python Built-In Functions
The table below lists 68 Python Built-in functions supplemented with
Priority, Category and
Description so you can develop a plan for what to
memorize first and leave the rest to look up later.
Python Built-in Functions in Version 3.5.3
Function |
Priority |
Category |
Description |
abs() |
High |
Math |
Absolute value of integer, float or complex number |
all() |
Mid |
Logic |
Returns True when all values are true. |
any() |
Mid |
Logic |
Returns True when any value is true. |
ascii() |
Mid |
Strings |
Return printable characters of an object with escaping. |
bin() |
Mid |
Binary |
Return a binary string for an integer. |
bool() |
Low |
Boolean |
Returns False if omitted or
False values, otherwise True. |
bytearray() |
Low |
Sequences |
Return an array of bytes. |
bytes() |
Low |
Bytes |
Returns a new bytes type of object. |
callable() |
Mid |
Logic |
Returns True if object is callable, otherwise
False. |
chr() |
High |
Characters |
Returns the character equivalent of the Unicode integer. |
classmethod() |
Low |
Functions |
Returns a class method for the supplied function. |
compile() |
Low |
Compiling |
Compiles source into code. |
complex() |
Low |
Numbers |
Returns a complex number from string or number. |
delattr() |
Low |
Objects |
Deletes an attribute from an object. |
dict() |
Mid |
Dictionary |
Create a new dictionary object. |
dir() |
Mid |
Directory |
Returns a list of names in the local scope, or attributes of the
given object. |
divmod() |
Mid |
Math |
Returns the quotient and remainder for integers and floats. |
enumerate() |
Low |
Objects |
Returns an enumerate object. |
eval() |
Low |
Code |
Returns the result of an evaluated statement. |
exec() |
Low |
Code |
For dynamic execution of code. |
filter() |
Low |
Functions |
Construct an interator of values that return True |
float() |
High |
Numbers |
Returns a floating point number. |
format() |
Mid |
Formatting |
Returns input as formatted by a custom specification. |
frozenset() |
Mid |
Containers |
Returns a new frozenset object type. |
getattr() |
Mid |
Attributes |
Returns the value of the named attribute. |
globals() |
Mid |
System |
Returns a dictionary of names, methods and classes globally. |
hasattr() |
Mid |
Attributes |
Returns True if the object has a specified attribute. |
hash() |
Low |
System |
Returns hash integer keys for an object. |
help() |
High |
Help |
Invokes the built-in help system. |
hex() |
Mid |
Numbers |
Converts an integer to a lowercase hexadecimal string. |
id() |
Mid |
Objects |
Returns the unique id of an object. |
input() |
High |
Interactivity |
Reads a line from input as a string. |
int()
|
High |
Numbers |
Returns an integer object constructed from a number or string. |
isinstance() |
Mid |
Classes |
Returns True if argument is an instance of a class. |
issubclass() |
Mid |
Classes |
Returns True if argument is an instance of a subclass |
iter() |
Mid |
Loops |
To iterate over an object like a set or tuple. |
len() |
High |
Objects |
Returns the length or number of items in an object. |
list() |
High |
Lists |
Create a mutable list object. |
locals() |
Mid |
System |
Returns a dictionary of names, methods and classes locally. |
map() |
Low |
Iterables |
Applies a function to each item of an iterable. |
max() |
High |
Sorting |
Returns the largest item from among iterables or arguments. |
memoryview() |
Low |
Objects |
Returns a memory view object from an argument. |
min() |
High |
Sorting |
Returns the smallest item from among iterables of arguments. |
next() |
Mid |
Iterators |
Returns the next item from an iterator. |
object() |
Mid |
Objects |
Returns a featureless object. |
oct() |
Mid |
Numbers |
Converts an integer into an octal string. |
open() |
High |
Files |
Open a file and create a file object. |
ord() |
Mid |
Strings |
Returns a Unicode integer for a string. |
pow() |
High |
Math |
Returns the exponent. |
print() |
High |
Printing |
Prints objects as text strings to the screen or standard output. |
property() |
Mid |
Classes |
Returns the property attribute for a class. |
range() |
High |
Sequences |
To create an immutable sequence type. |
repr() |
Mid |
Strings |
Returns object as a printable string |
reversed() |
Mid |
Sequences |
Returns a reverse interator. |
round() |
High |
Numbers |
Returns a number rounded to specified precision. |
set() |
Mid |
Sets |
Create a set object type. |
setattr() |
Mid |
Attributes |
Set's an attribute for a object. |
slice() |
Mid |
Slice |
Returns a slice object from another object. |
sorted() |
Mid |
Lists |
Return a sorted list object from an iterable object. |
staticmethod() |
Low |
Classes |
Returns a static method for a function. |
str() |
High |
Strings |
Returns the string version of an object. |
sum() |
High |
Math |
Returns the sum of an iterable object. |
super() |
Mid |
Classes |
To inherit methods from parent classes. |
tuple() |
High |
Sequences |
Create an immutable sequence type. |
type() |
High |
Objects |
Returns the type of object. |
vars() |
Mid |
Objects |
Returns the __dict__ attribute for objects that have them. |
zip() |
Low |
Iterators |
Creates an iterator of tuples. |
__import__() |
Mid |
Modules |
An internal Python function invoked during imports. |
The Priority column was determined subjectively based
on experience. Some have tried to scrape public Python code to rank
functions and methods with limited success.
3. Find Built-in Functions For Your Version of Python
To find the built-in functions for your version, the easiest path is
to go straight to the Python.org
documentation page listing all versions.
After clicking the link to your version number, click
Library Reference and then
Built-in Functions.
4. Calling Functions in Python
For beginners, let's solidify how functions are called and introduce
their syntax.
Let's create a text object, giving it a label
x
and use the most common function in
Python called print()
to print it to
the screen.
>>> x = "I'm a string"
>>> print(x)
I'm a string
We put the label (often called a variable) inside the function's
parentheses thereby telling Python what to print. Here we are
calling the function and sending it an argument.
What we didn't note here is that the
print()
function can take optional
arguments, or multiple arguments, as you can see here.
>>> print(x, x)
I'm a string I'm a string # note the space between the two strings
>>> print()
# no errors, a blank line printed
In the first case we gave the function two arguments and in the second
we gave it none.
Now you wouldn't know what arguments to send to a function without
knowing the syntax for each function. So next we'll see how to look
that up.
5. Accessing Python Function Syntax on your Local Machine
You can find the syntax for functions at the Python.org website as
mentioned earlier, or you may find it less distracting and faster to
use local help right in the Python Interpreter.
Entering help('print')
at the Python
Interpreter will show syntax for the print function, like this.
Help on built-in function print in module builtins:
print(...)
print(value, ..., sep=' ', end='\n, file=sys.stdout, flush=False)
Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.
(END)
We won't cover all of it, but in a nutshell the sytax shows that
multiple arguments can be entered and the optional keyword arguments
sep=' '
explains why there was a space
between the strings earlier. The
end='\n'
demonstrates why there was
a blank line when we didn't enter any arguments. These were the
defaults, and they could be overridden by specifying these arguments.
This is one short example that demonstrates function syntax and shows
how you can save time by looking for function syntax right at the
Python Interpreter.
Related Python Content