> For the complete documentation index, see [llms.txt](https://zmnako.gitbook.io/datascience/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://zmnako.gitbook.io/datascience/comments.md).

# Multi-line Statements

Python codes are merely just text, aka physical lines. Python compiles physical lines into logical lines then tokenise them.

Physical line end with a physical **newline** character. Logical line end with a logical **NEWLINE** token.&#x20;

### Physical newline vs logical newline

Physical newlines are ignored in order to combine multiple physical lines into a single logical line of code terminated by a logical NEWLINE token.

Conversion from physical line to logical line can either be **`implicit`** or **`explicit`.**

### Implicit

Implicit conversion of multiple physical lines into a logical line happen automatically by Python without the user.

These expressions support are implicitly convert multiple physical lines into a logical line:

* *list* literals: **\[ ]**

Python implicitly understand that there is continuation of lines

{% tabs %}
{% tab title="Simple list" %}

```python
[2, 
 4, 
 6]
```

{% endtab %}

{% tab title="Comment within a list" %}

```python
[2, #comment 1 
 4, #comment 2 
 6  #comment 3 
 ]
```

{% endtab %}
{% endtabs %}

* *tuple* literals: **( )**
* *dictionary* literals: **{ }**
* *set* literals: **{ }**
* *function* arguments / parameters
  * supports inline comments

{% tabs %}
{% tab title="Create function" %}

```python
def new_fun(value_1, #comment
            value_2):
   sum = value_1 + value_2
   return print(sum)
```

{% endtab %}

{% tab title="Call the function" %}

```python
new_fun(10, # first value
        20)
```

{% endtab %}

{% tab title="Output" %}

```
30
```

{% endtab %}
{% endtabs %}

### Explicit

Explicit conversion of multiple physical lines into a logical line happen where the user explicitly tells Python.&#x20;

You can break up statements over multiple lines explicitly, by using the  (`backslash`) character. Multi-line statements are not implicitly converted to a single logical line.

When we write codes, we do not want to have very long lines and have to keep readability in mind. In this example the code works as intended.

{% tabs %}
{% tab title="Code" %}

```python
value_1 = 3
value_2 = 15
value_3 = 100
if value_1 < 5 and value_2 > 10 and value_3 > 50:
    print("conditions are met")
```

{% endtab %}

{% tab title="Output" %}

```python
conditions are met
```

{% endtab %}
{% endtabs %}

But this one fails because Python cannot implicitly understand that there is continuation.

{% tabs %}
{% tab title="Code" %}

```python
if value_1 < 5 and 
   value_2 > 10 and 
   value_3 > 50:
   print("conditions are met")
```

{% endtab %}

{% tab title="Output" %}

```python
File "<ipython-input-2-c6eef7781e00>", line 1 
    if value_1 < 5 and ^ 

SyntaxError: invalid syntax
```

{% endtab %}
{% endtabs %}

We can explicitly tell Python that there is continuation by adding `backslash \`.The code runs successfully and prints the below output.

{% tabs %}
{% tab title="Code" %}

```python
if value_1 < 5 and \
   value_2 > 10 and \
   value_3 > 50:
    print("conditions are met")
```

{% endtab %}

{% tab title="Output" %}

```python
conditions are met
```

{% endtab %}
{% endtabs %}

We can also use parenthesis `()`to explicitly tell the physical lines about continuation.

{% tabs %}
{% tab title="Code" %}

```python
if (value_1 < 5 and
    value_2 > 10 and
    value_3 > 50):
  print("conditions are met")
```

{% endtab %}

{% tab title="Output" %}

```python
conditions are met
```

{% endtab %}
{% endtabs %}

### Multi-line Strings

Multi-line string literals can be created using triple delimiters (`' single` or `" double`)

{% tabs %}
{% tab title="Single quote multi-line string" %}

```python
'''You can create 
multi-line string this way'''
```

{% endtab %}

{% tab title="Double quote multi-line string" %}

```python
"""You can create 
multi-line string this way too"""
```

{% endtab %}
{% endtabs %}

A multi-line string is just a regular string.

Basically, whatever you type is part of your multi-line comment such as **tabs, spaces, newlines** etc. are part of your multi-line statement

You can use escaped characters (e.g. `\n`, `\t`), use string formatting.

Multi-line strings are not *comments*, although they can be used as such, especially with special comments called `docstrings`.
