Python includes some built-in functions such as len(), type(), int(), float(), dir(), help(), print(), input(). Here is the complete list of built-in functions. The real power of modern languages is the libraries of existing functionality that allow us to do complex things very quickly. Much of this functionality is bundled with "objects".
As it turns out, the types that we have looked at are somewhat more complex than they first appear – they carry some functionality around with them. We will talk more about this later, but the basic idea is that in most cases, functionality only makes sense for particular kinds of data. In an “object-oriented” language (which Python is), this functionality can be bundled together with the value for which it makes the most sense.
For example, consider a function that takes a string as an argument and returns a copy of the string with all of the letters converted to uppercase. Clearly, this only makes sense for strings. So, rather than having a function that only works properly when we pass it a string, we give the functionality to strings themselves. To access these functions that are built into values, we use “dot-notation”. We write the value (or, more typically, a variable containing the value), followed by a period, and then by the function name (with no spaces). When the function is called, it is as if the value to the left of the dot is passed in as the first argument.
Strings contain a function called
returns a new uppercase version of the string.
Create a string variable: >>> str1 = 'Run for your life!' Call upper >>> str1.upper()
As with our other expressions, the result is printed out on the
console, and then is no longer available. To keep the result, assign
it to another variable:
str2 = str1.upper()
Strings have many functions defined on them. There are a couple of ways to learn what they are.
You can use
dir(s) (where s is a string variable)
in the console to get a list of all available functions
(ignore any that start with __).
You can consult the official documentation.
To learn what a function does and how to use it, you can similarly read the documentation in one of a couple ways.
You can use the builtin
help() function. For
help(str1.upper). Note that we do not
include the () after upper. If we do, the help method will give us
help on the value returned by the function, rather than on the
You can read the description in the online documentation listed above.
Spend a few minutes and try out a couple of string methods.
Some functionality doesn’t necessarily make sense to include on all values of a particular type. Other functionality is specialized and doesn’t need to be included in every Python program. The way we handle this in Python is through the use of “modules”. These are packages containing functions and frequently, new types.
To use these modules, they need to be “imported”. This tells the Python interpreter that you wish to use the module in your code. You can put these import statements at the top of a Python script (and for experimenting we can import them in the console directly).
You will find that working with modules is very much like working
with objects. We can use
dir() to figure out what
functionality comes with the module and
help() to learn
more about it. We access the contents of a module by again using the
dot notation, prefacing items from the module with
module_name.*item_name. Google will also help you find
documentation online for most of the modules that we use (make sure
you are looking at the Python 3.6 documentation and not the 2.7
There are a number of ways to import libraries:
>>> import my_module
>>> import my_long_module_name as m
>>> from my_module import a, b, c
>>> from my_module import *
Our first module will be the
math module, which includes some basic mathematical functions not covered by our basic operators.
Import the module >>> import math Take a look at what is included >>> dir(math) Try a function >>> math.sqrt(9)
Notice that there are some constant values included in the math module like math.pi and math.e, and we do not use parenthese after them. These can be used like variables, though we shouldn’t change them (really – don’t change them).
Here are a couple things to investigate or try; they are just designed to get you looking through the documentation and using these functions.