Python - squeeze more than one statement onto a single line

Introduction

Although statements normally appear one per line, to squeeze more than one statement onto a single line in Python by separating them with semicolons:

a = 1; b = 2; print(a + b)               # Three statements on one line 

This is the only place in Python where semicolons are required.

Parentheses are the catch all since any expression can be wrapped in them.

X = (A + B + 
     C + D) 


if (A == 1 and 
    B == 2 and 
    C == 3): 
        print('test' * 3) 

An older rule allows for continuation lines when the prior line ends in a backslash:

X = A + B + \ 
      C + D     # An error-prone older alternative 

Related Topic