# Using the len() function to get the length of a string
len("Python")
6
In this lesson, you will learn about functions, methods, and libraries in Python, building on the basics we covered in the previous lesson.
To get started, open your preferred Python environment (e.g., Jupyter Notebook, VS Code, or PyCharm), and create a new Python file or notebook.
Next, save the file with a name like “functions_and_libraries.py” or “functions_and_libraries.ipynb” depending on your environment.
You should now type all the code from this lesson into that file.
A function is a block of code that performs a specific task. It can take inputs (arguments) and return outputs. Here’s an example of a built-in function with just one argument:
# Using the len() function to get the length of a string
len("Python")
6
The round()
function takes two arguments: the number to round and the number of decimal places to round to.
# Using the round() function to round a number
round(3.1415, 2)
3.14
Use the abs()
function to get the absolute value of -5.
Write your code below and run it to check your answer:
# Your code here
Arguments (also called parameters) are the values that you pass to a function (or method) when you call it.
There are different ways to pass arguments to a function.
Consider again the round()
function.
If we look at the documentation for the round()
function, with :
round?
We see that it takes two arguments:
number
: The number to round.ndigits
: The number of decimal places to round to.There are two main ways to pass arguments to this function.
number
then ndigits
, we can pass the arguments in that order without specifying the argument names, as we did above.round(3.1415, 2)
3.14
If we swap the order of the arguments, we get an error:
round(2, 3.1415)
=
and the argument value.round(number=3.1415, ndigits=2)
3.14
With this method, we can pass the arguments in any order, as long as we use the argument names.
round(ndigits=2, number=3.1415)
3.14
Specifying the keyword is usually recommended, except for simple functions with very few arguments, or when the order of the arguments is obvious from context.
pow()
Use the pow()
function to calculate 2 raised to the power of 7 by passing positional arguments. You may need to consult the documentation for the pow()
function to see how it works.
Write your code below and run it to check your answer:
# Your code here
round()
Use the round()
function to round the number 9.87652
to 3
decimal places by specifying keyword arguments.
Write your code below and run it to check your answer:
# Your code here
Methods are similar to functions, but they are associated with specific objects or data types. They are called using dot notation.
For example, every string object comes with a range of built-in methods, like upper()
to convert to uppercase, lower()
to convert to lowercase, replace()
to replace substrings, and many more.
Let’s see how to use these:
= "python"
name print(name.upper())
print(name.lower())
print(name.replace("p", "🐍"))
PYTHON
python
🐍ython
We can also call the methods directly on the string object, without assigning it to a variable:
# Using the upper() method on a string
print("python".upper())
print("PYTHON".lower())
print("python".replace("p", "🐍"))
PYTHON
python
🐍ython
Similarly, numbers in Python come with some built-in methods. For example, the as_integer_ratio()
(added in Python 3.8) method converts a decimalnumber to a ratio of two integers.
# Using the as_integer_ratio() method on a float
= 1.5
example_decimal example_decimal.as_integer_ratio()
(3, 2)
Come up with simple definitions for the following terms that are clear to YOU (even if not technically exactly accurate):
replace()
method on the string “Helo” to replace the single l with double l.split()
method on the string “Hello World” to split the string into a list of words.# Your code here
Libraries are collections of pre-written code that you can use in your programs. They extend the functionality of Python by providing additional functions and tools.
For example, the math
library provides mathematical functions like sqrt()
for square roots and sin()
for sine.
If we try to use the sqrt()
function without importing the math
library, we get an error:
# This will cause a NameError
16) sqrt(
We can import the math
library and use the sqrt()
function like this:
# Import the library
import math
Then we can use the sqrt()
function like this:
# Use the sqrt() function
16) math.sqrt(
4.0
We can get help on a function in a similar way, calling both the function and the library it’s in:
# Get help on the sqrt() function
math.sqrt?
We can also import libraries with aliases. For example, we can import the math
library with the alias m
:
# Import the entire library with an alias
import math as m
# Then we can use the alias to call the function
16) m.sqrt(
4.0
Finally, if you want to skip the alias/library name, you can either import the functions individually:
# Import specific functions from a library
from math import sqrt, sin
# Then we can use the function directly
16)
sqrt(0) sin(
0.0
Or import everything from the library:
# Import everything from the library
from math import *
# Then we can all functions directly, such as sqrt() and sin()
16)
sqrt(0)
cos(0)
tan(0) sin(
0.0
Phew that’s a lot of ways to import libraries! You’ll mostly see the import ... as ...
syntax, and sometimes the from ... import ...
syntax.
Note that we typically import all required libraries at the top of the file, in a single code chunk. This is a good practice to follow.
Come up with simple definitions for the following terms that are clear to YOU (even if not technically exactly accurate):
random
library and use the randint()
function to generate a random integer between 1 and 10. You can use the ?
operator to get help on the function after importing it.# Your code here
While Python comes with many built-in libraries, there are thousands of additional libraries available that you can install to extend Python’s functionality even further. Let’s look at how to install and use a simple external library, with the cowsay
library as an example.
If we try to import this library without first installing it, we get an error:
import cowsay
To install the library, you can use the !pip install
command in a code cell in Google Colab. For cowsay
, you would run:
!pip install cowsay
Pip installs packages from a remote repository called PyPI. Anyone can create and upload a package to PyPI. After a few checks, it’s then available for anyone to install.
For those working on local Python instances, you can install cowsay
using pip in your terminal:
pip install cowsay
Once installed, we can now import and use the cowsay
library:
import cowsay
# Make the cow say something
'Moo!') cowsay.cow(
____
| Moo! |
====
\
\
^__^
(oo)\_______
(__)\ )\/\
||----w |
|| ||
This should display an ASCII art cow saying “Moo!”.
emoji
library.emoji
library.emojize()
function in the emoji
library.emojize()
function to display an emoji for “thumbs up”.# Your code here
In this lesson, we’ve covered:
These concepts are fundamental to Python programming and will be used extensively as you continue to develop your skills. Practice using different functions, methods, and libraries to become more comfortable with these concepts.