<commit_before>Language Basics
===============

This section provides an informal language reference. It takes you through the base language features of Python and provides an example of how the equivalent is expressed in Chapel.

Variables and Types
-------------------

In Python variables are *implicitly* declared when assigned to along with their type. In Chapel variable declaration is *explicit*, but, the type of the variable can be inferred from its use in a manner equivalent to that of Python.

+--------------------------------------------------+-+----------------------------------------------------+
| Python                                           | | Chapel                                             |
+==================================================+=+====================================================+
| .. literalinclude:: /examples/vars.decl.py       | | .. literalinclude:: /examples/vars.decl.chpl       |
|    :language: python                             | |    :language: c                                    |
+--------------------------------------------------+-+----------------------------------------------------+

Types in Python are dynamic meaning that a variable can change type during it's lifetime.
The type of a variable in Chapel is static and inferred at compile-time.

Types Chapel are static, meaning that the inferred t statically type in contrast to dynamic duck-typing of Python. However, 

Duck-typing vs. Chapels static typing and type-inference.

Comments
--------

+--------------------------------------------------+-+----------------------------------------------------+
| Python                                           | | Chapel                                             |
+==================================================+=+====================================================+
| .. literalinclude:: /examples/comments.py        | | .. literalinclude:: /examples/comments.chpl        |
|    :language: python                             | |    :language: c                                    |
+--------------------------------------------------+-+----------------------------------------------------+


Literals
~~~~~~~~

These work in much the same way that you are used to. A brief overview is provided below.

+--------------------------------------------------+-+----------------------------------------------------+
| Python                                           | | Chapel                                             |
+==================================================+=+====================================================+
| .. literalinclude:: /examples/literals.py        | | .. literalinclude:: /examples/literals.chpl        |
|    :language: python                             | |    :language: c                                    |
+--------------------------------------------------+-+----------------------------------------------------+

Console input / output
----------------------

You can write to the console (standard output) using ``write`` and ``writeln``:

+--------------------------------------------------+-+----------------------------------------------------+
| Python                                           | | Chapel                                             |
+==================================================+=+====================================================+
| .. literalinclude:: /examples/console.py         | | .. literalinclude:: /examples/console.chpl         |
|    :language: python                             | |    :language: c                                    |
+--------------------------------------------------+-+----------------------------------------------------+

You can read input from the console (standard input) using ``read`` and ``readln``:

+--------------------------------------------------+-+----------------------------------------------------+
| Python                                           | | Chapel                                             |
+==================================================+=+====================================================+
| .. literalinclude:: /examples/console.read.py    | | .. literalinclude:: /examples/console.read.chpl    |
|    :language: python                             | |    :language: c                                    |
+--------------------------------------------------+-+----------------------------------------------------+

.. note::
    Notice that the interface for reading input is quite different, however, also equivally simple. In Python you need to explicitly cast the input, where in Chapel the type of the input is provided to the ``read/readln`` functions directly.


Conditionals and Blocks
-----------------------

Python is famous for using an indentation guided block-structure, thereby arguably improving readability and increasing consistency of code-style. Chapel uses curly-brackets to denote the start and end of a block.

+--------------------------------------------------+-+----------------------------------------------------+
| Python                                           | | Chapel                                             |
+==================================================+=+====================================================+
| .. literalinclude:: /examples/cond.if.py         | | .. literalinclude:: /examples/cond.if.chpl         |
|    :language: python                             | |    :language: c                                    |
+--------------------------------------------------+-+----------------------------------------------------+

Switch / Case
~~~~~~~~~~~~~

Python does not support ``switch-statements`` and instead rely on chaining ``if-elif-else`` statements.

Chapel on the other hand does have a ``switch-statements``, specifically ``select-when-otherwise`` statements:

+-----------------------------------------------+-+-------------------------------------------------+
| Python                                        | | Chapel                                          |
+===============================================+=+=================================================+
| .. literalinclude:: /examples/cond.switch.py  | | .. literalinclude:: /examples/cond.switch.chpl  |
|    :language: python                          | |    :language: c                                 |
+-----------------------------------------------+-+-------------------------------------------------+

.. note::
    Notice than in the case of both Python and Chapel then these forms of ``switch-statements`` does not **fall through**, meaning that one and only one case will be executed. Coming from Python this might not surpise you, however, if you have ever written a ``switch-statement`` in other languages then this is slightly surprising.

Ranges
------

In Python ``range`` is a list-constructor often used for driving for-loops or list comprehensions. For lowered memory comsumption Python provides the generator equivalent of ``range`` namely ``xrange``.

In Chapel a **range** is a language-construct which behaves and is used in much the same way as lists are used in Python. When u think about lists and slicing operations in Python, think of ranges in Chapel.

+--------------------------------------------------+-+----------------------------------------------------+
| Python                                           | | Chapel                                             |
+==================================================+=+====================================================+
| .. literalinclude:: /examples/ranges.py          | | .. literalinclude:: /examples/ranges.chpl          |
|    :language: python                             | |    :language: c                                    |
+--------------------------------------------------+-+----------------------------------------------------+

.. note::
    In Python, ``range`` return values in the interval ``[start, stop]``.
    In Chapel a range-expression yields values the interval ``[start, stop]``.

For both languages the above is a shorthand of the wider form: ``start, stop, step``.

+--------------------------------------------------+-+----------------------------------------------------+
| Python                                           | | Chapel                                             |
+==================================================+=+====================================================+
| .. literalinclude:: /examples/ranges.skip.py     | | .. literalinclude:: /examples/ranges.skip.chpl     |<commit_msg>Python range is in fact [start, stop[.<commit_after>Language Basics
===============

This section provides an informal language reference. It takes you through the base language features of Python and provides an example of how the equivalent is expressed in Chapel.

Variables and Types
-------------------

In Python variables are *implicitly* declared when assigned to along with their type. In Chapel variable declaration is *explicit*, but, the type of the variable can be inferred from its use in a manner equivalent to that of Python.

+--------------------------------------------------+-+----------------------------------------------------+
| Python                                           | | Chapel                                             |
+==================================================+=+====================================================+
| .. literalinclude:: /examples/vars.decl.py       | | .. literalinclude:: /examples/vars.decl.chpl       |
|    :language: python                             | |    :language: c                                    |
+--------------------------------------------------+-+----------------------------------------------------+

Types in Python are dynamic meaning that a variable can change type during it's lifetime.
The type of a variable in Chapel is static and inferred at compile-time.

Types Chapel are static, meaning that the inferred t statically type in contrast to dynamic duck-typing of Python. However, 

Duck-typing vs. Chapels static typing and type-inference.

Comments
--------

+--------------------------------------------------+-+----------------------------------------------------+
| Python                                           | | Chapel                                             |
+==================================================+=+====================================================+
| .. literalinclude:: /examples/comments.py        | | .. literalinclude:: /examples/comments.chpl        |
|    :language: python                             | |    :language: c                                    |
+--------------------------------------------------+-+----------------------------------------------------+


Literals
~~~~~~~~

These work in much the same way that you are used to. A brief overview is provided below.

+--------------------------------------------------+-+----------------------------------------------------+
| Python                                           | | Chapel                                             |
+==================================================+=+====================================================+
| .. literalinclude:: /examples/literals.py        | | .. literalinclude:: /examples/literals.chpl        |
|    :language: python                             | |    :language: c                                    |
+--------------------------------------------------+-+----------------------------------------------------+

Console input / output
----------------------

You can write to the console (standard output) using ``write`` and ``writeln``:

+--------------------------------------------------+-+----------------------------------------------------+
| Python                                           | | Chapel                                             |
+==================================================+=+====================================================+
| .. literalinclude:: /examples/console.py         | | .. literalinclude:: /examples/console.chpl         |
|    :language: python                             | |    :language: c                                    |
+--------------------------------------------------+-+----------------------------------------------------+

You can read input from the console (standard input) using ``read`` and ``readln``:

+--------------------------------------------------+-+----------------------------------------------------+
| Python                                           | | Chapel                                             |
+==================================================+=+====================================================+
| .. literalinclude:: /examples/console.read.py    | | .. literalinclude:: /examples/console.read.chpl    |
|    :language: python                             | |    :language: c                                    |
+--------------------------------------------------+-+----------------------------------------------------+

.. note::
    Notice that the interface for reading input is quite different, however, also equivally simple. In Python you need to explicitly cast the input, where in Chapel the type of the input is provided to the ``read/readln`` functions directly.


Conditionals and Blocks
-----------------------

Python is famous for using an indentation guided block-structure, thereby arguably improving readability and increasing consistency of code-style. Chapel uses curly-brackets to denote the start and end of a block.

+--------------------------------------------------+-+----------------------------------------------------+
| Python                                           | | Chapel                                             |
+==================================================+=+====================================================+
| .. literalinclude:: /examples/cond.if.py         | | .. literalinclude:: /examples/cond.if.chpl         |
|    :language: python                             | |    :language: c                                    |
+--------------------------------------------------+-+----------------------------------------------------+

Switch / Case
~~~~~~~~~~~~~

Python does not support ``switch-statements`` and instead rely on chaining ``if-elif-else`` statements.

Chapel on the other hand does have a ``switch-statements``, specifically ``select-when-otherwise`` statements:

+-----------------------------------------------+-+-------------------------------------------------+
| Python                                        | | Chapel                                          |
+===============================================+=+=================================================+
| .. literalinclude:: /examples/cond.switch.py  | | .. literalinclude:: /examples/cond.switch.chpl  |
|    :language: python                          | |    :language: c                                 |
+-----------------------------------------------+-+-------------------------------------------------+

.. note::
    Notice than in the case of both Python and Chapel then these forms of ``switch-statements`` does not **fall through**, meaning that one and only one case will be executed. Coming from Python this might not surpise you, however, if you have ever written a ``switch-statement`` in other languages then this is slightly surprising.

Ranges
------

In Python ``range`` is a list-constructor often used for driving for-loops or list comprehensions. For lowered memory comsumption Python provides the generator equivalent of ``range`` namely ``xrange``.

In Chapel a **range** is a language-construct which behaves and is used in much the same way as lists are used in Python. When u think about lists and slicing operations in Python, think of ranges in Chapel.

+--------------------------------------------------+-+----------------------------------------------------+
| Python                                           | | Chapel                                             |
+==================================================+=+====================================================+
| .. literalinclude:: /examples/ranges.py          | | .. literalinclude:: /examples/ranges.chpl          |
|    :language: python                             | |    :language: c                                    |
+--------------------------------------------------+-+----------------------------------------------------+

.. note::
    In Python, ``range`` return values in the interval ``[start, stop[``.
    In Chapel a range-expression yields values the interval ``[start, stop]``.

For both languages the above is a shorthand of the wider form: ``start, stop, step``.

+--------------------------------------------------+-+----------------------------------------------------+
| Python                                           | | Chapel                                             |
+==================================================+=+====================================================+
| .. literalinclude:: /examples/ranges.skip.py     | | .. literalinclude:: /examples/ranges.skip.chpl     |
{'fraction_non_alphanumeric': 0.387250625254464, 'fraction_numerical': 0.0, 'mean_word_length': 1.0861441397719, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 101, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4082724', 'n_tokens_mistral': 2939, 'n_tokens_neox': 2690, 'n_words': 1036}