{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# What is Python?\n\nThis section will give a very brief introduction to the Python language.\n\n.. tip::\n\n See also the [Python home page](https://www.python.org/) for further\n information.\n\n# Executing Python code\nYou can execute Python code interactively by starting the interpreter like with\nthe command ``python3`` and test it with any python command such as:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print('hello')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also put the ``print(\"hello\")`` line in a file (``hello.py``) and\nexecute it as a Python script with ``python3 hello.py`` or ``python3 -i\nhello.py`` to enter interactive mode after the file is executed.\n\nFinally, you can put ``#!/usr/bin/env python3`` in the first line of the\n``hello.py`` file, make it executable (``chmod +x hello.py``) and execute it\nlike any other executable.\n\n\n\n.. tip::\n\n For a better interactive experience, consider ipython.\n\n# Types\n\n.. list-table:: Supported Python Types\n :header-rows: 1\n :widths: 15 30 30\n\n * - **Type**\n - **Description**\n - **Example**\n * - ``bool``\n - Boolean\n - ``False``\n * - ``int``\n - Integer\n - ``117``\n * - ``float``\n - Floating point number\n - ``1.78``\n * - ``complex``\n - Complex number\n - ``0.5 + 2.0j``\n * - ``str``\n - String\n - ``'abc'``\n * - ``tuple``\n - Tuple\n - ``(1, 'hmm', 2.0)``\n * - ``list``\n - List\n - ``[1, 'hmm', 2.0]``\n * - ``dict``\n - Dictionary\n - ``{'a': 7.0, 23: True}``\n\nA dict object is mapping from keys to values:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "d = {'s': 0, 'p': 1}\nd['d'] = 2\nprint('the whole dictionary:', d)\nprint('one entry of the dictionary:', d['p'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this example all keys are strings and all values are integers. Types can\nbe freely mixed in the same dictionary; any type can be used as a value and\nmost types can be used as keys (mutable objects cannot be keys).\n\nA ``list`` object is an ordered collection of arbitrary objects:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "l = [1, ('gg', 7), 'hmm', 1.2]\nprint('the whole list:', l)\nprint('one list element:', l[1])\nprint('negative index:', l[-2])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Indexing a list with negative numbers counts from the end of the list, so\nelement ``-2`` is the second last.\n\nA tuple behaves like a list -- except that it can\u2019t be modified in place.\nObjects of types list and dict are mutable -- all the other types listed in\nthe table are immutable, which means that once an object has been created, it\ncan not change. Tuples can therefore be used as dictionary keys, lists\ncannot.\n\n
List and dictionary objects can change. Variables in Python are references\n to objects -- think of the ``=`` operator as a \u201cnaming operator\u201d, not as\n an assignment operator. This is demonstrated here:
Another very important type is the ``ndarray`` type described here:\n [Numeric arrays in Python](https://ase-lib.org/numpy.html#numpy). It is\n an array type for efficient numerics, and is heavily used in ASE.