Download Python for Computations - Python basics 2

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Python for Computations
Python basics 2
Krzysztof Gdawiec
Institute of Computer Science
University of Silesia
The dictionary is a mutable type. Each element in the dictionary
consists from a key and value.
To create a dictionary we place the key-value pairs between the
{ }, e.g.,
1
2
3
4
d1
d2
d3
d4
=
=
=
=
{"aa" : 1, "b" : 2.0, "cc" : 1}
{1 : 10, 2 : −10, 3 : 10}
{10 : [1, 2], 11 : (2, 3), 12 : 3}
{}
If we want to read the value with the given key, then between the
square brackets we give the key, e.g., d1["aa"].
If we want to alter the value with the given key, then we use the
assignment operator, e.g., d1["aa"] = 10.
When we want to add a key-value pair that does not exists in the
dictionary we simply use the following syntax: d4[key] = value.
From any dictionary we can read:
d4.keys(), d4.values().
We can also check if a given key exists in the dictionary
d4.has_key("b").
Defining own functions in Python is very simple. We can make this
in the following way:
1
2
3
def function_name( args ) :
"""documenting string"""
instructions
The arguments are separated by commas and they are passed to
the function by reference.
We can give a default value to each of the arguments:
1
def foo( arg1, arg2, arg3 = val1, arg4 = val2 ):
The arguments with the default values must be in the end of the
arguments list.
To return a value we use the return command. We can return
several values at the same time. For this purpose we can use
comma separated values, e.g., return l1, d2, 3, or tuple, e.g.,
return (l1, d2, 3).
When we write own function sometimes we require that the
arguments are of some fixed type. We can check if the value
passed to the function is of a desired type in the following way:
1
if type( v ) == type( 1.0 ) :
In Python, similar to other programming languages, variables that
are defined outside of all functions are global variables.
We have access to global variable in every function. We can read
its value, but when we want to alter its value then we need to add
at the beginning of the function the global word followed by the
name of the variable, e.g.,
1
counter = 0
2
3
4
def foo() :
global counter
5
6
counter += 1
Some functions used in Python need to pass other functions as an
argument. If the function that we want to pass is short we can use
anonymous functions, which in Python are called lambda functions.
To define a lambda function after the lambda keyword we give the
list of arguments (separated by commas), then we give a colon and
finally the body of the function, e.g.,
1
lambda x, y: x + y
We can assign lambda function to a variable, and then use it as a
normal function, e.g.,
1
2
sum = lambda x, y: x + y
sum( 1, 2 )
In our classes we will be needing reading/writing from/to textfiles.
In both cases first we need to open a file, then operate on the file
and finally close the file.
To open a file we use the following command:
1
The
f = file( path, access )
is a path to the file which we want to open and the
access is information how do we want to open the file: "w" for
writing to file, "r" for reading from file.
path
To close the file we use the following command:
1
del f
To write some text into the file
ways:
1
f.write(text)
2
# read only one
line
l = f.readline()
we can use one of the following
1
To read some text from the file
1
f
1
2
f
print >> f, text
we have several possibilities:
# read all the
lines
l = f.readlines()
1
2
# read the whole
file
l = f.read()
A very useful library for matrices, arrays and numerical
computations is NumPy.
1
import numpy
2
3
m = numpy.array( [[1, 2, 3], [4, 5, 6]] )
4
5
6
7
print m[0, 1] # gives 2
print m[1, :] # gives 2nd row
print m[:, 1] # gives 2nd column
Functions from NumPy, unlike the standard mathematical
functions from Python, can operate on arguments that are lists
and not only single numbers.
We can also generate lists of evenly spaced numbers using the
linspace function:
1
numpy.linspace( start, stop, num = 50 )