You are on page 1of 17

Useful Packages

Chris Rowlands
Stuff-I-got-interested-in-this-week group
Useful Libraries

Learning outcomes
• Understand the differences between built-in functions, the standard library,
and external packages
• Be able to import modules and packages
• Be able to work out what functions are useful to complete a programming task
• Be able to use some functions in NumPy, SciPy and Matplotlib
Useful Libraries

Outline
• Built-in functions
• pip
• Importing packages
• Python standard library
– random
• NumPy, SciPy, MatPlotLib
– Working with NumPy arrays
– Demonstration code
Useful Libraries

Built-in functions
• Guaranteed to always be available
• help(), range(), print()
• int(), float(), str(), bool(), chr(), complex()
• list(), tuple(), dict(), set()
• any(), all(), max(), min(), abs(), len(), pow(), round(), sum()
• https://docs.python.org/3/library/functions.html
Useful Libraries

Python Standard Library


• Python is ‘Batteries Included’ – the standard library is included in almost every
distribution.
• Includes several useful packages
– math, cmath, calendar, random, csv, io, html, tkinter, unittest
• https://docs.python.org/3/library/
• Many functions are implemented as methods of objects
– Syntax: object.method()
Useful Libraries

Importing
• Basic syntax: import module [as name]
• Also try: from module import identifier [as name]
– identifier can be a class, type, function, module etc.
• Can also import multiple modules: import random, io, os
Useful Libraries

random
• Package for generating pseudorandom numbers
– Not cryptographically secure!
• Used in scientific simulation (monte carlo methods), games, data sampling…
• Distributions: Uniform, Normal, Exponential, Logarithmic, Pareto etc.
• Integers, floating points, (weighted) selections from a list, shuffled lists
• You can seed the random number generator to always get the same results.
– random.seed(n)
Useful Libraries

Challenge
• I want to build a game to play poker.
My cards will start in a list, but we
need to deal five cards to each
player, and each card should be
unique (no cases where two players
hold the King of Spades for
example). What functions should I
use, and how should I use them?
Useful Libraries

pip
• Recursive acronym for
‘Pip Installs Packages’
• Command-line tool for
installing and uninstalling
Python packages
Useful Libraries

NumPy
• Introduces an efficient, C-like array type called ndarray
• Supports a range of functions common in computationally-intense signal
processing tasks
• https://numpy.org/doc/stable/user/
Useful Libraries

SciPy
• High-level mathematical functions – similar to MATLAB
• Function optimization (scipy.optimize)
• Interpolation (scipy.interpolate)
• Fourier transforms (scipy.fft)
• Signal processing (scipy.signal)
• Linear algebra (scipy.linalg)
• Statistics (scipy.stats)
• https://docs.scipy.org/doc/scipy/reference/
Useful Libraries

Matplotlib
• Easy plotting and visualization library
• Supports a wide variety of 1D, 2D, 3D and nD plots
• https://matplotlib.org/stable/contents.html
Useful Libraries

Some demonstrations: NumPy arrays


• Creating Numpy arrays, slice them, find their shape, perform some useful
operations (find the maximum value, find the sum)
Useful Libraries

NumPy arrays - watch out!


• Numpy arrays support C-like data types
– Defaults to int32
– Will automatically use appropriate dtype when created (possibly a
float, or even an object)
– Default overflow behaviour for numpy.array is to follow C convention
(undefined, but normally wrap)
Useful Libraries

Some demonstrations: Plotting curves


• Curve fitting example
Useful Libraries

Some demonstrations: Images


• Plotting the Fourier Transform
of an image
Useful Libraries

Summary
• Summarized some built-in functions
• Importing packages, installing using pip
• Python standard library
– Random numbers (random)
• NumPy, SciPy, Matplotlib
– Demonstration of some functions

You might also like