Computation for Physics 計算物理概論 Python Programming for Physicists.

230
Computation for Physics 計計計計計計 Python Programming for Physicists

Transcript of Computation for Physics 計算物理概論 Python Programming for Physicists.

Page 1: Computation for Physics 計算物理概論 Python Programming for Physicists.

Computation for Physics計算物理概論

Python Programming for Physicists

Page 2: Computation for Physics 計算物理概論 Python Programming for Physicists.

What is Python?

Page 3: Computation for Physics 計算物理概論 Python Programming for Physicists.

Python Programming Language

• Python is a general-purpose, high-level programming language whose design philosophy emphasizes code readability.

• Python supports multiple programming paradigms, including object-oriented, imperative and functional programming styles.

• Python features a fully dynamic type system and automatic memory management,

Page 4: Computation for Physics 計算物理概論 Python Programming for Physicists.

PEP 20 “The Zen of Python”PEP=Python Enhancement Proposal

• Beautiful is better than ugly.• Explicit is better than implicit.• Simple is better than complex.• Complex is better than complicated.• Readability counts.Guido van Rossum• Creator of python• Benevolent Dictator for Life

Page 5: Computation for Physics 計算物理概論 Python Programming for Physicists.

Programming Language

• High-level language• Low-level language

Page 6: Computation for Physics 計算物理概論 Python Programming for Physicists.

Compilation v.s. Interpretation

SOURCECODE INTERPRETER OUTPUT

SOURCECODE OUTPUTCOMPILER OBJECT

CODEEXECUTOR

Page 7: Computation for Physics 計算物理概論 Python Programming for Physicists.

Python: Interpreted Language

• Interactive mode– You type Python programs and the interpreter

displays the result.

• Script mode– You store code in a file and use the interpreter to

execute the contents of the file.

Page 8: Computation for Physics 計算物理概論 Python Programming for Physicists.

PYTHON DEVELOPMENT ENVIRONMENT

Page 9: Computation for Physics 計算物理概論 Python Programming for Physicists.

Python(x,y)Scientific-oriented Python Distribution based on Qt and Spyder

• Python(x,y) is a free scientific and engineering development software for numerical computations, data analysis and data visualization based on Python programming language, Qt graphical user interfaces and Spyder interactive scientific development environment.

• https://code.google.com/p/pythonxy/

Page 10: Computation for Physics 計算物理概論 Python Programming for Physicists.

Download Python(x,y)

Download from one of the mirrors

Page 11: Computation for Physics 計算物理概論 Python Programming for Physicists.

Python(x,y) Plugins=Python Packages

• IDLE, IPython– Development environment

• NumPy– Multidimensional arrays support and basic operations

• SciPy– Advanced math, signal processing, optimization, statistics

• Matplotlib– 2D plotting library

• VPython– Creation of 3D interactive models of physical systems

• SymPy– Symbolic Mathematics Library

Page 12: Computation for Physics 計算物理概論 Python Programming for Physicists.

IDLEIntegrated DeveLopment Environment

• IDLE is the Python IDE built with the Tkinter GUI toolkit.

• IDLE has the following features:– coded in 100% pure Python, using the Tkinter GUI toolkit– cross-platform: works on Windows and Unix– multi-window text editor with multiple undo, Python colorizing and many other

features, e.g. smart indent and call tips– Python shell window (a.k.a. interactive interpreter)– debugger (not complete, but you can set breakpoints, view and step)

• It is a part of the standard library and is described in the reference manual: http://www.python.org/doc/current/lib/idle.html

• The source code for IDLE is included in the standard library. More information on developing IDLE can be found at http://docs.python.org/devguide/

Page 13: Computation for Physics 計算物理概論 Python Programming for Physicists.

IDLE

Integrated development environment

Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32Type "copyright", "credits" or "license()" for more information.>>>

Page 14: Computation for Physics 計算物理概論 Python Programming for Physicists.

Start IDLE開始所有程式 python(x,y)IDLE

Page 15: Computation for Physics 計算物理概論 Python Programming for Physicists.

File New Window

Page 16: Computation for Physics 計算物理概論 Python Programming for Physicists.

File Save As

Page 17: Computation for Physics 計算物理概論 Python Programming for Physicists.

Run Run Module

Page 18: Computation for Physics 計算物理概論 Python Programming for Physicists.

Python Shell

Page 19: Computation for Physics 計算物理概論 Python Programming for Physicists.

PYTHONPROGRAMMING LANGUAGE

Page 20: Computation for Physics 計算物理概論 Python Programming for Physicists.

What is a Program?

• A sequence of instructions that specifies how to perform a computation

• Basic instructions– Input– Output– Math– Conditional execution– Repetition

Page 21: Computation for Physics 計算物理概論 Python Programming for Physicists.

What is Debugging

• Programming is error-prone• Programming errors=bugs• The process of tracking bugs down=debugging

Three kinds of errors• Syntax errors• Runtime errors• Semantic errors

Page 22: Computation for Physics 計算物理概論 Python Programming for Physicists.

First Program=“Hello, World!”

• Traditionally, the first program you write in a new language is to display the words – “Hello, World”

print(‘Hello, World!’)

• Case-sensitive

Page 23: Computation for Physics 計算物理概論 Python Programming for Physicists.

Variables, Expressions, Statements

Page 24: Computation for Physics 計算物理概論 Python Programming for Physicists.

Variables, Values, Stage Diagram

• State diagram– variables value

• message ’And now for something’• n 17• pi 3.1415926535• z 1+3i

Page 25: Computation for Physics 計算物理概論 Python Programming for Physicists.

Python Keywords

False class finally is returnNone continue for lambda tryTrue def from nonlocal whileand del global not withas elif if or yieldassert else import passbreak except in raise

Page 26: Computation for Physics 計算物理概論 Python Programming for Physicists.

Assignment Statement

• “=“ is an “assignment”• message = ’And now for something’• n = 17• pi = 3.1415926535• z = 1+3j

• 2*x=y – Syntax Error

Page 27: Computation for Physics 計算物理概論 Python Programming for Physicists.

data types

• integer=integer number• float=floating point number• complex=complex floating point number• str=string>>> type(17)<type ‘int’>>>> type(3.2)<type ‘float’>>>> type(1+3j)<type ‘complex’>>>> type(‘Hello, World!’)<type ‘str’>

Page 28: Computation for Physics 計算物理概論 Python Programming for Physicists.

data types

• integer=integer number• float=floating point number• complex=complex floating point number• str=string>>> type(n)<type ‘int’>>>> type(pi)<type ‘float’>>>> type(z)<type ‘complex’>>>> type(message)<type ‘str’>

Page 29: Computation for Physics 計算物理概論 Python Programming for Physicists.

Dynamically Typed

>>> x=3# x is of integer type

>>> x=3.0# x is of float type

>>> x=‘blah’# x is of string type

>>> x=[3,3.0,’blah’]# x is of list type

Page 30: Computation for Physics 計算物理概論 Python Programming for Physicists.

Variable Names & Keywords

• Keywords– The interpreter uses keywords to recognize the

structure of the program, and they cannot be used as variable names

– and– if– else– while– …etc

Page 31: Computation for Physics 計算物理概論 Python Programming for Physicists.

Operators & Operand

• +– addition: x+y

• -– subtraction: x-y

• *– multiplication: x*y

• /– division: x/y

• **– exponentizaton x**y

• //– the integer part of x/y: 14.5//3.64.0, 14.5//3.73.0

• %– module=remainder after x/y: 1.5%0.40.3

Page 32: Computation for Physics 計算物理概論 Python Programming for Physicists.

Expression & Statement

• An expression is a combination of values, variables, and operators. – x– x+17

• A statement is a unit of code that the Python interpreter can execute.– x=3– print(‘Hello’)

Page 33: Computation for Physics 計算物理概論 Python Programming for Physicists.

Interactive & Script Mode

• Interactive mode• Script mode

• Major difference– Python actually evaluates the expression, but it

doesn’t display the value unless you tell it to

Page 34: Computation for Physics 計算物理概論 Python Programming for Physicists.

Order of Operations

• Rue of precedence

• Parentheses• Exponentiation• Multiplication and Division• Left to right for operators with the same

precedence

Page 35: Computation for Physics 計算物理概論 Python Programming for Physicists.

Comments

• It is a good idea to add notes to your programs to explain in natural language what the program is doing.

• Comments start with the # symbol

# compute the velocityv=dx / dtv=dx / dt # compute the velocity

Page 36: Computation for Physics 計算物理概論 Python Programming for Physicists.

x=1print(x)

• variable• assignment statement

Page 37: Computation for Physics 計算物理概論 Python Programming for Physicists.

Variables Types

• Integer– x=1

• Float– x=1.5– x=1.0– x=float(1)

• Complex– x=1.5+2.0j– x=1.5+0j– x=complex(1.5)

Page 38: Computation for Physics 計算物理概論 Python Programming for Physicists.

string

String literals can be written enclosed in either two single or two double quotes

x='This is a string'x="This is a string"

y='1.234' # create a string with value ‘1.234’y=1.234 # create a real number with value 1.234

z='x=1' # create a string with value ‘x=1’

Page 39: Computation for Physics 計算物理概論 Python Programming for Physicists.

type conversion functionsstring to integer

>>> int(‘32’)32>>> int(3.999)3>>> int(-2.3)-2

>>> int('Hello')ValueError: invalid literal for int() with base 10: 'hello'>>> int('3.999')ValueError: invalid literal for int() with base 10: '3.999'

Page 40: Computation for Physics 計算物理概論 Python Programming for Physicists.

Help(int)>>>help(int)Help on class int in module __builtin__:

class int(object) | int(x[, base]) -> integer | | Convert a string or number to an integer, if possible. A floating point | argument will be truncated towards zero (this does not include a string | representation of a floating point number!) When converting a string, use | the optional base. It is an error to supply a base when converting a | non-string. If base is zero, the proper base is guessed based on the | string content. If the argument is outside the integer range a | long object will be returned instead.

Page 41: Computation for Physics 計算物理概論 Python Programming for Physicists.

Type conversion functionsstring to float

>>> float(‘32’)32.0>>> (‘3.14159’)3.14159>>> int(float(‘3.999’))3

class float(object) | float(x) -> floating point number | | Convert a string or number to a floating point number, if possible.

Page 42: Computation for Physics 計算物理概論 Python Programming for Physicists.

type conversion functions‘blah’ to complex

>>> complex(32)(32+0j)>>> complex(‘3.2’)(3.2+0j)>>> complex(3+j)NameError: name 'j' is not defined>>> complex(3+0j)(3+0j)>>> complex(3+1j)(3+1j)>>> complex(‘3+j’)(3+1j)>>> complex(‘3+0j’)(3+0j)>>> complex(‘3+1j’)(3+1j)

Page 43: Computation for Physics 計算物理概論 Python Programming for Physicists.

Output Statements

>>> x = 1>>> print(x)1>>> x=1>>> y=2>>> print(x,y)1 2>>> print("The value of x is", x, "and y=",y)The value of x is 1 and y=2

Page 44: Computation for Physics 計算物理概論 Python Programming for Physicists.

Input Statement

>>> x = input("Enter the value of x:”)(The computer will stop and wait)>>> print("The value of x is ",x)

>>> x = input("Enter the value of x:")(You enter "Hello")>>> print("The value of x is ",x)The value of x is Hello

Page 45: Computation for Physics 計算物理概論 Python Programming for Physicists.

Input Statement

Convert input string to float

temp = input("Enter the value of x: ")x=float(temp)print("The value of x is ",x)

x = float(input("Enter the value of x: "))print("The value of x is ",x)

Page 46: Computation for Physics 計算物理概論 Python Programming for Physicists.

Python 3 v.s. Python 2

Include following statement in your codefrom __future__ import division, print_function• Division return a floating value– 3/2 1 # python 2– 3/2 1.5 # python 3– 4/2 2 # python 2– 4/2 2.0 #python 3

• Print is a statement– print "The energy is ", E # python 2– print("The energy is ",E) # python 3

Page 47: Computation for Physics 計算物理概論 Python Programming for Physicists.

Python 3 v.s. Python 2

• Input returns a string– In Python 3 the input function always returns a

string– x=input()– you enter ‘2.5’– type(x) <type ‘float’> # python 2– type(x) <type ‘str’> # python 3

– x=raw_input()– type(x) <type ‘str’> # python 2

Page 48: Computation for Physics 計算物理概論 Python Programming for Physicists.

Python 3 v.s. Python 2

• Iterators v.s. List– range(10) [0,1,2,3,4,5,6,7,8,9] # python 2– range(10) range(0,10) # python 3– list(range(10)) [0,1,2,3,4,5,6,7,8,9] # python 3

– x = [1,2,3]– map(log,x) [0.0, 0.69…, 1.09…] # python 2– map(log,x) <map object at …> # python 3– list(map(log,x)) [0.0, 0.69…, 1.09…] # python 3

Page 49: Computation for Physics 計算物理概論 Python Programming for Physicists.

“x=a+b” is an assignment statement!

• x = 1 – x 1

• x = x+1– x x+1– temp = x+1– x=temp

• x = x**2 – 2– x x**2 - 2

Page 50: Computation for Physics 計算物理概論 Python Programming for Physicists.

Python Modifiers

• x += 1 – x = x + 1

• x -= 4– x = x - 4

• x *= -2.6– x = x*(-2.6)

• x /= 5*y– x = x / (5*y)

• x //= 3.4– x = x // 3.4

Page 51: Computation for Physics 計算物理概論 Python Programming for Physicists.

Assignment of Multiple Variables

• x, y = 1, 2.5– x = 1– y = 2.5

• x, y = 2*z+1, (x+y)/3– temp1 = 2*z+1– temp2 = (x+y)/3– x = temp1– y = temp2

Page 52: Computation for Physics 計算物理概論 Python Programming for Physicists.

Assignment of Multiple Variables

You can interchange of values of two variables• x, y = y, x– temp = y– y = x– x = temp

Page 53: Computation for Physics 計算物理概論 Python Programming for Physicists.

Try: A ball dropped from a tower

• Write a program to ask the user to enter h the height of the tower and a time interval t, then prints the height of the ball above the ground at time t after it is dropped.

Page 54: Computation for Physics 計算物理概論 Python Programming for Physicists.

A ball dropped from a tower

Simple version

h = float(input(“Enter the height of the tower: "))t = float(input("Enter the time interval: "))s = 9.81*t**2/2print("The height of the ball is", h-s, "meters")

To make it more readableg = 9.81s = g*t**2/2

Page 55: Computation for Physics 計算物理概論 Python Programming for Physicists.

Another ball dropped from a tower

• A ball is dropped from a tower of height h with initial velocity zero. Write a program that asks the user to enter the height in meters and the calculates and prints the time the ball takes until it hits the ground.

Page 56: Computation for Physics 計算物理概論 Python Programming for Physicists.

Functions, Packages, & Modules

>>> from math import log>>> x = log(2.5)

log(...) log(x[, base])

Return the logarithm of x to the given base. If the base not specified, returns the natural logarithm (base e) of x.

Page 57: Computation for Physics 計算物理概論 Python Programming for Physicists.

math—Mathematical Functionsfrom math import *

Functions:• log• log10• exp• sin, cos, tan• asin, acos, atan• sinh, cosh, tanh• sqrtConstants:• pi• e

Page 58: Computation for Physics 計算物理概論 Python Programming for Physicists.

Try: Converting polar coordinates

• Get the user to enter the value of r and θ• Convert those values to Cartesian coordinates• Print out the results

Page 59: Computation for Physics 計算物理概論 Python Programming for Physicists.

Converting polar coordinates

from math import sin, cos, pi

r = float(raw_input("Enter r: "))d = float(raw_input("Enter theta in degrees: "))theta = d*pi/180

x = r*cos(theta)y = r*sin(theta)print("x= ",x, "y= ",y)

Page 60: Computation for Physics 計算物理概論 Python Programming for Physicists.

Comment Statements

from math import sin, cos, pi# Ask the user for the values of the radius and angler = float(input(“Enter r: “))d = float(input(“Enter theta in degrees: “))# Convert the angle to radianstheta = d*pi/180# Calculate the equivalent Cartesian coordinatesx = r*cos(theta)y = r*sin(theta)

print(“x= “,x,”y= “,y) # Print out the results

Page 61: Computation for Physics 計算物理概論 Python Programming for Physicists.

Multiline \

1+2\3

Page 62: Computation for Physics 計算物理概論 Python Programming for Physicists.

help

>>> help(sum)Help on built-in function sum in module __builtin__:

sum(...) sum(sequence[, start]) -> value

Returns the sum of a sequence of numbers (NOT strings) plus the value of parameter 'start' (which defaults to 0). When the sequence is empty, returns start.

Page 63: Computation for Physics 計算物理概論 Python Programming for Physicists.

Branching

Page 64: Computation for Physics 計算物理概論 Python Programming for Physicists.

Boolean expressions

>>>5 == 5True

>>>5 == 6False

>>>type(True)<type ‘bool’>

>>>type(False)<type ‘boo’>

Page 65: Computation for Physics 計算物理概論 Python Programming for Physicists.

Relational Operators

x == y # x is equal to yx != y # x is not equal to yx > y # x is greater than yx < y # x is less than yx >= y # x is greater than or equal to yx <= y # x is less than or equal to y

Page 66: Computation for Physics 計算物理概論 Python Programming for Physicists.

Logical Operators

• and• or• not

>>> 3>1 and 3>2True>>> 3>1 or 3<2Ture>> not (3>1)False

Page 67: Computation for Physics 計算物理概論 Python Programming for Physicists.

Numerical Values of True & False

>>>int(True)1>>>int(False)0>>>0==FalseTrue>>>1==TrueTrue>>>2==TrueFalse

Page 68: Computation for Physics 計算物理概論 Python Programming for Physicists.

Conditional Execution

if x > 0: print(‘x is positive’) print(‘Hello, World!’)

Condition

Compound statements

Page 69: Computation for Physics 計算物理概論 Python Programming for Physicists.

Alternative Execution

if x % 2 ==0: print(‘x is even’)else: print(‘x is odd’)

Page 70: Computation for Physics 計算物理概論 Python Programming for Physicists.

Chained Conditionals

if x < y: print(‘x is less than y’)elif x > y: print(‘x is greater than y’)else: print(‘x and y are equal’)

elif=“else if”

Page 71: Computation for Physics 計算物理概論 Python Programming for Physicists.

Chained Conditionals

if choice == ‘a’ draw_a()elif choice == ‘b’ draw_b()elif choice == ‘c’ draw_c()

function call

Page 72: Computation for Physics 計算物理概論 Python Programming for Physicists.

Nested Conditionals

if x == y: print(‘x and y are equal’)else: if x < y: print(‘x is less than y’) else: print(‘x is greater than y’)

Page 73: Computation for Physics 計算物理概論 Python Programming for Physicists.

Use Logic Operators to Simplify

if 0 < x: if x < 10: print(‘x is ……’)

if 0 < x and x < 10: print(‘x is ……’)

Page 74: Computation for Physics 計算物理概論 Python Programming for Physicists.

If (anything):

• 0 is interpreted as False• Any other number is interpreted as True• Empty lists and objects return False• Non-empty lists and objects return True

Page 75: Computation for Physics 計算物理概論 Python Programming for Physicists.

if (anything):>>>if 0:... print(‘x’)>>>if 1:... print(‘x’)x>>>if 1.1:... print(‘x’)x>>>d=[]>>>if d:... print(‘x’)>>>d=[1,2]>>>if 1.1:... print(‘x’)x

Page 76: Computation for Physics 計算物理概論 Python Programming for Physicists.

Iteration

Page 77: Computation for Physics 計算物理概論 Python Programming for Physicists.

The ‘While’ Statement

n=10while n > 0: print(n) n = n-1

Loop

You should change the value of one or more variables, otherwise it will become an infinite loop

Page 78: Computation for Physics 計算物理概論 Python Programming for Physicists.

Will the Loop Terminates for all n?

n = 3while n != 1: print(n) if n%2 == 0: # n is even n = n//2 else: # n is odd n = n*3+1

3,10,5,16,8,4,2,1

Page 79: Computation for Physics 計算物理概論 Python Programming for Physicists.

Break

while True: line = raw_input(‘> ‘) if line == ‘done’: break print(line)print(‘finished’)

Page 80: Computation for Physics 計算物理概論 Python Programming for Physicists.

Try: Fibonacci Sequence

• Sequence of integers in which each is the sum of previous two, starting with 1,1

• 1, 1, 2, 3, 5, 8, 13, 21f1 = 1f2 = 1next = f1 + f2while f1 <= 1000: print(f1) f1 = f2 f2 = next next = f1 + f2

Page 81: Computation for Physics 計算物理概論 Python Programming for Physicists.

Fibonacci Sequence, Simplified

f1, f2 = 1, 1while f1 < 1000: print(f1) f1, f2 = f2, f1+f2

Page 82: Computation for Physics 計算物理概論 Python Programming for Physicists.

Try: Catalan Numbers

• Write a program that prints in increasing order all Catalan numbers less than or equal to one billion

Page 83: Computation for Physics 計算物理概論 Python Programming for Physicists.

Try: Square Roots

• Newton’s method to find the square root of a– Start from any estimate x– Compute a better estimate – Repeat until it converges

• This is an algorithm

Page 84: Computation for Physics 計算物理概論 Python Programming for Physicists.

Newton’s Method, Interactively

>>>a=4.0>>>x=3.0>>>y=(x+a/x)/2>>>print(y)2.16666666667>>>x=y>>>y=(x+a/x)/2>>>print(y)2.00641025641

Page 85: Computation for Physics 計算物理概論 Python Programming for Physicists.

Square Roots

while True: print(x) y = (x + a/x ) / 2 if y == x: break x = y

Page 86: Computation for Physics 計算物理概論 Python Programming for Physicists.

Square Roots (Safer Check)

epsilon = 0.0000001while True: print(x) y = (x + a/x ) / 2 if abs(y-x) < epsilon: break x = y

Page 87: Computation for Physics 計算物理概論 Python Programming for Physicists.

User-Defined Functions

Page 88: Computation for Physics 計算物理概論 Python Programming for Physicists.

Factorial Function

def factorial(n): f = 1.0 for k in range(1,n+1) f *= k return f

a = factorial(10)b = factorial(r+2*s)

Local variables

Argument

Page 89: Computation for Physics 計算物理概論 Python Programming for Physicists.

Distance Function

def distance(r, theta, z): x = r*cos(theta) y = r*sin(theta) d = sqrt(x**2+y**2+z**2) return d

D = distance(2.0,0.1,-1.5)

Multiple arguments

Page 90: Computation for Physics 計算物理概論 Python Programming for Physicists.

Use “def” Statement to Define a Function

def add(arg1, arg2) x=arg1+arg2 return x

def power(x,y) if x <=0: return 0 else return x**y

Page 91: Computation for Physics 計算物理概論 Python Programming for Physicists.

Return Arbitrary Type

def cartesian(r,theta): x = r*cos(theta) y = r*sin(theta) position = [x,y] return position

def f(z): # Some calculations here... return a,bx,y = f(1)

Page 92: Computation for Physics 計算物理概論 Python Programming for Physicists.

Return No Value

def print_vector(r): print("(",r[0],r[1],r[2],")")

Page 93: Computation for Physics 計算物理概論 Python Programming for Physicists.

Map User-defined Function to a List

def f(x): return 2*x-1newlist = list(map(f,oldlist))

Page 94: Computation for Physics 計算物理概論 Python Programming for Physicists.

Try: Square Root Function

• Write your own square root function• Input a and epsilon• Output square root of a• Compare the result with built-in function

sqrt()

Page 95: Computation for Physics 計算物理概論 Python Programming for Physicists.

Lists

Page 96: Computation for Physics 計算物理概論 Python Programming for Physicists.

Build-in Type: List

• List=[element1, elemen2, element3,…]

r = [ 1, 1, 2, 3, 5, 8, 13, 21]print(r)[ 1, 1, 2, 3, 5, 8, 13, 21]x = 1.0y = 1.5z = -2.2r = [x,y,z]r = [ 2*x, x+y, z/sqrt(x**2+y**2) ]

Page 97: Computation for Physics 計算物理概論 Python Programming for Physicists.

Access List Elements

>>> r = [ 1.0, 1.5, -2.2]>>> r[0] 1.0>>> r[4]Traceback (most recent call last): File "<stdin>", line 1, in <module>IndexError: list index out of range>>> r[-1]-2.2

The index number starts at zero

Page 98: Computation for Physics 計算物理概論 Python Programming for Physicists.

Modify List Elements

>>>r = [ 1.0, 1.5, -2.2]>>>r[0] = 3.51.0>>>r[3.5, 1.5, -2.2]

Page 99: Computation for Physics 計算物理概論 Python Programming for Physicists.

List Operations

>>> a = [1, 2, 3]>>> b = [4, 5, 6]>>> c = a + b>>> print(c)[1, 2, 3, 4, 5, 6]

>>> [0] * 4[0, 0, 0, 0]>>> [1, 2, 3] * 3[1, 2, 3, 1, 2, 3, 1, 2, 3]

Page 100: Computation for Physics 計算物理概論 Python Programming for Physicists.

List Functions

• Functions that take ‘list’ as an argument>>> r = [ 1.0, 1.5, -2.2]>>> sum(r)0.3>>> sum(r)/len(r)0.1

Page 101: Computation for Physics 計算物理概論 Python Programming for Physicists.

Meta-Function: map

• Apply functions to all elements of a list• map(log, r) take the natural logarithm of each

element of a list r

from math import logr = [ 1.0, 1.5, 2.2 ]logr = list(map(log,r))print(logr) [0.0, 0.4054651081, 0.7884573603]|

Page 102: Computation for Physics 計算物理概論 Python Programming for Physicists.

List Methods: Add an Element

>>>r = [ 1.0, 1.5, -2.2 ]>>>x = 0.8>>>r.append(2*x+1)>>>print(r)[1.0, 1.5, -2.2, 2.6]>>>t1 = [‘a’, ‘b’, ‘c’]>>>t2 = [‘d’, ‘e’]>>>t1.extend(t2)>>>print(t1)[‘a’, ‘b’, ‘c’, ‘d’, ‘e’]

Page 103: Computation for Physics 計算物理概論 Python Programming for Physicists.

Starts from an Empty List

>>>r=[]>>>r.append(1.0)>>>r.append(1.5)>>>r.append(-2.2)>>>print(r)[1.0, 1.5, -2.2]

Page 104: Computation for Physics 計算物理概論 Python Programming for Physicists.

List Methods: Remove am Element

>>>r = [ 1.0, 1.5, -2.2, 2.6 ]>>>r.pop()>>>print(r)[1.0, 1.5, -2.2]

>>>t =[‘a’, ‘b’, ‘c’]>>>x = t.pop(1)>>>print(t)>>>print(x)

Page 105: Computation for Physics 計算物理概論 Python Programming for Physicists.

Remove a known Element

>>>t = [‘a’, ‘b’, ‘c’]>>>t.remove(‘b’)>>>print(t)[‘a’, ‘c’]

Page 106: Computation for Physics 計算物理概論 Python Programming for Physicists.

Traveling a List: For Loop

r = [ 1, 3, 5 ]for n in r: print(n) print(2*n)print("Finished")1236510Finished

Page 107: Computation for Physics 計算物理概論 Python Programming for Physicists.

Traveling a List: For Loop

for i in [3, ‘Hello’, 9.5]: print i3Hello9.5

for x in []: print(‘This never happens’)

Page 108: Computation for Physics 計算物理概論 Python Programming for Physicists.

Run Some Code a Number of Times

r = [0, 1, 2, 3, 4 ]for i in r: blah blah blahprint(‘Finished’)

If we want to loop 10000000 times?

Page 109: Computation for Physics 計算物理概論 Python Programming for Physicists.

Built-in Function: range()

range(5)gives [0, 1, 2, 3, 4]

range(2,8)gives [2, 3, 4, 5, 6, 7]

range(2,20,3)gives [2, 5, 8, 11, 14, 17]

range(20,2,-3)gives [20, 17, 14, 11, 8, 5]

Page 110: Computation for Physics 計算物理概論 Python Programming for Physicists.

range(integer)

p = 10q = 2range(p/q) (error!)range(p//q) (ok!)

Page 111: Computation for Physics 計算物理概論 Python Programming for Physicists.

Performing a Sum

• Evaluate the sum s = 0.0for k in range(1,101): s += 1/kprint(s)

Page 112: Computation for Physics 計算物理概論 Python Programming for Physicists.

List Slices: list[lower:upper:step]

• Inclusive lower element index (default=0)• Exclusive upper element index (default=Length)>>>l=[1,2,3,4,5]>>>l[0:4][1,2,3,4]>>>l[0:4:2][1,3]>>>l[:4][1,2,3,4]>>>l[2:][3,4,5]>>>l[::2][1,3,5]

Page 113: Computation for Physics 計算物理概論 Python Programming for Physicists.

List Comprehensions

>>>[i*i for i in range(5)][0, 1, 4, 9, 16]

>>>[k*5 for k in [4,8,9]][20, 40, 45]

>>>[q**(0.5) for q in [4,6,9]][2.0, 3.0, 4.0]

>>>[ k%2 == 0 for k in range(5)][True, False, True, False, True]

Page 114: Computation for Physics 計算物理概論 Python Programming for Physicists.

Examples

Page 115: Computation for Physics 計算物理概論 Python Programming for Physicists.

Try: Emission Line of Hydrogen

R = 1.097e-2 for m in [1,2,3]: print("Series for m =", m) for k in [1,2,3,4,5]: n=m+k invlambda = R*(1/m**2-1/n**2) print(" ",1/invlambda," nm")

1𝜆

=𝑅 ( 1𝑚2−1

𝑛2 )

Page 116: Computation for Physics 計算物理概論 Python Programming for Physicists.

Use range() Function to Simplify

R = 1.097e-2for m in range(1,4): print("Series for m =",m) for n in range(m+1,m+6): invlambda = R*(1/m**2-1/n**2) print(" ",1/invlambda," nm")

Page 117: Computation for Physics 計算物理概論 Python Programming for Physicists.

Try: The Madelung Constant

• In condensed matter physics the Madelung constant gives the total electric potential felt by an atom in a solid. It depends on the charges on the other atoms nearby and their locations.

• Consider for instance solid sodium chloride—table salt. The sodium chloride crystal has atoms arranged on a cubic lattice, but with alternating sodium and chlorine atoms, the sodium ones having a single positive charge +e and the chlorine ones a single negative charge −e, where e is the charge on the electron.

• If we label each position on the lattice by three integer coordinates (i, j, k), then the sodium atoms fall at positions where i + j + k is even, and the chlorine atoms at positions where i + j + k is odd.

Page 118: Computation for Physics 計算物理概論 Python Programming for Physicists.

Try: The Madelung Constant• Consider a sodium atom at the origin, i = j = k = 0, the spacing of atoms on the

lattice is a. The distance from the origin to the atom at position (i, j, k) is

• The potential at the origin created by such an atom is

• The sign of the expression depending on whether i + j + k is even or odd. The total potential felt by the sodium atom is the sum of this quantity over all other atoms. Assume a cubic box around the sodium at the origin, with L atoms in all directions.

• M=Madelung constant is the value of M when L → ∞, but one can get a good approximation just by using a large value of L.

√ (𝑖𝑎 )2+( 𝑗𝑎 )2+(𝑘𝑎)2=𝑎√𝑖2+ 𝑗2+𝑘2

𝑉 (𝑖 , 𝑗 ,𝑘 )=± 𝑒

4𝜋 𝜀0𝑎√𝑖2+ 𝑗2+𝑘2

𝑉 𝑡𝑜𝑡= ∑𝑖 , 𝑗 ,𝑘=−𝐿 ,(𝑖 , 𝑗 ,𝑘 )≠(0,0,0)

𝐿

𝑉 (𝑖 , 𝑗 ,𝑘 )=¿ 𝑒4 𝜋 𝜀0𝑎

𝑀 ¿

Page 119: Computation for Physics 計算物理概論 Python Programming for Physicists.

Try: The Madelung Constant

• Write a program to calculate and print the Madelung constant for sodium chloride. Use as large a value of L as you can, while still having your program run in reasonable time—say in a minute or less.

Page 120: Computation for Physics 計算物理概論 Python Programming for Physicists.

The Semi-Empirical Mass Formula• A formula for calculating the approximate nuclear binding energy B of an atomic nucleus

with atomic number Z and mass number A:

• , , • Write a program that takes as its input the values of A and Z, and prints out the binding

energy for the corresponding atom. Use your program to find the binding energy of an atom with A = 58 and Z = 28. (Hint: Answer is around 490 MeV.)

• Modify your program to print out not the total binding energy B, but the binding energy per nucleon, which is B/A.

• Modify your program so that it takes as input just a single value of the atomic number Z and then goes through all values of A from A = Z to A = 3Z, to find the one that has the largest binding energy per nucleon. Have your program print out the value of A for this most stable nucleus and the value of the binding energy per nucleon.

• Modify your program so that, instead of taking Z as input, it runs through all values of Z from 1 to 100 and prints out the most stable value of A for each one. At what value of Z does the maximum binding energy per nucleon occur?

𝐵=𝑎1𝐴−𝑎2𝐴2 /3−𝑎3

𝑍 2

𝐴1 /3 −𝑎4( 𝐴−2𝑍 )2

𝐴+𝑎5𝐴1 /2

Page 121: Computation for Physics 計算物理概論 Python Programming for Physicists.

Try: Prime Factors and Prime Numbers

• Suppose we have an integer n and we want to know its prime factors. The prime factors can be calculated relatively easily by dividing repeatedly by all integers from 2 up to n and checking to see if the remainder is zero.

Page 122: Computation for Physics 計算物理概論 Python Programming for Physicists.

Try: Factor List

• Input an integer n• Output the factor list• Input=28• Output=[2,2,7]

Page 123: Computation for Physics 計算物理概論 Python Programming for Physicists.

Try: Factorlist

def factor(n): factorlist = [] k = 2 while k <= n: while n%k == 0: factorlist.append(k) n //= k #n=n//k k += 1 return factorlist

Page 124: Computation for Physics 計算物理概論 Python Programming for Physicists.

Try: Optimization: Find All the Primes

• Finds all the primes up to n. • Create a list to store the primes, which starts

out with just the one prime number 2 in it.• Look over n=2 to 10000• Call factor(n)• If the factorlist contains only one element, the

n is a prime. Add n to the prime list.• Slow and stupid, any better way?

Page 125: Computation for Physics 計算物理概論 Python Programming for Physicists.

Try: Optimization: Find All the Primes

• Finds all the primes up to n. • Create a list to store the primes, which starts out with

just the one prime number 2 in it. • For each number n from 3 to 10000 check whether the

number is divisible by any of the primes in the list up to and including . As soon as you find a single prime factor you can stop checking the rest of them—you know n is not a prime. If you find no prime factors or less then n is prime and you should add it to the list.

• You can print out the list all in one go at the end of the program, or you can print out the individual numbers as you find them.

Page 126: Computation for Physics 計算物理概論 Python Programming for Physicists.

Time your code!

• from time import *• start_time=time()• blah• blah• blah• end_time=time()• used_time=end_time-start_time

Page 127: Computation for Physics 計算物理概論 Python Programming for Physicists.

Good Programming Style

Page 128: Computation for Physics 計算物理概論 Python Programming for Physicists.

Good Programming Style

• Include comments in your programs.• Use meaningful variable names. • Use the right types of variables.• Import functions first.• Give your constants names.• Employ user-defined functions, where appropriate.• Print out partial results and updates throughout your program.• Lay out your programs clearly.• Don’t make your programs unnecessarily complicated.

Page 129: Computation for Physics 計算物理概論 Python Programming for Physicists.

Dictionary

Page 130: Computation for Physics 計算物理概論 Python Programming for Physicists.

Dictionary {Key:Value}>>>eng2sp = dict()>>>print(eng2sp){}>>>eng2sp[‘one’]='uno'>>>print(eng2sp){'one':'uno'}>>>eng2sp={'one':'uno','two':'dos','three':'tres'}>>>print(eng2sp}{'one':'uno','three':'tres','two':'dos'}>>>print(eng2sp['two'])dos>>>print(eng2sp['four'])KeyError: 'four'>>>len(eng2sp)3

Page 131: Computation for Physics 計算物理概論 Python Programming for Physicists.

Something as a Key or a Value?

>>>'one' in eng2spTrue>>>'uno' in eng2spFalse>>>vals = eng2sp.values()>>>'uno' in valsTrue

• Python2's "has_key()" is moved in Python3

Page 132: Computation for Physics 計算物理概論 Python Programming for Physicists.

Dictionary as a Set of Counters

• Want to count how many times each letter appears in a given string.

• Create 26 variables, one for each letter of the alphabet.

• Create a list with 26 elements. Convert each character to a number which is used as an index.

• Create a a dictionary with characters as key and counter as values.

Page 133: Computation for Physics 計算物理概論 Python Programming for Physicists.

Dictionary as a Set of Counters

def histogram(s):d = dict()for c in s:

if c not in d:d[c] = 1 else:d[c] += 1

return d>>>h = histogram('brontosaurus')>>>print(h){'a':1, 'b':1, 'o':2, 'n':1, 's':2, 'r':2 , 'u':2, 't':1 }

Page 134: Computation for Physics 計算物理概論 Python Programming for Physicists.

Dictionaries and Listsdef invert_dict(d): inverse = dict() for key in d: val = d[key] if val not in inverse: inverse[val] = [key] else: inverse[val].append(key) return inverse>>> hist = histogram('parrot')>>> print hist{'a': 1, 'p': 1, 'r': 2, 't': 1, 'o': 1}>>> inverse = invert_dict(hist)>>> print inverse{1: ['a', 'p', 't', 'o'], 2: ['r']}

Page 135: Computation for Physics 計算物理概論 Python Programming for Physicists.

Fibonacci Function

• fibonacci(0)=0• fibonacci(1)=1• fibonacci(n)=ibonacci(n-1)+ibonacci(n-2)

def fibonacci (n): if n == 0: return 0 elif n == 1: return 1 else: return fibonacci(n-1) + fibonacci(n-2)

Page 136: Computation for Physics 計算物理概論 Python Programming for Physicists.

Keep Tracks of the Values

known = {0:0, 1:1}

def fibonacci(n): if n in known: return known[n]

res = fibonacci(n-1) + fibonacci(n-2) known[n] = res return res

Page 137: Computation for Physics 計算物理概論 Python Programming for Physicists.

Tuples

Page 138: Computation for Physics 計算物理概論 Python Programming for Physicists.

Tuples are Immutable

• A tuple is a sequence of values. The values can be any type, and they are indexed by integers, so in that respect tuples are a lot like lists. The important difference is that tuples are immutable.

Page 139: Computation for Physics 計算物理概論 Python Programming for Physicists.

tuple=a comma-separated list of values

>>>t = 'a', 'b', 'c', 'd', 'e'>>>t = ('a', 'b', 'c', 'd', 'e')

>>>t1 = 'a',>>>type(t1)<type 'tuple'>

>>>t2 = ('a')>>>type(t2)<type 'str'>

Page 140: Computation for Physics 計算物理概論 Python Programming for Physicists.

Built-in Function: tuple

>>>t = tuple()>>>print(t)()

>>>t = tuple('lupins')>>>print(t)('l', 'u', 'p', 'i', 'n', 's')

>>>t = tuple(['l','u','p','i','n',s'])>>>print(t)('l', 'u', 'p', 'i', 'n', 's')

Page 141: Computation for Physics 計算物理概論 Python Programming for Physicists.

Most List Operators also Work on Tuples

>>> t = ('a', 'b', 'c', 'd', 'e')>>> print t[0]'a'>>> print t[1:3]('b', 'c')

>>> t[0] = 'A'TypeError: object doesn't support item assignment

>>> t = ('A',) + t[1:]>>> print t('A', 'b', 'c', 'd', 'e')

Page 142: Computation for Physics 計算物理概論 Python Programming for Physicists.

Tuple Assignment

>>>a, b = b, a>>>a, b = 1, 2, 3ValueError: too many values to unpack

>>>addr = '[email protected]'>>>uname,domain = addr.split('@')>>>print(uname)monty>>>print(domain)python.org

Page 143: Computation for Physics 計算物理概論 Python Programming for Physicists.

Tuples ad Return Values

>>>t = divmod(7,3)>>>print(t)(2, 1)

>>>quot, rem = divmod(7,3)>>>print(quot)2>>>print(rem)1def min_max(t):

return min(t), max(t)

Page 144: Computation for Physics 計算物理概論 Python Programming for Physicists.

Variable-length argument tuples

• Functions can take a variable number of arguments• A parameter name that begins with * gathers

arguments into a tuple

def printall(*agr):print args

>>>printall(1, 2.0, '3')(1, 2.0, '3')

Page 145: Computation for Physics 計算物理概論 Python Programming for Physicists.

Scatter

• divmod takes exactly two arguments; it doesn't work with a tuple

• But you can scatter the tuple>>>t = (7,3)>>>divmod(t)TypeError: divmod expected 2 arguments, got 1>>>divmod(*t)(2, 1)

Page 146: Computation for Physics 計算物理概論 Python Programming for Physicists.

Lists and Tuples

• zip is a built-in function that takes two or more sequences and "zip" them into a list/iterator of tuples where each tuple contains one element from each sequence.

>>>s = 'abc'>>>t = [0,1,2]>>>zip(s, t)[('a',0),('b',1),('c',2)]>>>zip('Anne', 'Elk')[('A','E'),('n','l'),('n','k')]

Page 147: Computation for Physics 計算物理概論 Python Programming for Physicists.

Module

• from xxx import yyy• from xxx import *• The advantage of importing everything from the

module is that your code can be more concise.

• import xxx• The disadvantage is that there might be conflicts

between names defined in different modules, or between a name from a module and one of your variables.

Page 148: Computation for Physics 計算物理概論 Python Programming for Physicists.

Module

• import random– random.random()

• from random import seed– random()

• from random import *– random()

Page 149: Computation for Physics 計算物理概論 Python Programming for Physicists.

math

• Constants– pi– e

Page 150: Computation for Physics 計算物理概論 Python Programming for Physicists.

math

• Power and logarithmic functions• exp(x)• expml(x)• log(x[,base])• log1p(x)• log2(x)• log10(x)• pow(x,y)

Page 151: Computation for Physics 計算物理概論 Python Programming for Physicists.

random• seed(a=None, version=2)

– Initialize the random number generator.• getstate()

– Return an object capturing the current internal state of the generator.• setstate(state)

– Restores the internal state of the generator to what it was at the time getstate() was called.• random()

– Return the next random floating point number in the range [0.0, 1.0).• uniform(a,b)

– Return a random floating point number N such that a <= N <= b for a <= b and b <= N <= a for b < a.• randrange(stop)• randrange(start,stop[,step])

– Return a randomly selected element from range(start, stop, step). • randint(a,b)

– Return a random integer N such that a <= N <= b. Alias for randrange(a, b+1).• choice(seq)

– Return a random element from the non-empty sequence seq.• shuffle(x[,random])

– Shuffle the sequence x in place.• sample(polulation,k)

– Return a k length list of unique elements chosen from the population sequence or set.

Page 152: Computation for Physics 計算物理概論 Python Programming for Physicists.

Examples and Recipes>>>random()0.37444887175646646>>>uniform(1,10)1.1800146073117523>>>randrange(10)7>>>randrange(0,101,2)26>>>choice('abcdefghjk')'c'>>>items=[1,2,3,4,5,6,7]>>>shuffle(items)>>>items[7,3,2,5,6,4,1]>>>sample([1,2,3,4,5],3)[4,1,5]

Page 153: Computation for Physics 計算物理概論 Python Programming for Physicists.

Global Variables

Page 154: Computation for Physics 計算物理概論 Python Programming for Physicists.

Case Study: Data Structure Selection

Page 155: Computation for Physics 計算物理概論 Python Programming for Physicists.

Class and Objects

Page 156: Computation for Physics 計算物理概論 Python Programming for Physicists.

User-defined Types: Points

• Create a type called Point that represents a point in 2D space.

• In mathematical notation, points are written as (x,y)

Page 157: Computation for Physics 計算物理概論 Python Programming for Physicists.

Class

• A user-defined type is also called a class• Define a class named Point creates a class object>>>class Point(object):>>>"""Represents a pint in 2D space.""">>>print(Point)<class '__main__.Point'>>>>blank = Point()>>>print(blank)<__main__.Point object at 0x10c180890>

Page 158: Computation for Physics 計算物理概論 Python Programming for Physicists.

Class and Objects

• Creating a new object is called instantiation,• Object is an instance of the class.• In the example above– "blank" is an instance of the class "Point".

• When you print an instance Python tells you– What class it belongs.– Where it is stored in memory.

Page 159: Computation for Physics 計算物理概論 Python Programming for Physicists.

Attributes

• You can assign values to an instance using dot notation.

>>>blank.x = 3.0>>>blank.y = 4.0

• You can read the value of an attribute>>>print(blank.y)4.0>>>x=blank.x>>>print(blank.x)3.0

Page 160: Computation for Physics 計算物理概論 Python Programming for Physicists.

Attributes

• You can use dot notation as part of expression>>>print('(%g %g)' % (blank.x, blank.y)>>>(3.0, 4.0)

• You can pass an instance as an argument>>>def print_point(p):>>> print('(%g, %g)' % (p.x, p.y))>>>print_point(blank)(3.0, 4.0)

Page 161: Computation for Physics 計算物理概論 Python Programming for Physicists.

User-defined Types: Rectangles

• Create a type called Rectangle that represents a rectangle in 2D space.

• In mathematical notation, we need to specify – lower-left corner (as a Point)– width– height

Page 162: Computation for Physics 計算物理概論 Python Programming for Physicists.

Rectangles

>>>class Rectangle(object):>>> """Represents a rectangle.>>>>>> attributes: width, height, corner>>> """>>>box = Rectangle()>>>box.width = 100.0>>>box.height = 200.0>>>box.corner = Point()>>>box.corner.x = 0.0>>>box.corner.y = 0.0

Page 163: Computation for Physics 計算物理概論 Python Programming for Physicists.

Object Diagram

Point

width 100

corner

Rectanglebox

height 100

x 0.0y 0.0

Page 164: Computation for Physics 計算物理概論 Python Programming for Physicists.

Instances as Return Values

• find_center takes a Rectangle as an argument and returns a Point that contains the coordinates of the center of the Rectangle.

>>>def find_center(rect):>>> p = Point()>>> p.x = rect.corner.x + rect.width/2.0>>> p.y = rect.corner.y + rect.height/2.0>>> return p>>>center = find_center(box)>>>print_point(center)(50.0, 100.0)

Page 165: Computation for Physics 計算物理概論 Python Programming for Physicists.

Objects are Mutable

• You can change the state of an object by making an assignment to one of its attributes.

• You can write functions that modify objects.>>>def grow_rectangle(rect,dwidth,dheight):>>> rect.width += dwidth>>> rect.height += dheight>>>print(box.width,box.height)100>>>grow_rectangle(box,50,100)200>>>print(box.width,box.height)(150,300)

Page 166: Computation for Physics 計算物理概論 Python Programming for Physicists.

Copying

>>>p1 = Point()>>>p1.x = 3.0>>>p1.y = 4.0>>>pp1 = p1>>>print_point(p1)(3.0, 4.0)>>>print_point(pp1)(3.0, 4.0)>>>print( p1 is pp1 )True>>>print( p1 == pp1 )True

Page 167: Computation for Physics 計算物理概論 Python Programming for Physicists.

Object Diagram

p1

Point

x 3.0

y 4.0pp1

Page 168: Computation for Physics 計算物理概論 Python Programming for Physicists.

Copying

>>>import copy>>>p1 = Point()>>>p1.x = 3.0>>>p1.y = 4.0>>>p2 = copy.copy(p1)>>>print_point(p1)(3.0, 4.0)>>>print_point(pp1)(3.0, 4.0)>>>print( p1 is p2 )False>>>print( p1 == p2 )False

Page 169: Computation for Physics 計算物理概論 Python Programming for Physicists.

Object Diagram

p1

Point

x 3.0

y 4.0

p2Point

x 3.0

y 4.0

Copy

Page 170: Computation for Physics 計算物理概論 Python Programming for Physicists.

Copying

>>>box2 = copy.copy(box)>>>print(box2 is box)False>>>print(box2.corner is box.corner)True

Page 171: Computation for Physics 計算物理概論 Python Programming for Physicists.

Object Diagram

Point

width 100

corner

Rectanglebox

height 200

x 0.0y 0.0

width 100

corner

Rectanglebox2

height 200

Page 172: Computation for Physics 計算物理概論 Python Programming for Physicists.

>>>box3 = copy.deepcopy(box)>>>print(box3 is box)False>>>print(box3.corner is box.corner)False

Page 173: Computation for Physics 計算物理概論 Python Programming for Physicists.

Object Diagram

Point

width 100

corner

Rectanglebox

height 200

x 0.0y 0.0

width 100

corner

Rectanglebox3

height 200Point

x 0.0y 0.0

Page 174: Computation for Physics 計算物理概論 Python Programming for Physicists.

Debugging

• If you try to access an attribute that doesn't exist, you get an AttributeError:

>>>p = Point()>>>print(p.z)AttributeError: Point instance has no attribute 'z'

• If you are not sure what type an object is you can ask:>>>type(p)<type '__main__.Point>

• If you are not sure whether an object has a particular attribute, you can use the build-in function hasattr:

>>>hasattr(p, 'x')True>>>hasattr(p, 'z')False

Page 175: Computation for Physics 計算物理概論 Python Programming for Physicists.

Try

• Write a function called "distance_between_points" that takes two Points as arguments and return the distance between them.

• Write a function called "move_rectangle" that takes a Rectangle and two numbers named "dx" and "dy". It should change the location of the rectangle by adding dx to the x coordinate of corner and adding dy to the y coordinate of corner.

• Write a version of "move_rectangle" that creates and returns a new Rectangle instead of modifying the old one.

Page 176: Computation for Physics 計算物理概論 Python Programming for Physicists.

Class and Functions

• A class called Time that record the time of day>>>class Time(object):>>> """Represents the time of day.>>>>>> attributes: hour, minute, second>>> """>>>time = Time()>>>time.hour = 11>>>time.minute = 59>>>time.second = 30

Page 177: Computation for Physics 計算物理概論 Python Programming for Physicists.

print_time>>>def print_time(t):>>> print('%.2d:%.2d:%.2d' % (t.hour, t.minute, t.second))

Page 178: Computation for Physics 計算物理概論 Python Programming for Physicists.

Pure Functions

• Pure functions don't modify any of the objects passed to it as arguments.

• add_time>>>def add_time(t1, t2):>>> sum = Time()>>> sum.hour = t1.hour + t2.hour>>> sum.minute = t1.minute + t2.minute>>> sum.second = t1.second + t2.second>>> return sum>>>>>>

Page 179: Computation for Physics 計算物理概論 Python Programming for Physicists.

add_time

>>>start = Time()>>>start.hour = 9>>>start.minute = 45>>>start.second = 0>>>duration = Time()>>>duration.hour = 1>>>duration.minute = 35>>>duration.second = 0>>>done = add_time(start, duration)>>>print_time(done)10:80:00

Page 180: Computation for Physics 計算物理概論 Python Programming for Physicists.

add_time>>>def add_time(t1, t2):>>> sum = Time()>>> sum.hour = t1.hour + t2.hour>>> sum.minute = t1.minute + t2.minute>>> sum.second = t1.second + t2.second>>> return sum>>> # carry>>> if sum.second >= 60:>>> sum.second -= 60>>> sum.minute +=1>>> if sum.minute >= 60:>>> sum.minute -= 60>>> sum.hour +- 1>>> return sum

Page 181: Computation for Physics 計算物理概論 Python Programming for Physicists.

Modifiers

>>>def increment(time, seconds):>>> time.second += seconds>>> if time.second >= 60:>>> time.second -= 60>>> time.minute +=1>>> if time.minute >= 60:>>> time.minute -= 60>>> time.hour +- 1• What happens if the parameters seconds is much greater than sixty?

Page 182: Computation for Physics 計算物理概論 Python Programming for Physicists.

Time Object Integers

>>>def time_to_int(time):>>> minutes = time.hour * 60 + time.minutes>>> seconds = minutes * 60 + time.second>>> return seconds

>>>def int_to_time(seconds):>>> time = Time()>>> minute, time.second = divmod(second, 60)>>> time.hour, time.minute = divmod(minutes, 60)>>> return time

Page 183: Computation for Physics 計算物理概論 Python Programming for Physicists.

Try

• Write a boolean function called is_after that takes two Times objects t1 and t2, and return True if t1 follows t2 chronologically and False otherwise.– Try not to use 'if' statement!

• Write a correct version of increment that doesn't contain any loops.

• Write a pure version of increments that creates and returns a new Time object.

• Rewrite increment using time_to_int and int_to time

Page 184: Computation for Physics 計算物理概論 Python Programming for Physicists.

Try

• Write a function called mul_time that takes a Time object and a number and returns a new Time object that contains the product of the original Time and the number.

• Use mul_time to write a function that takes a Time object that represents the finishing time in a race, and a number that represents the distance, and return a Time object that represents the average pace (time per mile).

Page 185: Computation for Physics 計算物理概論 Python Programming for Physicists.

Class and Methods

• Object-oriented features– Programs are made up of object definitions and

function definitions, and most of the computation is expressed in terms of operations on objects.

– Each object definition corresponds to some object or concept in the real world, and the functions that operate on that object correspond to the ways real-world objects interact.

Page 186: Computation for Physics 計算物理概論 Python Programming for Physicists.

Methods

• A method is a function that is associated with a particular class. – We have seen methods for strings, lists, dictionaries and

tuples. – We will define methods for user-defined types.

• Methods are semantically the same as functions, but there are two syntactic differences:– Methods are defined inside a class definition in order to

make the relationship be- tween the class and the method explicit.

– The syntax for invoking a method is different from the syntax for calling a function.

Page 187: Computation for Physics 計算物理概論 Python Programming for Physicists.

print_time methodclass Time(object):

"""Represents the time of day.

attributes: hour, minute, second"""def print_time(t):

print('%.2d:%.2d:%.2d' % (t.hour, t.minute, t.second))

Page 188: Computation for Physics 計算物理概論 Python Programming for Physicists.

print_time method

• The first (less common) way is to use function syntax.• The second (more common) way is to use method

syntax.>>>start = Time()>>>start.hour = 9>>>start.minute = 45>>>start.second = 00>>>Time.print_time(start)09:45:00>>>start.print_time()09:45:00

Page 189: Computation for Physics 計算物理概論 Python Programming for Physicists.

method syntax : dot notation

• start.print_time()• In the use dot notation.– print_time is the name of the method– start is the object the method is invoked on, which

is called the subject.– Inside the method, the subject is assigned to the

first parameter.• By convention, the first parameters of a

method is called self

Page 190: Computation for Physics 計算物理概論 Python Programming for Physicists.

print_time method using selfclass Time(object):

"""Represents the time of day.

attributes: hour, minute, second"""def print_time(self):

print('%.2d:%.2d:%.2d' % (self.hour, self.minute, self.second))

Page 191: Computation for Physics 計算物理概論 Python Programming for Physicists.

increment method using selfclass Time(object):

"""Represents the time of day.

attributes: hour, minute, second"""def print_time(self):

print('%.2d:%.2d:%.2d' % (self.hour, self.minute, self.second))

def increment(self, seconds):seconds += self.time_to_int()return int_to_time(seconds)

>>>start.print_time()09:45:00>>>end = start.increment(1337)>>>end.print_time()10:07:17

Page 192: Computation for Physics 計算物理概論 Python Programming for Physicists.

Confusing Error Message>>>end=start.increment(1337,460)TypeError: increment() takes exactly 2 arguments (3 given)

• This is because the subject is also considered as an argument.

Page 193: Computation for Physics 計算物理概論 Python Programming for Physicists.

is_after method

• is_after method takes two Time objects as parameters. – name the first as self, name the second as other.

def is_after(self, other):return self.time_to_int() >

other.time_to_int()>>>end.is_after(start)True

Page 194: Computation for Physics 計算物理概論 Python Programming for Physicists.

The _ _init_ _ method

• The init method (short for initialization) is a special method that get invoked when an object is instantiated.

• Its full name is _ _ init_ _ (two underscores, follows by init, then two more underscores)

class Time(object):def __init__(self, hour=0, minute=0, second=0):

self.hour = hour self.minute = minute self.second = second

Page 195: Computation for Physics 計算物理概論 Python Programming for Physicists.

The _ _init_ _ method

>>>time=Time()>>>time.print_time()00:00:00>>>time=Time(9)>>>time.print_time()09:00:00>>>time=Time(9,45)>>>time.print_time()09:45:00

Page 196: Computation for Physics 計算物理概論 Python Programming for Physicists.

The _ _ str_ _ method

• The _ _str_ _ method is a special method that is supposed to return a string representation of an object.

class Time(object):def __str__(self):

return '%.2d:%.2d:%.2d' % (self.hour, self.minute, self.second)>>>time=Time(9,45)>>>print(time)09:45:00

Page 197: Computation for Physics 計算物理概論 Python Programming for Physicists.

Try

• Write an init method for the Point class that takes x and y as optional parameters and assign them to the corresponding attributes.

• Write a str method for the Point class. Create a Point object and print it.

Page 198: Computation for Physics 計算物理概論 Python Programming for Physicists.

Operator Overloadingdef __add__(self,other):

seconds = self.time_to_int()+other.time_to_int()

return int_to_time(seconds)>>>start = Time(9,45)>>>duration = Time(1,35)>>>print(start+duration)11:20:00

Page 199: Computation for Physics 計算物理概論 Python Programming for Physicists.

Type-based Dispatchdef __add__(self,other):

if isinstance(other,Time):return self.add_time(other)

else:return self.increment(other)

def add_time(self,other):seconds = self.time_to_int()+other.time_to_int()return int_to_time(seconds)

def increment(self,seconds):seconds += self.time_to_int()return int_to_time(seconds)

>>>start = Time(9,45)>>>duration = Time(1,35)>>>print(start+duration)11:20:00>>>print(start+1337)10:07:17

Page 200: Computation for Physics 計算物理概論 Python Programming for Physicists.

Type-based Dispatch>>>print(start+1337)10:07:17>>>print(1337+start)TypeError: unsupported operand type(s) for +: 'int' and 'instance'

Page 201: Computation for Physics 計算物理概論 Python Programming for Physicists.

Polymorphism

• Write functions that works correctly for arguments with different types.

def histogram(s): d = dict() for c in s: if c not in d: d[c] = 1 else: d[c] = d[c]+1 return d>>> t = ['spam', 'egg', 'spam', 'spam', 'bacon', 'spam']>>> histogram(t){'bacon': 1, 'egg': 1, 'spam': 4}

Page 202: Computation for Physics 計算物理概論 Python Programming for Physicists.

debugging

• Use hassttr• Use _ _dict_ _>>>p = Point(3,4)>>>print(p.__dict__){'y':4, 'x':3}

def print_attributes(obj):for attr in obj.__dict__:

print(attr,getattr(obj.attr))

Page 203: Computation for Physics 計算物理概論 Python Programming for Physicists.

Try

• Write an add method for the Point class.• Write an add method for Points that works

with either a Point object or a tuple:– If the second operand is a Point, the method

should return a new Point whose x coordiante is the sum of the x coordinates of the operands, and likewise for the y coordinates.

– If the second operand is a tuple, the method should add the first element of the tuple to the x coordinate and the second element to the y coordinate, and return a new Point with the result.

Page 204: Computation for Physics 計算物理概論 Python Programming for Physicists.

Inheritance

Page 205: Computation for Physics 計算物理概論 Python Programming for Physicists.

Card Objects

• A deck• Suits– Spade ♠– Hearts ♥– Diamonds ♦– Clubs ♣

• Ranks– Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King

Page 206: Computation for Physics 計算物理概論 Python Programming for Physicists.

Use Integers to Encode

• Suits– Spade 3– Hearts 2– Diamonds 1– Clubs 0

• Ranks– Ace 1,– 2, 3, 4, 5, 6, 7, 8, 9, 10, – Jack 11, – Queen 12, – King 13

Page 207: Computation for Physics 計算物理概論 Python Programming for Physicists.

Class Definition for Card

class Card(object): """Represents a standard playing card."""

def __init__(self, suit=0, rank=2): self.suit = suit self.rank = rank

Create a card with specific suit and rank>>>queen_of_diamonds = Card(1,12)

Page 208: Computation for Physics 計算物理概論 Python Programming for Physicists.

Class Attributesclass Card(object): """Represents a standard playing card."""

# inside class Card: suit_names = ['Clubs', 'Diamonds', 'Hearts', 'Spades'] rank_names = [None, 'Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King']

def __init__(self, suit=0, rank=2): self.suit = suit self.rank = rank

def __str__(self): return '%s of %s' % (Card.rank_names[self.rank], Card.suit_names[self.suit])>>>card1 = Card(2,11)>>>print(card1)Jack of Hearts# Each card has its own suit and rank, but there is only one copy of suit_names and rank_names.

Page 209: Computation for Physics 計算物理概論 Python Programming for Physicists.

Comparing Cards

• Relational operators such as <,>,==, etc depend on the comparison of values.

• Override the behavior of comparison by __cmp__.

• __cmp__ take two parameters, self and other– return a positive number if self > other– return a negative number if self < other– return 0 if self == other

• We choose to make suit more important.– Any Spades > any Diamonds

Page 210: Computation for Physics 計算物理概論 Python Programming for Physicists.

Comparing Cards# inside class Card: def __cmp__(self, other): # check the suits if self.suit > other.suit: return 1 if self.suit < other.suit: return -1

# suits are the same... check ranks if self.rank > other.rank: return 1 if self.rank < other.rank: return -1

# ranks are the same... it's a tie return 0

# inside class Card: def __cmp__(self, other): t1 = self.suit, self.rank t2 = other.suit, other.rank return cmp(t1, t2)

Page 211: Computation for Physics 計算物理概論 Python Programming for Physicists.

Deck

• A deck is made up of cards.• Each deck contain a list of cards as an attribute.

class Deck(object):

def __init__(self): self.cards = [] for suit in range(4): for rank in range(1, 14): card = Card(suit, rank) self.cards.append(card)

Page 212: Computation for Physics 計算物理概論 Python Programming for Physicists.

Printing the Deck#inside class Deck:

def __str__(self): res = [] for card in self.cards: res.append(str(card)) return '\n'.join(res)

• This method demonstrates an efficient way to accumulate a large string: – building a list of strings and then using join.

• The built-in function str invokes the __str__ method on each card and returns the string representation.

Page 213: Computation for Physics 計算物理概論 Python Programming for Physicists.

str.join

str.join(iterable)

Return a string which is the concatenation of the strings in the iterable iterable. The separator between elements is the string providing this method.

>>>res=['a','b','c']>>>print('\n'.join(res))abc

Page 214: Computation for Physics 計算物理概論 Python Programming for Physicists.

Printing the Deck

>>> deck = Deck()>>> print deckAce of Clubs2 of Clubs3 of Clubs...10 of SpadesJack of SpadesQueen of SpadesKing of Spades

Page 215: Computation for Physics 計算物理概論 Python Programming for Physicists.

Add, Remove, Shuffle and Sort

import random#inside class Deck:

def pop_card(self): return self.cards.pop() def add_card(self, card): self.cards.append(card) def shuffle(self): random.shuffle(self.cards)

Page 216: Computation for Physics 計算物理概論 Python Programming for Physicists.

Try

• Write a Deck method named sort that uses the list method sort to sort the cards in a Deck.

• sort uses __cmp__ method we defined to determine sort order.

Page 217: Computation for Physics 計算物理概論 Python Programming for Physicists.

sort in Python

# sorted function>>> sorted([5, 2, 3, 1, 4])[1, 2, 3, 4, 5]

# list sort method>>>a=[5,2,3,1,4]>>>a.sort()>>>a[1, 2, 3, 4, 5]

Page 218: Computation for Physics 計算物理概論 Python Programming for Physicists.

Inheritance

• Inheritance is the ability to define a new class that is a modified version of an existing class.

• The new class inherits the methods of the existing class. – The existing class is called the parent.– The new class is called the child.

Page 219: Computation for Physics 計算物理概論 Python Programming for Physicists.

Hand

• Hand: the set of cards held by one player.• A hand is similar to a deck:– Both are made up of a set of cards.– Both require operations like adding & removing

• A hand is also different from a deck:– Some operations are only for a hand.– Comparing two hands.

Page 220: Computation for Physics 計算物理概論 Python Programming for Physicists.

Child Class

class Hand(Deck): """Represents a hand of playing cards."""# Hand also inherits __init__ from Deck, but the init method for Hands should initialize cards with an empty list.

def __init__(self, label=''): self.cards = [] self.label = label>>>hand=Hand('new hand')>>>print(hand.cards)[]>>>print(hand.label)new hand

Page 221: Computation for Physics 計算物理概論 Python Programming for Physicists.

Inherited Methods

>>> deck = Deck()>>> card = deck.pop_card()>>> hand.add_card(card)>>> print handKing of Spades

Page 222: Computation for Physics 計算物理概論 Python Programming for Physicists.

Move Cards

#inside class Deck:

def move_cards(self, hand, num): for i in range(num): hand.add_card(self.pop_card())

Page 223: Computation for Physics 計算物理概論 Python Programming for Physicists.

Try

• Write a Deck method called deal_hands that – Takes two parameters• the number of hands• the number of cards per hand,

– Creates new Hand objects, deals the appropriate number of cards per hand, and returns a list of Hand objects.

Page 224: Computation for Physics 計算物理概論 Python Programming for Physicists.

Possible Hands in Poker

• pair: two cards with the same rank• two pair: two pairs of cards with the same rank• three of a kind: three cards with the same rank• straight: five cards with ranks in sequence (aces can be high

or low, so Ace-2-3-4-5 is a straight and so is 10-Jack-Queen-King-Ace, but Queen-King-Ace-2-3 is not.

• flush: five cards with the same suit• full house: three cards with one rank, two cards with another• four of a kind: four cards with the same rank• straight flush: five cards in sequence (as defined above) and

with the same suit

Page 225: Computation for Physics 計算物理概論 Python Programming for Physicists.

Try

• Implement Card class• Implement Deck class• Implement Hand class• Implement Hand method for– has_pair– has_two_pair– has_flush

• Write a main program that create a deck, shuffle it, deal 7-cards hands see if you got pair, two_pair, flush. Repeat the process to estimate the probability.

Page 226: Computation for Physics 計算物理概論 Python Programming for Physicists.

PokerHand Classclass PokerHand(Hand):

def suit_hist(self): """Builds a histogram of the suits that appear in the hand. Stores the result in attribute suits. """ self.suits = {} for card in self.cards: self.suits[card.suit] = self.suits.get(card.suit, 0) + 1

def has_flush(self): """Returns True if the hand has a flush, False otherwise. Note that this works correctly for hands with more than 5 cards. """ self.suit_hist() for val in self.suits.values(): if val >= 5: return True return False

Page 227: Computation for Physics 計算物理概論 Python Programming for Physicists.

This code deals seven 7-cards poker hands and check to see if any of them contains a flush.# make a deck deck = Deck() deck.shuffle()

# deal the cards and classify the hands for i in range(7): hand = PokerHand() deck.move_cards(hand, 7) hand.sort() print hand print hand.has_flush() print ''

Page 228: Computation for Physics 計算物理概論 Python Programming for Physicists.

Try: Add methods to PokerHand

• Add methods to PokerHand class– has_pair, has_twopair, etc– Return True of False– Your code should work correctly for "hands" that contain any

number of cards.• Add a method– classify– Figures out the highest-value classification for a hand and sets

the label attribute accordingly.• Write a function that shuffles a deck of cards, divides it

into hands, classify the hands, and counts and number of times various classifications appears. (each hand has 5 cards)

Page 229: Computation for Physics 計算物理概論 Python Programming for Physicists.

pass Statements

• The pass statement does nothing. It can be used when a statement is required syntactically but the program requires no action.

>>> while True:... pass # Busy-wait for keyboard interrupt (Ctrl+C)...>>> class MyEmptyClass:... pass...>>> def initlog(*args):... pass # Remember to implement this!...

Page 230: Computation for Physics 計算物理概論 Python Programming for Physicists.