Introduction to Python · 2019-11-07 · Introduction to Python University of Oxford Department of...

41
Introduction to Python University of Oxford Department of Particle Physics October 2019 – Part 2 Vipul Davda Particle Physics Systems Administrator Room 661 Telephone: x73389 [email protected] Introduction to Python 1

Transcript of Introduction to Python · 2019-11-07 · Introduction to Python University of Oxford Department of...

Page 1: Introduction to Python · 2019-11-07 · Introduction to Python University of Oxford Department of Particle Physics October 2019 –Part 2 Vipul Davda Particle Physics Systems Administrator

Introduction to PythonUniversity of Oxford

Department of Particle PhysicsOctober 2019 – Part 2

Vipul DavdaParticle Physics Systems Administrator

Room 661Telephone: x73389

[email protected]

Introduction to Python 1

Page 2: Introduction to Python · 2019-11-07 · Introduction to Python University of Oxford Department of Particle Physics October 2019 –Part 2 Vipul Davda Particle Physics Systems Administrator

Write a program to calculate the area and the surface volume of a sphere.

The volume inside a sphere V = 4/3*π*r3

The area of a sphere is A = 4*π*r2

Introduction to Python 2

Exercise 1

Page 3: Introduction to Python · 2019-11-07 · Introduction to Python University of Oxford Department of Particle Physics October 2019 –Part 2 Vipul Davda Particle Physics Systems Administrator

Introduction to Python 3

Exercise 1 solution

import math

pi = math.pi

r = float(input('Radius of sphere: '))

surfaceArea = 4.0 * pi * r **2

surfaceArea = "{0:.2f}".format(round(surfaceArea,2))

print("Surface Area: ", surfaceArea)

volume = (4.0/3.0) * (pi * r ** 3)

volume = "{0:.2f}".format(round(volume,2))

print("Volume is: ", volume)

Page 4: Introduction to Python · 2019-11-07 · Introduction to Python University of Oxford Department of Particle Physics October 2019 –Part 2 Vipul Davda Particle Physics Systems Administrator

Write a script to find numbers between 100 and 300 that are divisible by 7 and multiple of 5.

Introduction to Python 4

Exercise 2

Page 5: Introduction to Python · 2019-11-07 · Introduction to Python University of Oxford Department of Particle Physics October 2019 –Part 2 Vipul Davda Particle Physics Systems Administrator

for n in range(100, 300):

if (n%5==0) and (n%7==0):

print (n)

Introduction to Python 5

Exercise 2 possible solutions

list_n = []

for n in range(100, 300):

if (n%5==0) and (n%7==0):

list_n.append(str(n))

print (','.join(list_n))

Page 6: Introduction to Python · 2019-11-07 · Introduction to Python University of Oxford Department of Particle Physics October 2019 –Part 2 Vipul Davda Particle Physics Systems Administrator

Write a program to print a list after removing the 0th, 4th and 5th

elements.

Numbers = [‘Zero’, ‘One’, ‘Two’, ‘Three’, ‘Four’, ‘Five’]

Hint: Use built-in function enumerate.

for counter, value in enumerate(some_list):

print(counter, value)

Introduction to Python 6

Exercise 3

Page 7: Introduction to Python · 2019-11-07 · Introduction to Python University of Oxford Department of Particle Physics October 2019 –Part 2 Vipul Davda Particle Physics Systems Administrator

Introduction to Python 7

Exercise 3 solution

numbers = ['Zero', 'One', 'Two', 'Three', 'Four', 'Five']

new_numbers = []

for c,v in enumerate(numbers):

if c not in (0,4,5):

new_numbers.append(v)

print(new_numbers)

numbers = ['Zero', 'One', 'Two', 'Three', 'Four', 'Five']

numbers = [v for (c,v) in enumerate(numbers) if c not in (0,4,5)]

print(numbers)

Page 8: Introduction to Python · 2019-11-07 · Introduction to Python University of Oxford Department of Particle Physics October 2019 –Part 2 Vipul Davda Particle Physics Systems Administrator

To open a file for reading or writing use the open() function. open() returns a file object, and commonly used with two arguments.

file_object = open(filename, mode)

Mode can be:

'r' read only

'w' write only

'a' for appending

'r+' open the file for both reading and writing.

Introduction to Python 8

Reading and Writing Files

Page 9: Introduction to Python · 2019-11-07 · Introduction to Python University of Oxford Department of Particle Physics October 2019 –Part 2 Vipul Davda Particle Physics Systems Administrator

with open(‘temperature.txt’, ‘r’) as f:

print (f.read())

with open(‘temperature.txt’, ‘r’) as f:

print (f.read(10))

with open (‘temperature.txt’, ‘r’) as f:

for line in f:

print (line)

with open(‘temperature.txt’) as f:

temp_data = f.readlines()

Introduction to Python 9

How to read a text file

Page 10: Introduction to Python · 2019-11-07 · Introduction to Python University of Oxford Department of Particle Physics October 2019 –Part 2 Vipul Davda Particle Physics Systems Administrator

filename = ‘temperature.txt’

with open(filename, 'r') as f:

for line in f:

if line.strip():

if not line.lstrip().startswith("#"):

line = line.rstrip('\r\n')

(temp, date, time) = line.split(' ')

print (date, time, temp)

f.close()

Introduction to Python 10

How to read a text file

###################################

#Temperature Date Time

###################################

16.4500 2016-09-15 00:01:40.589860

16.4469 2016-09-15 00:06:40.328581

17.6750 2016-09-15 00:11:40.084250

17.1656 2016-09-15 00:16:40.858005

16.3344 2016-09-15 00:21:41.630639

15.9187 2016-09-15 00:26:41.375433

16.7875 2016-09-15 00:31:41.138033

17.6500 2016-09-15 00:36:41.894884

16.8313 2016-09-15 00:41:42.640529

17.3813 2016-09-15 00:46:42.399374

17.3281 2016-09-15 00:51:42.168011

16.3375 2016-09-15 00:56:42.924873

15.7312 2016-09-15 01:01:43.668548

Page 11: Introduction to Python · 2019-11-07 · Introduction to Python University of Oxford Department of Particle Physics October 2019 –Part 2 Vipul Davda Particle Physics Systems Administrator

f = open("newfile.txt", "w")

f.write("hello world")

f.write("and another line")

f.close()

Use ‘with’ command to create a write block

with open("hello.txt", "w") as f:

f.write("Hello World")

f.write("and another line")

f.close

Introduction to Python 11

How to write a text file

Page 12: Introduction to Python · 2019-11-07 · Introduction to Python University of Oxford Department of Particle Physics October 2019 –Part 2 Vipul Davda Particle Physics Systems Administrator

Handling IOErrorfilename= ‘test.txt”

try:

f = open(filename, 'r')

text = f.readlines()

f.close()

print (text)

except IOError:

print ('cannot open', filename)

Introduction to Python 12

Exceptions Handling

Handling ValueErrorwhile True:

try:

x = int(raw_input(“Enter a number: "))

break

except ValueError:

print (“Invalid number. Try

again...“)

https://docs.python.org/2.7/tutorial/errors.html

Python provides Exception to handle any unexpected error in programs.

Page 13: Introduction to Python · 2019-11-07 · Introduction to Python University of Oxford Department of Particle Physics October 2019 –Part 2 Vipul Davda Particle Physics Systems Administrator

Introduction to Python 13

Built-in Functions

https://docs.python.org/2/library/functions.htmlhttps://docs.python.org/3/library/functions.html

abs() divmod() input() open() staticmethod()

all() enumerate() int() ord() str()

any() eval() isinstance() pow() sum()

basestring() execfile() issubclass() print() super()

bin() file() iter() property() tuple()

bool() filter() len() range() type()

bytearray() float() list() raw_input() unichr()

callable() format() locals() reduce() unicode()

chr() frozenset() long() reload() vars()

classmethod() getattr() map() repr() xrange()

cmp() globals() max() reversed() zip()

compile() hasattr() memoryview() round() __import__()

complex() hash() min() set()

delattr() help() next() setattr()

dict() hex() object() slice()

dir() id() oct() sorted()

Page 14: Introduction to Python · 2019-11-07 · Introduction to Python University of Oxford Department of Particle Physics October 2019 –Part 2 Vipul Davda Particle Physics Systems Administrator

A function in Python is indented using the keyword def, followed by a function name, a signature within brackets (), and a colon :

def func():

print(“Hello“)

func()

Hello

Introduction to Python 14

User defined function

Page 15: Introduction to Python · 2019-11-07 · Introduction to Python University of Oxford Department of Particle Physics October 2019 –Part 2 Vipul Davda Particle Physics Systems Administrator

def name_age( name, age = 21 ):

print ("Name: ", name)

print ("Age ", age)

return

name_age( age=70, name=“Vip" )

name_age( name=“Vip" )

def sum( arg1, arg2 ):

# add two numbers

total = arg1 + arg2

# return total

return total

mytotal = sum( 10, 20 )

print (“Total is: ", mytotal)

Introduction to Python 15

User defined function

Page 16: Introduction to Python · 2019-11-07 · Introduction to Python University of Oxford Department of Particle Physics October 2019 –Part 2 Vipul Davda Particle Physics Systems Administrator

# This is a global variable.

total = 0

def sum( arg1, arg2 ):

total = arg1 + arg2

# local total variable.

print ("Inside the function local total : ", total)

return total

sum( 10, 20 )

print ("Outside the function global total : ", total)

Introduction to Python 16

Scope

Page 17: Introduction to Python · 2019-11-07 · Introduction to Python University of Oxford Department of Particle Physics October 2019 –Part 2 Vipul Davda Particle Physics Systems Administrator

Write a function called pascal_triangle that prints out the first n rows of Pascal's triangle.

Hint: Use built-in function zip.x = [1, 2, 3]

y = [4, 5, 6]

zipped = zip(x, y)

print (zipped)

[(1, 4), (2, 5), (3, 6)]

Introduction to Python 17

Exercise 4

Page 18: Introduction to Python · 2019-11-07 · Introduction to Python University of Oxford Department of Particle Physics October 2019 –Part 2 Vipul Davda Particle Physics Systems Administrator

Introduction to Python 18

Exercise 4 solution

def pascal_triangle(n):

row = [1]

y = [0]

for x in reversed(range(n)):

print(row)

row=[l+r for l,r in zip(row+y, y+row)]

return n>=1

pascal_triangle(6)

Page 19: Introduction to Python · 2019-11-07 · Introduction to Python University of Oxford Department of Particle Physics October 2019 –Part 2 Vipul Davda Particle Physics Systems Administrator

Introduction to Python 19

Exercise 4 solution

def pascal_triangle(n):

row = [1]

y = [0]

for x in range(max(n,0)):

print(row)

row=[l+r for l,r in zip(row+y, y+row)]

return n>=1

pascal_triangle(6)

Page 20: Introduction to Python · 2019-11-07 · Introduction to Python University of Oxford Department of Particle Physics October 2019 –Part 2 Vipul Davda Particle Physics Systems Administrator

Introduction to Python 20

Exercise 4 solution

def pascal_triangle(n):

row = [1]

y = [0]

for x in range(n,0,-1):

print(row)

row=[l+r for l,r in zip(row+y, y+row)]

return n>=1

pascal_triangle(6)

Page 21: Introduction to Python · 2019-11-07 · Introduction to Python University of Oxford Department of Particle Physics October 2019 –Part 2 Vipul Davda Particle Physics Systems Administrator

Modules are imported with the “import” statement:import math

print(math.sin(math.pi))

The module has its own namespace:

A collection of objects: variables, functions, classes.

This allows a clean separation of code with the same name

import math

import scipy as sp

print(math.sin(math.pi))

print(sp.sin([math.pi, 2*math.pi]))

https://docs.python.org/2/library/

https://docs.python.org/3/library/

Introduction to Python 21

Python Standard Library

Page 22: Introduction to Python · 2019-11-07 · Introduction to Python University of Oxford Department of Particle Physics October 2019 –Part 2 Vipul Davda Particle Physics Systems Administrator

Python has a feature called virtual environments. This allows you to build on system versions of python to do the following:

Add additional modules that are not already installed on the system

Run newer versions of modules than the system provides

Run a newer version of python than the system provides

Introduction to Python 22

Virtual Environment Using Python 3

Page 23: Introduction to Python · 2019-11-07 · Introduction to Python University of Oxford Department of Particle Physics October 2019 –Part 2 Vipul Davda Particle Physics Systems Administrator

python3 –m venv py3venv

# To activate your virtual environment

. py3venv/bin/activate

which python

/home/davda/demo/15_virtual_enviroments/py3venv/bin/python

(py3venv) davda@pplxdt50:$ python --versionPython 3.6.8

# To leave the virtual environment

deactivate

Introduction to Python 23

Virtual Environment Using Python 3

https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/

Page 24: Introduction to Python · 2019-11-07 · Introduction to Python University of Oxford Department of Particle Physics October 2019 –Part 2 Vipul Davda Particle Physics Systems Administrator

To create a virtualenv called myVirtualEnv to use Python 2.7, do the following:

cd $HOME

module load python/2.7

virtualenv --system-site-packages \

-p `which python` myVirtualenv

Every time you wish to use the virtualenv

in the future, run just the following:cd $HOME

module load python/2.7

$HOME/myVirtualenv/bin/activate

pip search numpy

pip install numpy

pip install matplotlib

Introduction to Python 24

Creating a Python2 Virtual Environment on Legacy PP Interactive Servers

Page 25: Introduction to Python · 2019-11-07 · Introduction to Python University of Oxford Department of Particle Physics October 2019 –Part 2 Vipul Davda Particle Physics Systems Administrator

In [1]: import numpy as np

In [2]: import matplotlib.pyplot as plt

In [3]: a=np.array([1,2,3,4,5,6,7,8])

In [4]: line=plt.plot(a,a)

In [5]: plt.show()

Introduction to Python 25

Creating a Python2 Virtual Environment on Legacy PP Interactive Servers

Page 26: Introduction to Python · 2019-11-07 · Introduction to Python University of Oxford Department of Particle Physics October 2019 –Part 2 Vipul Davda Particle Physics Systems Administrator

pyROOT is a Python extension module that allows the user to interact with any ROOT class from the Python interpreter.

To use pyROOT on Particle Physics Interactive Servers To use pyROOT, the C++ libraries must be on your PYTHONPATH.

https://www2.physics.ox.ac.uk/content/python-tutorial

Introduction to Python 26

pyROOT

Page 27: Introduction to Python · 2019-11-07 · Introduction to Python University of Oxford Department of Particle Physics October 2019 –Part 2 Vipul Davda Particle Physics Systems Administrator

import ROOT

import random

ROOT.gROOT.Reset()

hist=ROOT.TH1F("theName","theTitle; \

xlabel;ylabel", \

100,0,100)

for x in range(100):

val = int(random.randint(45,55))

hist.Fill(val)

hist.Draw()

Introduction to Python 27

pyROOT

Page 28: Introduction to Python · 2019-11-07 · Introduction to Python University of Oxford Department of Particle Physics October 2019 –Part 2 Vipul Davda Particle Physics Systems Administrator

The numpy module is used in almost all numerical computation using Python.

It is a package that provide high-performance vector, matrix and higher-dimensional data structures.

Much of the scientific stack for Python is built on top of numpy.

Numpy provides highly optimised implementations of the fundamental datatypes for linear algebra: vectors, matrices, and their higher-dimensional analogues.

http://www.numpy.org/

Introduction to Python 28

numpy

Page 29: Introduction to Python · 2019-11-07 · Introduction to Python University of Oxford Department of Particle Physics October 2019 –Part 2 Vipul Davda Particle Physics Systems Administrator

Introduction to Python 29

Simple numpy example

Page 30: Introduction to Python · 2019-11-07 · Introduction to Python University of Oxford Department of Particle Physics October 2019 –Part 2 Vipul Davda Particle Physics Systems Administrator

Write a script to create a 5 by 5 2d array with 1 on the border and 0 inside.

Hint: Use numpy.ones function.

Introduction to Python 30

Exercise 5

Page 31: Introduction to Python · 2019-11-07 · Introduction to Python University of Oxford Department of Particle Physics October 2019 –Part 2 Vipul Davda Particle Physics Systems Administrator

Introduction to Python 31

Exercise 5 solution

import numpy as np

x = np.ones((5,5))

print(x)

x[1:-1,1:-1] = 0

print(x)

Page 32: Introduction to Python · 2019-11-07 · Introduction to Python University of Oxford Department of Particle Physics October 2019 –Part 2 Vipul Davda Particle Physics Systems Administrator

The scipy package contains various submodules to solve common issues in scientific computing.

scipy builds upon numpy

http://www.scipy.org/

Introduction to Python 32

scipy

Page 33: Introduction to Python · 2019-11-07 · Introduction to Python University of Oxford Department of Particle Physics October 2019 –Part 2 Vipul Davda Particle Physics Systems Administrator

scipy.clusterscipy.fftpackscipy.integratescipy.interpolatescipy.ioscipy.linalgscipy.maxentropyscipy.ndimagescipy.odrscipy.optimizescipy.signalscipy.sparsescipy.spatialscipy.algorithmsscipy.specialscipy.stats

Vector quantization / KmeansFourier transformintegrateinterpolateData input and outputLinear algebra routinesRoutines for fitting maximum entropy modelsn-dimensional image packageOrthogonal distance regressionOptimizationSignal processingSparse matricesSpatial data structures and algorithmsAny special mathematical functionsStatistics

Introduction to Python 33

scipy

Each submodule is used to solve different applications:

Page 34: Introduction to Python · 2019-11-07 · Introduction to Python University of Oxford Department of Particle Physics October 2019 –Part 2 Vipul Davda Particle Physics Systems Administrator

import numpy as np

import matplotlib.pyplot as plt

from scipy import stats

a = np.random.normal(size=1000)

bins = np.arange(-10, 10)

# returns value of hist and bin edges

hist, bin_edges = np.histogram(a, bins=bins)

# use scipy normal probability density function

b = stats.norm.pdf(bin_edges)*1000

plt.bar(bin_edges[:-1], hist)

plt.plot(bin_edges, b, 'r')

plt.show()

Introduction to Python 34

scipy example

Page 35: Introduction to Python · 2019-11-07 · Introduction to Python University of Oxford Department of Particle Physics October 2019 –Part 2 Vipul Davda Particle Physics Systems Administrator

Matplotlib is a 2D and 3D graphics library for generating scientific figures.

Some of the many advantages of this module include:

Easy to get started Support for LATEX formatted labels and texts Great control of every element in a figure High-quality output in many formats GUI for interactively exploring figures and support for headless

generation of figures

http://matplotlib.org/

Introduction to Python 35

matplotlib

Page 36: Introduction to Python · 2019-11-07 · Introduction to Python University of Oxford Department of Particle Physics October 2019 –Part 2 Vipul Davda Particle Physics Systems Administrator

def main():

# calc the set

m_set = mandelbrot(10, 2., 101, 101)

# plot

plt.imshow(m_set.T, extent=[-2, 1, -1.5, 1.5])

plt.show()

if __name__ == "__main__":

main()

Introduction to Python 36

Matplotlib – Mandelbrot example

#!/usr/bin/python3

import numpy as np

import matplotlib.pyplot as plt

np.seterr(all=‘ignore’)

def mandelbrot(n_max, threshold, nx, ny):

# A grid of c-values

x = np.linspace(-2, 1, nx)

y = np.linspace(-1.5, 1.5, ny)

c = x[:,np.newaxis] + 1j*y[np.newaxis,:]

# Mandelbrot iteration

z = c

for j in range(n_max):

val = np.isnan(z[0])

if not val[0]:

try:

z = z**2 + c

except Exception:

pass

m_set = (np.abs(z) < threshold)

return m_set

Page 37: Introduction to Python · 2019-11-07 · Introduction to Python University of Oxford Department of Particle Physics October 2019 –Part 2 Vipul Davda Particle Physics Systems Administrator

Python provides Multiprocessing module for concurrency:

Supports spawning process.

Offer local and remote concurrency

The Pool class represents a pool of worker processes. It has methods which allows tasks to be offloaded to the worker processes.

Introduction to Python 37

Multiprocessing

https://docs.python.org/3/library/multiprocessing.html

Page 38: Introduction to Python · 2019-11-07 · Introduction to Python University of Oxford Department of Particle Physics October 2019 –Part 2 Vipul Davda Particle Physics Systems Administrator

from os import getpid

from multiprocessing import Pool

def square(x):

pid = getpid()

sqr = x*x

return x, sqr, pid

# create pool with 6 workers

myPool = Pool(processes=6)

# my data to process

myData = range(20)

# assign work to processes in our pool

myOutput = myPool.map(square, myData)

# print our output

print (myOutput)

Introduction to Python 38

Multiprocessing Example

Page 39: Introduction to Python · 2019-11-07 · Introduction to Python University of Oxford Department of Particle Physics October 2019 –Part 2 Vipul Davda Particle Physics Systems Administrator

PEP 8 has emerged as the style guide that most projects adhere to; the most important points are:

Use 4-space indentation, and no tabs. (# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4)

Wrap lines, don’t exceed 79 characters. Use blank lines to separate functions and classes, and in larger blocks of code inside functions. When possible, put comments on a line of their own.

Comments that contradict the code are worse than no comments. Use spaces around operators and after commas, but not directly inside bracketing

constructs: a = f(1, 2) + g(3,4) Name your classes and functions consistently. Use meaningful variable names Use meaningful constant names – at the start of the program Don’t use fancy encodings. Use Python’s default UTF-8, or even plain ASCII work best. Imports should be on separate lines

import numpy

import scipy

https://www.python.org/dev/peps/pep-0008/

Introduction to Python 39

Programming style

Page 40: Introduction to Python · 2019-11-07 · Introduction to Python University of Oxford Department of Particle Physics October 2019 –Part 2 Vipul Davda Particle Physics Systems Administrator

Introduction to Python 40

Molly - Python 3 Essential Training

https://www.linkedin.com/learning/python-essential-training-2

Page 41: Introduction to Python · 2019-11-07 · Introduction to Python University of Oxford Department of Particle Physics October 2019 –Part 2 Vipul Davda Particle Physics Systems Administrator

Introduction to Python 41

Useful Links

Python Tutorial https://docs.python.org/3/tutorial/

Python Code Stylehttp://docs.python-guide.org/en/latest/writing/style/https://www.python.org/dev/peps/pep-0008/

The Python Language Reference https://docs.python.org/2/reference/

The Python Standard Library http://docs.python.org/2/library/

Numpy http://www.numpy.org/

Scipy

http://www.scipy.org/http://docs.scipy.org/doc/scipy/reference/tutorial/http://scipy-cookbook.readthedocs.io/

pyROOT

https://root.cern.ch/howtos/http://wlav.web.cern.ch/wlav/pyroot/http://nbviewer.jupyter.org/github/mazurov/webfest2013/blob/master/notebooks/MasterClassD0-ex1.ipynbhttp://nbviewer.jupyter.org/github/mazurov/webfest2013/blob/master/notebooks/MasterClassD0-ex2%2Cex3.ipynb

Matplotlibhttp://matplotlib.org/http://matplotlib.org/users/pyplot_tutorial.html

Molly - Python 3 Essential Training https://www.linkedin.com/learning/python-essential-training-2