Machine Learning in Cloud

Machine Learning in Cloud

Multi-line Statements

A brief introduction to multi-line statements and usage.

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.

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

#Simple list
[2, 
 4, 
 6]

#Comments within a list
[2, #comment 1 
 4, #comment 2 
 6  #comment 3 
 ]
  • tuple literals: ( )
  • dictionary literals: { }
  • set literals: { }
  • function arguments / parameters
    • supports inline comments
#create a function
def new_fun(value_1, #comment
            value_2):
   sum = value_1 + value_2
   return print(sum)

#call the function
new_fun(10, # first value
        20)

Output:

30

Explicit

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

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.

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\")

Output:

conditions are met

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

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

Output:

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

SyntaxError: invalid syntax

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

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

Output:

conditions are met

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

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

Output:

conditions are met

Multi-line Strings

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

#single quote multi-line string
\'\'\'You can create 
multi-line string this way\'\'\'


#double quote multi-line string
\"\"\"You can create 
multi-line string this way too\"\"\"

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.

Previous Article
Next Article