Python explicit line joining and implicit line joining

In this post we will discuss two terms Python explicit line joining and Python implicit line joining.


Python explicit line joining

When we want to join many logical lines to render a suitable condition, we can use the backslash characters (\). This character (\) will join the logical lines together as if they are declared in one single line. Such method of joining the lines using the backslash(\) is known as explicit line joining.

Consider the logical line given below.

>>> x> 80 and x< 100 | y<101 and x < y
True

The code is simple. It checks the value of ‘x’ and ‘y’ fro certain condition.

We can also write the code given above as.

>>> x> 80 and x< 100 | \
    y<101 and x < y
True

Instead of writing the logical line in one line we have used the backslash and continued the half logical expression in the next line. The above way of writing is allowed. The backslash purpose is very much clear here: to join the logical lines together. Here is another code example.

>>> a<100 and b>20 | \
    a>100 and c>100 | \
    b<50 | c>=100
False
>>> (a^2)>1000 or (b^2)> 500 and (c^2)>1000 or \
    (a*b)>(a*c) or (a^2)>(b*c) or (b^2)>(a*c) or \
    (c^2)>(a*b) or (c^3)>(a*b*c)
False


Some points to note:

i)There cannot be comment after the backslash character.

>>> a<100 and b>20 | \ #comment
SyntaxError: unexpected character after line continuation character

You get error message if there is any comment after the backslash.

ii)A backslash cannot continue comment.

iii)A backslash can continue only string literals. It cannot continue any other literals type.

>>> str="Happy " \
	 'new year and '\
	 "marry Christmas!"
>>> str
'Happy new year and marry Christmas!'
>>> #Using '\' to continue integers literals
>>> i=890 \
       234
SyntaxError: invalid syntax

Trying to continue the integer literals using ‘\’ render an error message.

iv) A backslash is illegal everywhere except the in string literals.


Python Implicit line joining

In Python implicit line joining we uses parentheses, square brackets or curly braces to split the expression in multiple lines. For instance consider the code below.

>>> #Using parentheses
>>> Month=( 'Jan' , 'Feb' , 'Mar' , 'April' , 'May' ,
	    'June' , 'July' , 'Aug' , 'Sept' ,
	    'Nov' , 'Dec' )
>>> #Using curly braces
>>> Weekdays={ 'Monday' , 'Tuesday' ,
	       'Wednesday' , 'Thursday' , 'Friday' ,
	       'Saturday' , 'Sunday' }

Some points to note:

i)There can be comment after the line.

 
>>> Weekdays={ 'Monday' , 'Tuesday' , #First line
	       'Wednesday' , 'Thursday' , 'Friday' , #Second line  
	       'Saturday' , 'Sunday' } #third line

ii)Blank lines are allowed to continue.

 
>>> Var={      #Blank
	'new',
	'old'}

iii)Triple-quoted strings are also allowed to have continued lines but here there cannot be comment in each line.

>>> tri_str={ """new and old""",
	     """Sad and joy""" ,
	     '''Delight and dismay''' }
>>> tri_str
{'Delight and dismay', 'Sad and joy', 'new and old'}



Leave a Reply

Your email address will not be published. Required fields are marked *