Python
Python
Python
Outline
Why Use Python?
Running Python
Types and Operators Basic Statements Functions
Runninglanguage, PythonPython (1) is also an In addition to being a programming interpreter. The interpreter reads other Python programs and commands, and executes them. Note that Python programs are compiled automatically before being scanned into the interpreter. The fact that this process is hidden makes Python faster than a pure interpreter.
How to call up a Python interpreter will vary a bit depending on your platform, but in a system with a terminal interface, all you need to do is type python (without the quotation marks) into your command line. Example: # From here on, the $ sign denotes the start of a terminal command line, and the # sign denotes a comment. Note: the # sign denotes a comment in Python. Python ignores anything written to the right of a # sign on a given line $ python # Type python into your terminal's command line >>> # After a short message, the >>> symbol will appear. This signals # the start of a Python interpreter's command line
Strings are ordered blocks of text Strings are enclosed in single or double quotation marks Double quotation marks allow the user to extend strings over multiple lines without backslashes, which usually signal the continuation of an expression Examples: 'abc', ABC Concatenation and repetition Strings are concatenated with the + sign: >>> 'abc'+'def' 'abcdef' Strings are repeated with the * sign: >>> 'abc'*3 'abcabcabc'
Types and Operators: Mutable vs. Immutable Types (2) Immutable and mutable types handle name assignments differently
>>> a = 2 >>> b = a # a and b are both numbers, and are thus immutable >>> a = 3 >>> b 2 Even though we set b equal to a, changing the value of a does not change the value of b. However, for mutable types, this property does not hold. >>> La = [0,1,2] >>> Lb = La # La and Lb are both lists, and are thus mutable >>> La = [1,2,3] >>> Lb [1,2,3] Setting Lb equal to La means that changing the value of La changes that of Lb. To circumvent this property, we would make use of the function copy.copy(). >>> La = [0,1,2] >>> Lb = copy.copy(La) Now, changing the value of La will not change the value of Lb.
Functions (1)
Usually, function definitions have the following basic structure: def func(args): return values Regardless of the arguments, (including the case of no arguments) a function call must end with parentheses. Functions may be simple one-to-one mappings >>> def f1(x): ... return x*(x-1) ... >>> f1(3) 6 They may contain multiple input and/or output variables >>> def f2(x,y): ... return x+y,x-y ... >>> f2(3,2) (5,1)
Functions (2)
Functions don't need to contain arguments at all: >>> def f3(): ... print 'Hello world' ... >>> f3() Hello world The user can set arguments to default values in function definitions: >>> def f4(x,a=1): ... return a*x**2 ... >>> If this function is called with only one argument, the default value of 1 is assumed for the second argument >>> f4(2) 4 However, the user is free to change the second argument from its default value >>> f4(2,a=2) # f4(2,2) would also work 8
Functions (3)
Functions need not take just numbers as arguments, nor output just numbers or tuples. Rather, they can take multiple types as inputs and/or outputs. Examples: >>> arr = arange(4) >>> f4(arr,a=2) # using the same f4 as on the previous slide [0,2,8,18,] >>> def f5(func, list, x): ... L = [] ... for i in range(len(list)): ... L.append(func(x+list[i])) ... arr = array(L) ... return L,arr ... >>> L1 = [0.0,0.1,0.2,0.3] >>> L,arr = f5(exp,L1,0.5) >>> arr [ 1.64872127, 1.8221188 , 2.01375271, 2.22554093,] Note: the function above requires Numeric, NumPy, or a similar package
Functions (4)
Anything calculated inside a function but not specified as an output quantity (either with return or global) will be deleted once the function stops running >>> def f5(x,y): ... a = x+y ... b = x-y ... return a**2,b**2 ... >>> f5(3,2) (25,1) If we try to call a or b, we get an error message: >>> a Traceback (most recent call last): File "<stdin>", line 1, in ? NameError: name 'a' is not defined This brings us to scoping issues, which will be addressed in the next section.
Functions: Getting Help library utilities can If you forget how to use a standard function, Python's help. Say we want to know how to use the function execfile(). In this case, Python's help() library functions is extremely relevant. Usage: >>> help(execfile) # don't include the parentheses when using the function name as an argument Entering the above into the interpreter will call up an explanation of the function, its usage, and the meanings of its arguments and outputs. The interpreter will disappear and the documentation will take up the entire terminal. If the documentation takes up more space than the terminal offers, you can scroll through the documentation with the up and down arrow keys. Striking the q key will quit the documentation and return to the interpreter. WARP has a similar library function called doc(). It is used as follows: >>> from warp import * >>> doc(execfile) The main difference between help() and doc() is that doc() prints the relevant documentation onto the interpreter screen.