This is a very basic introduction to Python. It is not exhaustive, but is meant to give you a starting point.

This notebook was written for PHY 403 by Segev BenZvi, University of Rochester, (Spring 2016), and updated by Aran Garcia-Bellido (Spring 2017).

It is based on a similar (longer) Python guide written by Kyle Jero (UW-Madison) for the IceCube Programming Bootcamp in June 2015, and includes elements from older guides by Jakob van Santen and Nathan Whitehorn.

Python is an **imperative**, **interpreted** programming language with **strong** **dynamic** typing.

**Imperative**: programs are built around one or more subroutines known as "functions" and "classes"**Interpreted**: program instructions are executed on the fly rather than being pre-compiled into machine code**Dynamic Typing**: data types of*variables*(`int`

,`float`

,`string`

, etc.) are determined on the fly as the program runs**Strong Typing**: converting a variable from one type to another (e.g.,`int`

to`string`

) is not always done automatically

Python offers fast and flexible development and can be used to glue together many different analysis packages which have "Python bindings."

As a rule, Python programs are slower than compiled programs written in Fortran, C, and C++. But it's a much more forgiving programming language.

Python is one of the most popular scripting languages in the world, with a huge community of users and support on all major platforms (Windows, OS X, Linux).

Pretty much every time I've run into a problem programming in Python, I've found a solution after a couple of minutes of searching on google or stackoverflow.com!

- NumPy: random number generation, transcendental functions, vectorized math, linear algebra.
- SciPy: statistical tests, special functions, numerical integration, curve fitting and minimization.
- Matplotlib: plotting: xy plots, error bars, contour plots, histograms, etc.
- IPython: an interactive python shell, which can be used to run Mathematica-style analysis notebooks.

- SciKits: data analysis add-ons to SciPy, including machine learning algorithms.
- Pandas: functions and classes for specialized data analysis.
- AstroPy: statistical methods useful for time series analysis and data reduction in astronomy.
- Emcee: great implementation of Markov Chain Monte Carlo; nice to combine with the package Corner.

Many C and C++ packages used in high energy physics come with bindings to Python. For example, the ROOT package distributed by CERN can be run completely from Python. If you are building ROOT in your computer from scratch, make sure you enable the python binding when you build ROOT.

If you don't want to install all these packages on your own computer, you can create a free account at cloud.sagemath.com. Sagemath gives you access to ipython notebooks running on remote servers. Recent versions of SciPy, NumPy, and Matplotlib are provided.

Similarly, try.jupyter.org allows to check simple code without opening an account (but does not allow to save files, for that you need to download Anaconda/Jupyter into your computer). Jupyter can also be run from github.com which is very useful as a code repository.

We will go through the following topics, and then do some simple exercises.

- Arithmetic Operators
- Variables and Lists
- Conditional Statements
- Loops (
`for`

and`while`

) - Functions
- Importing Modules

1

2

3

In [1]:

1+23

4

24

5

In [3]:

19993 - 7743

6

12250

7

In [4]:

3*8

8

24

9

In [5]:

50 / 2

10

25

In [6]:

1 / 3

11

0

Note: in Python 2, division of two integers is always **floor division**. In Python 3, 1/2 automatically evaluates to the *floating point number* 0.5. To use floor division in Python 3, you'll have to run `1 // 2`

.

12

In [7]:

1.0000000 / 2

13

0.5

In [8]:

float(1) / 2

14

0.5

15

In [9]:

30 % 4

16

2

In [10]:

3.14159265359 % 1.

17

0.14159265359000006

18

In [11]:

4**2

19

16

In [12]:

pow(4,2)

20

16

Variables are extremely useful for storing values and using them later. One can declare a variable to contain the output of any variable, function call, etc. However, variable names must follow certain rules:

- Variable names must start with a letter (upper or lower case) or underscore
- Variable names may contain only letters, numbers, and underscores _
- The following names are
**reserved keywords**in Python and cannot be used as variable names:

`and del from not while`

`as elif global or with`

`assert else if pass yield`

`break except import print`

`class exec in raise`

`continue finally is return`

`def for lambda try`

21

In [13]:

x = 5 + 6

22

This time nothing printed out because the output of the expression was stored in the variable `x`

. To see the value we have to call the `print`

function:

23

In [14]:

print(x)

24

11

Alternatively, just call `x`

and the notebook will evaluate it and dump the value to the output:

25

In [15]:

x

26

11

Recall that we don't have to explicitly declare what type something is in python, something that is not true in many other languages, we simply have to name our variable and specify what we want it to store. However, it is still nice to know the types of things sometimes and learn what types python has available for our use.

27

In [16]:

print(type(x))

28

<type 'int'>

In [17]:

y = 2 print(type(x/y))

29

<type 'int'>

In [18]:

z = 1. print(type(z/y))

30

<type 'float'>

In [19]:

h = "Hello" print(type(h))

31

<type 'str'>

In [20]:

s = " " w = "World!" print(h + s + w)

32

Hello World!

In [21]:

apostrophes="They're " quotes='"hypothetically" ' saying=apostrophes + quotes + "good for you to know." print(saying)

33

They're "hypothetically" good for you to know.

C-style formatted printing is also allowed:

34

In [22]:

p = "Pi" print("%s = %.3f" % (p, 3.14159265359))

35

Pi = 3.142

Imagine that we are storing the heights of people or the results of a random process. We could imagine taking and making a new variable for each piece of information but this becomes convoluted very quickly. In instances like this it is best to store the collection of information together in one place. In python this collection is called a list and can be defined by enclosing data separated by commas in square brackets. A empty list can also be specified by square brackets with nothing between them and filled later in the program.

36

In [23]:

blanklist=[] blanklist

37

[]

In [3]:

alist=[1, 2, 3] print(alist) print(type(alist)) blist=10*[1.5] print(blist)

38

[1, 2, 3]
<type 'list'>
[1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5]

Notice that the type of our list is list and no mention of the data type it contains is made. This is because python does not fuss about what type of thing is in a list or even mixing of types in lists. If you have worked with nearly any other language this is different then you are used to since the type of your list must be homogeneous.

39

In [25]:

blist=[1, "two", 3.0] blist

40

[1, 'two', 3.0]

In [26]:

print(type(blist))

41

<type 'list'>

You can check the current length of a list by calling the `len`

function with the list as the argument:

42

In [27]:

len(blist)

43

3

In addition, you can add objects to the list or remove them from the list in several ways:

44

In [28]:

blist.append("4") blist

45

[1, 'two', 3.0, '4']

In [29]:

blist.insert(0, "0") blist

46

['0', 1, 'two', 3.0, '4']

In [30]:

blist.extend([5,6]) print(blist) print(len(blist))

47

['0', 1, 'two', 3.0, '4', 5, 6]
7

In [31]:

blist.append(7) blist

48

['0', 1, 'two', 3.0, '4', 5, 6, 7]

In [32]:

blist = blist*2 blist

49

['0', 1, 'two', 3.0, '4', 5, 6, 7, '0', 1, 'two', 3.0, '4', 5, 6, 7]

In [33]:

blist.remove("4") blist

50

['0', 1, 'two', 3.0, 5, 6, 7, '0', 1, 'two', 3.0, '4', 5, 6, 7]

In [34]:

blist.remove('4') blist.remove(3.0) blist

51

['0', 1, 'two', 5, 6, 7, '0', 1, 'two', 3.0, 5, 6, 7]

Individual elements (or ranges of elements) in the list can be accessed using the square bracket operators . For example:

52

In [35]:

print(blist[0]) print(blist[4])

53

0
6

In [36]:

print(blist[-1]) print(blist[-2]) print(blist[-3])

54

7
6
5

In [37]:

blist[0:4]

55

['0', 1, 'two', 5]

In [38]:

print(blist) # list slicing example: blist[0:6:2] # sytax: start, stop, stride

56

['0', 1, 'two', 5, 6, 7, '0', 1, 'two', 3.0, 5, 6, 7]

['0', 'two', 6]

This is an example of a slice, where we grab a subset of the list and also decide to step through the list by skipping every other element. The syntax is

`listname[start:stop:stride]`

Note that if start and stop are left blank, the full list is used in the slice by default.

57

In [39]:

blist[::2]

58

['0', 'two', 6, '0', 'two', 5, 7]

In [40]:

print(blist[::-1]) # An easy way to reverse the order of elements print(blist)

59

[7, 6, 5, 3.0, 'two', 1, '0', 7, 6, 5, 'two', 1, '0']
['0', 1, 'two', 5, 6, 7, '0', 1, 'two', 3.0, 5, 6, 7]

A simple built-in function that is used a lot is the range function. It is not a list but returns one so we will discuss it here briefly. The syntax of the function is range(starting number, ending number, step size ). All three function arguments are required to be integers with the ending number not being included in the list. Additionally the step size does not have to be specified, and if it is not the value is assumed to be 1.

60

In [41]:

range(0,10)

61

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [42]:

range(0,10,2)

62

[0, 2, 4, 6, 8]

Conditionals are useful for **altering the flow of control** in your programs. For example, you can execute blocks of code (or skip them entirely) if certain conditions are met.

Conditions are created using `if/elif/else`

blocks.

For those of you familiar with C, C++, Java, and similar languages, you are probably used to code blocks being marked off with curly braces: { }

In Python braces are not used. Code blocks are *indented*, and the Python interpreter decides what's in a block depending on the indentation. Good practice (for readability) is to use 4 spaces per indentation. The IPython notebook will automatically handle the indentation for you.

63

In [43]:

x = 55 if x > 10: print("x > 10") elif x > 5: print("x > 5") else: print("x <= 5")

64

x > 10

In [44]:

isEven = (x % 2 == 0) # Store a boolean value if not isEven: print("x is odd") else: print("x is even")

65

x is odd

There are several predefined operators used to make boolean comparisons in Python. They are similar to operators used in C, C++, and Java:

`==`

... test for equality

`!=`

... test for not equal

`>`

... greater than

`>=`

... greater than or equal to

`<`

... less than

`<=`

... less than or equal to

Following the usual rules of boolean algebra, boolean values can be negated or combined in several ways:

You can combine two boolean variables using the operator `&&`

or the keyword `and`

:

66

In [45]:

print("x y | x && y") print("---------------") for x in [True, False]: for y in [True, False]: print("%d %d | %d" % (x, y, x and y))

67

x y | x && y
---------------
1 1 | 1
1 0 | 0
0 1 | 0
0 0 | 0

In [46]:

x = 10 if x > 2 and x < 20: print(x)

68

10

In [47]:

if x < 2 and x > 20: print(x)

69

You can also combine two boolean variables using the operator `||`

or the keyword `or`

:

70

In [48]:

print("x y | x || y") print("---------------") for x in [True, False]: for y in [True, False]: print("%d %d | %d" % (x, y, x or y))

71

x y | x || y
---------------
1 1 | 1
1 0 | 1
0 1 | 1
0 0 | 0

In [49]:

x = 10 if x > 2 or x < 0: print(x)

72

10

In [50]:

if x < 2 or x > 20: print(x)

73

It's possible to negate a boolean expression using the keyword `not`

:

74

In [51]:

print("x | not x") print("----------") for x in [True, False]: print("%d | %d" % (x, not x))

75

x | not x
----------
1 | 0
0 | 1

A more complex truth table demonstrating the duality

$\overline{AB} = \overline{A}+\overline{B}$:

76

In [52]:

print("A B | A and B | !(A and B) | !A or !B") print("-------------------------------------------") for A in [True, False]: for B in [True, False]: print("%d %d | %-7d | %-12d| %d" % (A, B, A and B, not (A and B), not A or not B))

77

A B | A and B | !(A and B) | !A or !B
-------------------------------------------
1 1 | 1 | 0 | 0
1 0 | 0 | 1 | 1
0 1 | 0 | 1 | 1
0 0 | 0 | 1 | 1

Loops are useful for executing blocks of code as long as a logical condition is satisfied.

Once the loop condition is no longer satisfied, the flow of control is returned to the main body of the program. Note that **infinite loops**, a serious runtime bug where the loop condition never evaluates to `False`

, are allowed, so you have to be careful.

The `while`

loop evaluates until a condition is false. Note that loops can be nested inside each other, and can also contain nested conditional statements.

78

In [53]:

i = 0 while i < 10: # Loop condition: i < 10 i += 1 # Increment the value of i if i % 2 == 0: # Print i if it's even print(i)

79

2
4
6
8
10

The `for`

loop provides the same basic functionality as the `while`

loop, but allows for a simpler syntax in certain cases.

For example, if we wanted to access all the elements inside a list one by one, we could write a while loop with a variable index `i`

and access the list elements as `listname[i]`

, incrementing `i`

until it's the same size as the length of the list.

However, the `for`

loop lets us avoid the need to declare an index variable. For example:

80

In [54]:

for x in range(1,11): # Loop through a list of values [1..10] if x % 2 == 0: # Print the list value if it's even print(x)

81

2
4
6
8
10

In [55]:

for i, x in enumerate(['a', 'b', 'c', 'd', 'e']): print("%d %s" % (i+1, x))

82

1 a
2 b
3 c
4 d
5 e

If we are interested in building lists we can start from a blank list and append things to it in a for loop or use a **list comprehension** which combines for loops and list creation into line. The syntax is a set of square brackets that contains formula and a for loop.

83

In [56]:

squaredrange = [e**2 for e in range(1,11)] print(squaredrange)

84

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

You can also loop through **two lists simultaneously** using the `zip`

function:

85

In [57]:

mylist = range(1,11) mylist2 = [e**2 for e in mylist] for x, y in zip(mylist, mylist2): print("%2d %4d" % (x, y))

86

1 1
2 4
3 9
4 16
5 25
6 36
7 49
8 64
9 81
10 100

To illustrate the zip function, this is what it does:

87

In [58]:

mylistA=[1,2,3] mylistB=[4,5,6] print(zip(mylistA,mylistB))

88

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

Functions are subroutines that accept some input and produce zero or more outputs. They are typically used to define common tasks in a program.

Rule of thumb: if you find that you are copying a piece of code over and over inside your script, it should probably go into a function.

The following function will round integers to the nearest 10:

89

In [59]:

def round_int(x): return 10 * ((x + 5)/10) for x in range(2, 50, 5): print("%5d %5d" % (x, round_int(x)))

90

2 0
7 10
12 10
17 20
22 20
27 30
32 30
37 40
42 40
47 50

With the small amount we've gone through, you can already write reasonably sophisticated programs. For example, we can write a loop that generates the Fibonacci sequence.

Just to remind you, the Fibonacci sequence is the list of numbers

1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ...

It is defined by the linear homogeneous recurrence relation

$F_{n} = F_{n-1} + F_{n-2}$, where $F_0=F_1=1$.

The exercise is:

- Write a Python function that generate $F_n$ given $n$.
- Use your function to generate the first 100 numbers in the Fibonacci sequence.

91

In [60]:

# Easy implementation: recursive function def fib(n): """Generate term n of the Fibonacci sequence""" if n <= 1: # if n==0 or n==1: return 1 return 1 else: return fib(n-1) + fib(n-2)

92

In [61]:

for n in range(0, 35): Fn = fib(n) print("%3d%25d" % (n, Fn))

93

0 1
1 1
2 2
3 3
4 5
5 8
6 13
7 21
8 34
9 55
10 89
11 144
12 233
13 377
14 610
15 987
16 1597
17 2584
18 4181
19 6765
20 10946
21 17711
22 28657
23 46368
24 75025
25 121393
26 196418
27 317811
28 514229
29 832040
30 1346269
31 2178309
32 3524578
33 5702887
34 9227465

This function will work just fine for small n. Unfortunately, the recursive calls to `fib`

cause the **function call stack** to grow rapidly with n. When n gets sufficiently large, you may hit the Python call stack limit. At that point your program will crash.

Here is a more efficient approach that does not require recursion:

94

In [62]:

def fibBetter(n): """Generate the Fibonacci series at position n""" a, b = 0, 1 while n > 0: # build up the series starting from the end (high n to n=0) a, b, n = b, a+b, n-1 # store results in loop variables return b

95

In [63]:

for n in range(0, 100): Fn = fibBetter(n) print("%3d%25d" % (n, Fn))

96

0 1
1 1
2 2
3 3
4 5
5 8
6 13
7 21
8 34
9 55
10 89
11 144
12 233
13 377
14 610
15 987
16 1597
17 2584
18 4181
19 6765
20 10946
21 17711
22 28657
23 46368
24 75025
25 121393
26 196418
27 317811
28 514229
29 832040
30 1346269
31 2178309
32 3524578
33 5702887
34 9227465
35 14930352
36 24157817
37 39088169
38 63245986
39 102334155
40 165580141
41 267914296
42 433494437
43 701408733
44 1134903170
45 1836311903
46 2971215073
47 4807526976
48 7778742049
49 12586269025
50 20365011074
51 32951280099
52 53316291173
53 86267571272
54 139583862445
55 225851433717
56 365435296162
57 591286729879
58 956722026041
59 1548008755920
60 2504730781961
61 4052739537881
62 6557470319842
63 10610209857723
64 17167680177565
65 27777890035288
66 44945570212853
67 72723460248141
68 117669030460994
69 190392490709135
70 308061521170129
71 498454011879264
72 806515533049393
73 1304969544928657
74 2111485077978050
75 3416454622906707
76 5527939700884757
77 8944394323791464
78 14472334024676221
79 23416728348467685
80 37889062373143906
81 61305790721611591
82 99194853094755497
83 160500643816367088
84 259695496911122585
85 420196140727489673
86 679891637638612258
87 1100087778366101931
88 1779979416004714189
89 2880067194370816120
90 4660046610375530309
91 7540113804746346429
92 12200160415121876738
93 19740274219868223167
94 31940434634990099905
95 51680708854858323072
96 83621143489848422977
97 135301852344706746049
98 218922995834555169026
99 354224848179261915075

If we want to use libraries and modules not defined within the built-in functionality of python we have to import them. There are a number of ways to do this.

97

In [64]:

import numpy, scipy

98

This imports the module `numpy`

and the module `scipy`

, and creates a reference to that modules in the current namespace. After you’ve run this statement, you can use `numpy.name`

and `scipy.name`

to refer to constants, functions, and classes defined in module numpy and scipy.

99

In [65]:

numpy.pi

100

3.141592653589793

In [66]:

from numpy import *

101

This imports the module numpy, and creates references in the current namespace to all public objects defined by that module (that is, everything that doesn’t have a name starting with “_”).

Or in other words, after you’ve run this statement, you can simply use a plain name to refer to things defined in module numpy. Here, numpy itself is not defined, so numpy.name doesn’t work. If name was already defined, it is replaced by the new version. Also, if name in numpy is changed to point to some other object, your module won’t notice.

102

In [67]:

pi

103

3.141592653589793

In [68]:

from scipy import special print(special.erf(0), special.erf(1), special.erf(2))

104

(0.0, 0.84270079294971478, 0.99532226501895271)

This imports the module `scipy`

, and creates references in the current namespace functions in the *submodule* `special`

. We then make 3 function calls to the Error Function `erf`

.

105

In [69]:

import numpy as np

106

This imports `numpy`

but assigns the name of the module to `np`

so that you can type `np`

rather than `numpy`

when you want to access variables and functions defined inside the module.

107

In [70]:

np.pi

108

3.141592653589793

In [71]:

np.arange(0,8) # acts like the range function, but return a numpy array

109

array([0, 1, 2, 3, 4, 5, 6, 7])

In [72]:

np.arange(0,8, 0.1) # unlike builtin range, you can use non-integer stride

110

array([ 0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ,
1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2. , 2.1,
2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 3. , 3.1, 3.2,
3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 4. , 4.1, 4.2, 4.3,
4.4, 4.5, 4.6, 4.7, 4.8, 4.9, 5. , 5.1, 5.2, 5.3, 5.4,
5.5, 5.6, 5.7, 5.8, 5.9, 6. , 6.1, 6.2, 6.3, 6.4, 6.5,
6.6, 6.7, 6.8, 6.9, 7. , 7.1, 7.2, 7.3, 7.4, 7.5, 7.6,
7.7, 7.8, 7.9])

111

NumPy is optimized for numerical work. The `array`

type inside of the module behaves a lot like a list, but it is *vectorized* so that you can apply arithmetic operations and other functions to the array without having to loop through it.

For example, when we wanted to square every element inside a python list we used a list comprehension:

112

In [73]:

mylist = range(1,11) [x**2 for x in mylist]

113

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

This isn't that hard, but the syntax is a little ugly and we do have to explicitly loop through the list. In contrast, to square all the elements in the NumPy array you just apply the operator to the array variable itself:

114

In [74]:

myarray = np.arange(1,11) myarray=myarray**2 print(myarray) myarray*2 # print(myarray)

115

[ 1 4 9 16 25 36 49 64 81 100]

array([ 2, 8, 18, 32, 50, 72, 98, 128, 162, 200])

NumPy provides two functions to give evenly spaced numbers on linear or logarithmic scales.

116

In [75]:

np.linspace(1, 10, 21) # gives 21 evenly spaced numbers in [1..10]

117

array([ 1. , 1.45, 1.9 , 2.35, 2.8 , 3.25, 3.7 , 4.15,
4.6 , 5.05, 5.5 , 5.95, 6.4 , 6.85, 7.3 , 7.75,
8.2 , 8.65, 9.1 , 9.55, 10. ])

In [76]:

np.logspace(1, 6, 6) # gives 6 logarithmically spaced numbers # between 1e1=10 and 1e6=1000000

118

array([ 1.00000000e+01, 1.00000000e+02, 1.00000000e+03,
1.00000000e+04, 1.00000000e+05, 1.00000000e+06])

In [77]:

np.logspace(1, 6, 6, base=2) # same as above, but base-2 logarithm

119

array([ 2., 4., 8., 16., 32., 64.])

An extremely useful feature in NumPy is the ability to create a "mask" array which can select values satisfying a logical condition:

120

In [78]:

x = np.arange(0, 8) # [0, 1, 2, 3, 4, 5, 6, 7] y = 3*x # [0, 3, 6, 9, 12, 15, 18, 21] c = x < 3 print(c)

121

[ True True True False False False False False]

In [79]:

print(x[c])

122

[0 1 2]

In [80]:

print(y[c]) print(y[x >= 3])

123

[0 3 6]
[ 9 12 15 18 21]

In [81]:

c = (x<3) | (x>5) # Combine cuts with bitwise OR or AND print(y[c])

124

[ 0 3 6 18 21]

This is the type of selection used *all the time* in data analysis.

125

Standard Python has functions to read basic text and binary files from disk.

However, for numerical analysis your files will usually be nicely formatted into numerical columns separated by spaces, commas, etc. For reading such files, NumPy has a nice function called `genfromtxt`

:

126

In [82]:

# Load data from file into a multidimensional array data = np.genfromtxt("data.txt") x = data[:,0] # x is the first column (numbering starts @ 0) y = data[:,1] # y is the second column print(x) print(y)

127

[ 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.]
[ 1. 4. 9. 16. 25. 36. 49. 64. 81. 100.]

Matplotlib is used to plot data and can be used to produce the usual xy scatter plots, contour plots, histograms, etc. that you're used to making for all basic data analyses.

I strongly recommend that you go to the Matplotlib website and check out the huge plot gallery. This is the easiest way to learn how to make a particular kind of plot.

**Note**: when you want to plot something in an IPython notebook, put the magic line

`%matplotlib inline`

before you import the `matplotlib`

module. This will ensure that your plots appear inside the notebook. Otherwise the plots will pop open in another window, which can be annoying.

128

In [83]:

%matplotlib inline import matplotlib.pyplot as plt

129

In [84]:

plt.plot(x, y, "k.") plt.xlabel("x [arb. units]") plt.ylabel("y [arb. units]") plt.title("Some XY data")

130

Text(0.5,1,'Some XY data')

Here is an example of how to change the default formatting of the text in your plot. Also note how LaTeX is supported!

131

In [85]:

import matplotlib as mpl mpl.rc("font", family="serif", size=16) plt.plot(x, y, "k.") plt.xlabel(r"$\sin({x)}$ [arb. units]") plt.ylabel(r"$\zeta(y)$ [arb. units]") plt.title("Some XY data")

132

Text(0.5,1,'Some XY data')

Here we create some fake data with NumPy and plot it, including a legend.

133

In [86]:

x = x=np.linspace(-np.pi, np.pi, 1000,endpoint=True) c = np.cos(x) s = np.sin(x) plt.plot(x,c,label="Cosine",color="r",linestyle="--",linewidth=2) plt.plot(x,s,label="Sine",color="b",linestyle="-.",linewidth=2) plt.xlabel("$x$",fontsize=14) plt.xlim(-np.pi,np.pi) # Override default ticks and labels xticks = [-np.pi, -0.5*np.pi, 0, 0.5*np.pi, np.pi] labels = ["$-\pi$", "$-\pi/2$", "$0$", "$\pi/2$", "$\pi$"] plt.xticks(xticks, labels) plt.ylabel("$y$",fontsize=14) plt.ylim(-1,1) plt.legend(fontsize=14, loc="best", numpoints=1)

134

<matplotlib.legend.Legend at 0x7f2a7c602410>

When running interactive sessions, you can use the built-in help function to view module and function documentation.

For example, here is how to view the internal documentation for the built-in function that calculates the greatest common divisor of two numbers:

135

In [87]:

from fractions import gcd help(gcd)

136

Help on function gcd in module fractions:
gcd(a, b)
Calculate the Greatest Common Divisor of a and b.
Unless b==0, the result will have the same sign as b (so that when
b is divided by it, the result comes out positive).

The `inspect`

module is nice if you actually want to look at the **source code** of a function. Just import inspect and call the `getsource`

function for the code you want to see:

137

In [88]:

from inspect import getsource print(getsource(gcd))

138

def gcd(a, b):
"""Calculate the Greatest Common Divisor of a and b.
Unless b==0, the result will have the same sign as b (so that when
b is divided by it, the result comes out positive).
"""
while b:
a, b = b, a%b
return a

In [ ]:

139