
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "examples_generated/python/01_whatispython.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_examples_generated_python_01_whatispython.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_examples_generated_python_01_whatispython.py:

.. _pythonintroduction:

What is Python?
===============

This section will give a very brief introduction to the Python language.

.. tip::

   See also the `Python home page <https://www.python.org/>`_ for further
   information.

Executing Python code
=====================
You can execute Python code interactively by starting the interpreter like with
the command ``python3`` and test it with any python command such as:

.. GENERATED FROM PYTHON SOURCE LINES 21-24

.. code-block:: Python

    print('hello')






.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    hello




.. GENERATED FROM PYTHON SOURCE LINES 25-75

You can also put the ``print("hello")`` line in a file (``hello.py``) and
execute it as a Python script with ``python3 hello.py`` or ``python3 -i
hello.py`` to enter interactive mode after the file is executed.

Finally, you can put ``#!/usr/bin/env python3`` 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 a better interactive experience, consider ipython.

Types
=====

.. list-table:: Supported Python Types
   :header-rows: 1
   :widths: 15 30 30

   * - **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}``

A dict object is mapping from keys to values:

.. GENERATED FROM PYTHON SOURCE LINES 77-82

.. code-block:: Python

    d = {'s': 0, 'p': 1}
    d['d'] = 2
    print('the whole dictionary:', d)
    print('one entry of the dictionary:', d['p'])





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    the whole dictionary: {'s': 0, 'p': 1, 'd': 2}
    one entry of the dictionary: 1




.. GENERATED FROM PYTHON SOURCE LINES 83-88

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:

.. GENERATED FROM PYTHON SOURCE LINES 90-95

.. code-block:: Python

    l = [1, ('gg', 7), 'hmm', 1.2]
    print('the whole list:', l)
    print('one list element:', l[1])
    print('negative index:', l[-2])





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    the whole list: [1, ('gg', 7), 'hmm', 1.2]
    one list element: ('gg', 7)
    negative index: hmm




.. GENERATED FROM PYTHON SOURCE LINES 96-111

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:


.. GENERATED FROM PYTHON SOURCE LINES 113-119

.. code-block:: Python

    a = ['q', 'w']
    b = a
    a.append('e')
    print('the original, changed list:', a)
    print('the second list:', b)





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    the original, changed list: ['q', 'w', 'e']
    the second list: ['q', 'w', 'e']




.. GENERATED FROM PYTHON SOURCE LINES 120-127

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:

.. GENERATED FROM PYTHON SOURCE LINES 129-138

.. code-block:: Python

    c = 42
    d = c + 5
    print('the first number:', c)
    print('the second number:', d)

    e = d
    e += 1
    print('second and third number:', (d, e))





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    the first number: 42
    the second number: 47
    second and third number: (47, 48)




.. GENERATED FROM PYTHON SOURCE LINES 139-148

.. note::

   Another very important type is the ``ndarray`` type described here:
   `Numeric arrays in Python <https://ase-lib.org/numpy.html#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:

.. GENERATED FROM PYTHON SOURCE LINES 150-154

.. code-block:: Python

    things = ['a', 7]
    for x in things:
        print(x)





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    a
    7




.. GENERATED FROM PYTHON SOURCE LINES 155-160

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:

.. GENERATED FROM PYTHON SOURCE LINES 162-165

.. code-block:: Python

    for i in range(5):
        print(i, i * i)





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    0 0
    1 1
    2 4
    3 9
    4 16




.. GENERATED FROM PYTHON SOURCE LINES 166-170

Functions and classes
=====================

A function is defined like this:

.. GENERATED FROM PYTHON SOURCE LINES 173-181

.. code-block:: Python

    def f(x, m=2, n=1):
        y = x + n
        return y**m


    print(f(5))
    print(f(5, n=8))





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    36
    169




.. GENERATED FROM PYTHON SOURCE LINES 182-186

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 class is defined like this:

.. GENERATED FROM PYTHON SOURCE LINES 189-200

.. code-block:: Python

    class A:
        def __init__(self, b):
            self.c = b

        def m(self, x):
            return self.c * x

        def get_c(self):
            return self.c









.. GENERATED FROM PYTHON SOURCE LINES 201-207

You can think of a class as a template for creating user defined objects. The
``__init__()`` function is called a 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.

.. GENERATED FROM PYTHON SOURCE LINES 209-214

.. code-block:: Python

    a = A(7)
    print(a.c)
    print(a.get_c())
    print(a.m(3))





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    7
    7
    21




.. GENERATED FROM PYTHON SOURCE LINES 215-241

Here we make an instance (or object) ``a`` of type ``A``.

Importing modules
=================

There are several ways to import modules (either ``.py`` files in your
working directory or packages that are installed in your python environment).
For example:

::

  import numpy; numpy.linspace(1, 10, num=5)

and

::

  import numpy as np; np.linspace(1, 10, num=5)

and

::

  from numpy import linspace; linspace(1, 10, num=5)

all yield the same result.


.. _sphx_glr_download_examples_generated_python_01_whatispython.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: 01_whatispython.ipynb <01_whatispython.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: 01_whatispython.py <01_whatispython.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: 01_whatispython.zip <01_whatispython.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
