Differences between revisions 3 and 6 (spanning 3 versions)
Revision 3 as of 2008-04-13 11:38:40
Size: 520
Editor: anonymous
Comment:
Revision 6 as of 2008-04-13 17:13:34
Size: 2615
Editor: anonymous
Comment:
Deletions are marked like this. Additions are marked like this.
Line 7: Line 7:
= Variable names =
Allowed names for identifier (name of variable or function):
 * Any sequence of alphabetical character, number, or underline character {{{_}}}
 * First symbol has to be a character
 * Case sensitive
 * Not a python built-in keyword
Line 8: Line 14:
== Name space ==

The Python interpreter maps the name of an identifier in the active name space. The active name space depends on the active code block (module, function). The identifier can be local or global with respect to the active code block.

{{{#!python
def bad_style(x):
 # Name space of bad_style
 global y
 y=x
 z=x
 return x

# Name space of __main__
x,y,z=2,2,2
x=bad_style(1)
print 'x=',x,'y=',y,'z=',z
# y is changed!
}}}

{{{#!python
import scipy
scipy.pi # pi is in the name space of scipy

from scipy import pi
pi # pi is in the active name space

from scipy import *
pi # all identifiers of scipy are in the active name space

del(pi) # Deletes pi from the active name space
reset # ipython function to delete all identifiers
}}}
Line 10: Line 48:
== Scalar/Number == == Scalar ==
Line 18: Line 56:
=== Casting ===

 * int()
 * long()
 * float()
 * complex()
 * str()
Line 21: Line 67:
{{{#!python
s1='Hello world'
s2="Hello world"
s1==s2
Line 22: Line 72:
=== Tuple ===

=== List ===
s='"'
s="'"
}}}
Line 27: Line 76:
{{{#!python
D={1:'one',2:'two','one':1,'two':2}
D['one']
D[1]
}}}
Line 32: Line 86:
= Copy and reference = NumPy/SciPy provides a multidimensional '''array''' data type. An array can hold arbitrary Python objects but usually they are used for N-dimensional numeric data types.

=== Array creation ===

 * {{{empty((d1,d2),dtype)}}} returns uninitialized array of shape d1,d2.
 * {{{zeros((d1,d2),dtype)}}} returns array of shape d1,d2 filled with zeros
 * {{{ones((d1,d2),dtype)}}} returns array of shape d1,d2 filled with ones
 * {{{array(object,dtype)}}} returns an array from an object, e.g. a list
 * {{{dtype}}} fundamental C data type e.g. uint8, int16, int64, float32, float64

=== Array indexing ===

 * {{{A[y,x]}}} returns (y,x) element of the array
 * {{{A[:,x]}}} returns all elements of the y-dimension at x
 * {{{A[y1:y2,x]}}}
 * {{{A[:,:]}}} returns a copy of two dimensional array A
 * {{{A[:,:,0]}}} returns the first sub-image of a 3-dimensional array

  
 

Variable names

Allowed names for identifier (name of variable or function):

  • Any sequence of alphabetical character, number, or underline character _

  • First symbol has to be a character
  • Case sensitive
  • Not a python built-in keyword

Name space

The Python interpreter maps the name of an identifier in the active name space. The active name space depends on the active code block (module, function). The identifier can be local or global with respect to the active code block.

   1 def bad_style(x):
   2         # Name space of bad_style
   3         global y
   4         y=x 
   5         z=x
   6         return x
   7 
   8 # Name space of __main__
   9 x,y,z=2,2,2
  10 x=bad_style(1)
  11 print 'x=',x,'y=',y,'z=',z
  12 # y is changed!

   1 import scipy
   2 scipy.pi # pi is in the name space of scipy
   3 
   4 from scipy import pi
   5 pi # pi is in the active name space
   6 
   7 from scipy import *
   8 pi # all identifiers of scipy are in the active name space
   9 
  10 del(pi) # Deletes pi from the active name space
  11 reset   # ipython function to delete all identifiers

Built-in

Scalar

  • Plain integer: 9

  • Long integer: 9**99, 1L

  • Hex integer: 0x10

  • Floating point: 0.1

  • Exponential floating point: 1e-3

  • Complex: 3+2j

Casting

  • int()
  • long()
  • float()
  • complex()
  • str()

Sequences

String

   1 s1='Hello world'
   2 s2="Hello world"
   3 s1==s2
   4 
   5 s='"'
   6 s="'"

Dictionary

   1 D={1:'one',2:'two','one':1,'two':2}
   2 D['one']
   3 D[1]

Extended data types

Array

NumPy/SciPy provides a multidimensional array data type. An array can hold arbitrary Python objects but usually they are used for N-dimensional numeric data types.

Array creation

  • empty((d1,d2),dtype) returns uninitialized array of shape d1,d2.

  • zeros((d1,d2),dtype) returns array of shape d1,d2 filled with zeros

  • ones((d1,d2),dtype) returns array of shape d1,d2 filled with ones

  • array(object,dtype) returns an array from an object, e.g. a list

  • dtype fundamental C data type e.g. uint8, int16, int64, float32, float64

Array indexing

  • A[y,x] returns (y,x) element of the array

  • A[:,x] returns all elements of the y-dimension at x

  • A[y1:y2,x]

  • A[:,:] returns a copy of two dimensional array A

  • A[:,:,0] returns the first sub-image of a 3-dimensional array

LehreWiki: SiaProgrammingPythonDatatypes (last edited 2008-04-13 17:13:34 by anonymous)