import math
print(math.pi)
3.141592653589793
We have seen how to use several built-in functions in Python such as print
and input
. If we think that these functions have been written by somebody else and we don’t need to worry about their implementation, it is easy to imagine how functions simplify our programming making our code smaller, more readable and with less errors.
However, Python’s built-in functionality can be greatly extended through the use of modules. Python modules can be thought of as code libraries, they are files containing code that define functions, classes, and variables and provide additional functionalities. Many modules come pre-installed with Python, such as os
, random
and datetime
, but other third-party modules can be installed using package managers like pip
. In the next session we will explore how you can create modules yourself to organize and reuse your code.
To use a module in your Python script, you need to import it with import
. For example:
import math
print(math.pi)
3.141592653589793
Note above that to call objects and functions that are defined in a module we will use the module name (math
), followed by a dot (.
), and then the object or function name(pi
).
Below you will learn how to use a module to read data files in CSV format.
We have previously explored how to obtain user input via the terminal, but for data analysis you will mostly need extracting data from files. Python offers various libraries to facilitate this process, each tailored to specific file formats. We can use the csv
module to read data from Comma-Separated Values (CSV) files.
The csv
module is a library designed specifically for working with CSV files. It provides functionality to both read from and write to CSV files efficiently. The script below shows it’s basic syntax:
read_csv.py
import csv
with open('../assets/sample.csv', 'r') as file:
# create a csv.reader object
= csv.reader(file)
csv_reader for row in csv_reader:
print(row[2])
-0.4220535
1.472827
0.5725429
-0.2157133
1.449306
0.1880375
-1.570342
0.8315003
In this example we used the function open()
to open a file in read mode (this is a Python built-in function), and created a csv.reader
object to iterate through the rows of the file. Note that there is also a new statement with, what we will not cover in this session.