Вы находитесь на странице: 1из 5

Introductory Topics

Control structure
Selection if ...: ... elif ...: ... else: ... Loops while ...: ... or for ... in ...: ... Control in loops can be modied using break and/or continue.

Variables and Data


Boolean: True and False None Numbers: int, long, oat, and complex Lists: Lists can be created explicitly using brackets or from the range function. Useful operations that mutate lists are: del mylist[i] list.reverse(mylist) list.sort(mylist) map(float, mylist) i = list.index(mylist, Ruijun) Ruijun in mylist

Tuple: A tuple is like a list except that it is immutable and constructed with parentheses, which can be omitted when there is no ambiguity. a, b, c = range(3) Dictionary: A dictionary is a sequence in which entries are accessed by a key rather than a nonnegative integer index. sqrt = {1:1, 4:2, 9:3} # a dictionary String: Strings that cross lines should be enclosed in triple quotes. This is a string! "This is a string!" """ This string crosses a line. """ This string also crosses a line. Useful constructions include string[1:2] str.upper(mystring) Attributes: Modules are objects and they may have data attributes, eg sys.argv math.pi and function attributes sys.exit math.sin There are more. Copy and assignment: A Python variable is a reference to an object. To be more specic, a Python variable contains the address of an object in memory. x = 2; y = x; x = 3 print x, y

import copy a = [1.0, 2.0, 3.0] b = copy.copy(a) c = copy.deepcopy(a) Each object x has a unique ID id(x), typically its address in memory. Whether or not variable x and y refer to the same object can be checked with x is y

Functions
A typical Python function can be sketched as def funciton_name(arg1, arg2, arg3): # statements return something All Python functions return a value, default is None. Call by reference def zero(x, y): x = 0. for i in range(len(b)): y[i] = 0. a = 1. b = [2., 3., 5.] zeros(a, b) print a = , a, b = , b, x = , x When the function zero is called, the reference in a and b are copied to local variables x and y, respectively. The function changes x to refer to 0, but there is no change to a. However, b and y are referencing the same list. Upon exist, the local variables become undened,. Scope: The parameter of a function have only local scope. def func(x): a = 1. return x + a + b a = 2. b = 3. func(5.) 3

The variable a is a local variable because it is dened in its rst occurrence, where b is a global variable because it is used without rst being dened. Argument lists: Python permits keyword arguments, which are optional labeled arguments. e.g. plot(x, y, linewidth=1.0) Passing function names: A couple of examples are myplot(f, -1., 1.) myplot(lambda x: x*x, 0., 5.) The lambda operator constructs a function without naming it. It is limited to functions that can be dened with a single expression.

Array
Creating Arrays Array of specied length, lled with zeros: from numpy import zeros # one dimensional array a = zeros(n, float) # two dimensional array b = zeros((p, q), float) Array with a sequence of numbers: from numpy import arange x = arange(-5, 5, 0.5, float) Array construction from a Python list pl = [0, 1.2, 4, -9.1, 5, 8] # Python list c = array(pl) Changing the dimensions of an array: a = array([0, 1.2, 4, -9.1, 5, 8]) a.shape = (2, 3) # turn a into a 2x3 matrix a. shape = (size(a),) # turn a into a vector of length 6 again Array Indexing: A general index has the form start:stop:step, indicating all elements from start up to stop-step in steps of step.

a = arange(-1, 1.01, 0.4) a[2: 4] = -1 # set a[2] and a[3] equal to -1 a[-1] = a[0] # set last element equal to first one a.shape = (2, 3) # turn a into a 2x3 matrix print a[0, 1] # print entry (0, 1) i = 0; j = 0 a[i, j] = 10 # assignment to entry (i, j) print a[:, 0] # print first column a[:, :] = 0 # set all elements of a equal to 0 Array Computations: Check the document list http://www.scipy.org/Numpy Example List ceil(a): nearest integers greater than or equal to a oor(a): nearest integers smaller than or equal to a x(a): round a to nearest integer towards zero round(decimals = 0, out = None) from numpy import * # rounds the items. array([1.2345, -1.647]).round() # integer arrays stay as they are array([1, -1]).round() # round to 1 decimal place array([1.2345, -1.647]).round(decimals=1) # both real and complex parts are rounded array([1.2345+2.34j, -1.647-0.238j]).round() # numpy rounds x.5 to nearest even array([0.0, 0.5, 1.0, 1.5, 2.0, 2.5]).round() # different output arrays may be specified a = zeros(3, dtype=int) array([1.2345, -1.647, 3.141]).round(out=a)

Вам также может понравиться