Professional Documents
Culture Documents
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
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
Summary
• Summarized some built-in functions
• Importing packages, installing using pip
• Python standard library
– Random numbers (random)
• NumPy, SciPy, Matplotlib
– Demonstration of some functions