/usr/share/python-ase/doc/python.rst is in python-ase-doc 3.9.1.4567-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | .. _python_info:
---------------
What is Python?
---------------
This section will give a very brief introduction to the Python
language.
.. seealso::
* The Python_ home page.
* Python Recipes_.
* Try a `Python quick reference card`_ or a `different reference card`_.
.. _Recipes: http://code.activestate.com/recipes/langs/python
.. _Python quick reference card: http://www.limsi.fr/Individu/pointal/python/pqrc
.. _different reference card: http://rgruet.free.fr/
.. _Python: http://www.python.org
Executing Python code
---------------------
You can execute Python code interactively by starting the interpreter
like this::
$ python
Python 2.5.1 (r251:54863, Mar 7 2008, 04:10:12)
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print 'hello'
hello
You can also put the ``print 'hello'`` line in a file (``hello.py``)
and execute it as a Python script::
$ python hello.py
hello
Or like this::
$ python -i hello.py
hello
>>> print 'hi!'
hi!
Finally, you can put ``#!/usr/bin/env python`` in the first line of
the ``hello.py`` file, make it executable (``chmod +x hello.py``) and
execute it like any other executable.
.. tip::
For interactive Python sessions, it is very convenient to have a
personal ``.pythonrc`` file::
import rlcompleter
import readline
readline.parse_and_bind("tab: complete")
from ase import *
and point the :envvar:`PYTHONSTARTUP` environment variable at it (see
rlcompleter_ for details).
.. _rlcompleter: https://docs.python.org/2/library/rlcompleter.html
.. tip::
For an even better interactive experience, use ipython_.
.. _ipython: http://ipython.scipy.org
Types
-----
Python has the following predefined types:
=========== ===================== ==========================
type description example
=========== ===================== ==========================
``bool`` boolean ``False``
``int`` integer ``117``
``float`` floating point number ``1.78``
``complex`` complex number ``0.5 + 2.0j``
``str`` string ``'abc'``
``tuple`` tuple ``(1, 'hmm', 2.0)``
``list`` list ``[1, 'hmm', 2.0]``
``dict`` dictionary ``{'a': 7.0, 23: True}``
``file`` file ``open('stuff.dat', 'w')``
=========== ===================== ==========================
A ``dict`` object is mapping from keys to values:
>>> d = {'s': 0, 'p': 1}
>>> d['d'] = 2
>>> d
{'p': 1, 's': 0, 'd': 2}
>>> d['p']
1
In this example all keys are strings and all values are integers.
Types can be freely mixed in the same dictionary; any type can be used
as a value and most types can be used as keys (mutable objects cannot
be keys).
A ``list`` object is an ordered collection of arbitrary objects:
>>> l = [1, ('gg', 7), 'hmm']
>>> l[1]
('gg', 7)
>>>
>>> l
[1, ('gg', 7), 'hmm', 1.2]
>>> l[-2]
'hmm'
Indexing a list with negative numbers counts from the end of the list,
so element -2 is the second last.
A ``tuple`` behaves like a ``list`` - except that it can't be modified
in place. Objects of types ``list`` and ``dict`` are *mutable* - all
the other types listed in the table are *immutable*, which means that
once an object has been created, it can not change. Tuples can
therefore be used as dictionary keys, lists cannot.
.. note::
List and dictionary objects *can* change. Variables in
Python are references to objects - think of the = operator as a
"naming operator", *not* as an assignment operator. This is demonstrated here:
>>> a = ['q', 'w']
>>> b = a
>>> a.append('e')
>>> a
['q', 'w', 'e']
>>> b
['q', 'w', 'e']
The line b = a gives a new name to the array, and both names now
refer to the same list.
However, often a new object is created and
named at the same time, in this example the number 42 is *not*
modified, a new number 47 is created and given the name ``d``. And
later, ``e`` is a name for the number 47, but then a *new*
number 48 is created, and ``e`` now refers to that number:
>>> c = 42
>>> d = c + 5
>>> c
42
>>> d
47
>>> e = d
>>> e += 1
>>> (d, e)
(47, 48)
.. note::
Another very important type is the :term:`ndarray` type described
here: :ref:`numpy`. It is an array type for efficient numerics,
and is heavily used in ASE.
Loops
-----
A loop in Python can be done like this:
>>> things = ['a', 7]
>>> for x in things:
... print x
...
a
7
The ``things`` object could be any sequence. Strings, tuples, lists,
dictionaries, ndarrays and files are sequences. Try looping over some
of these types.
Often you need to loop over a range of numbers:
>>> for i in range(5):
... print i, i*i
...
0 0
1 1
2 4
3 9
4 16
Functions and classes
---------------------
A function is defined like this:
>>> def f(x, m=2, n=1):
... y = x + n
... return y**m
...
>>> f(5)
36
>>> f(5, n=8)
169
Here ``f`` is a function, ``x`` is an argument, ``m`` and ``n`` are keywords with default values ``2`` and ``1`` and ``y`` is a variable.
A :term:`class` is defined like this:
>>> class A:
... def __init__(self, b):
... self.c = b
... def m(self, x):
... return self.c * x
... def get_c(self):
... return self.c
You can think of a class as a template for creating user defined
objects. The ``__init__()`` function is called a :term:`constructor`,
it is being called when objects of this type are being created.
In the class ``A`` ``__init__`` is a constructor, ``c`` is an
attribute and ``m`` and ``get_c`` are methods.
>>> a = A(7)
>>> a.c
7
>>> a.get_c()
7
>>> a.m(3)
21
Here we make an :term:`instance`/object ``a`` of type ``A``.
Importing modules
-----------------
If you put the definitions of the function ``f`` and the class ``C``
in a file ``stuff.py``, then you can use that code from another piece
of code::
from stuff import f, C
print f(1, 2)
print C(1).m(2)
or::
import stuff
print stuff.f(1, 2)
print stuff.C(1).m(2)
or::
import stuff as st
print st.f(1, 2)
print st.C(1).m(2)
Python will look for ``stuff.py`` in these directories:
1) current working directory
2) directories listed in your :envvar:`PYTHONPATH`
3) Python's own system directory (typically :file:`/usr/lib/python2.5`)
and import the first one found.
|