0 votes
1 view
in Python by (50.5k points)

Sometimes I break long conditions in ifs onto several lines. The most obvious way to do this is:

if (cond1 == 'val1' and cond2 == 'val2' and cond3 == 'val3' and cond4 == 'val4'): 


It isn't very appealing visually, because the action blends with the conditions. However, it is the natural way using the correct Python indentation of 4 spaces.

For the moment I'm using:

if ( cond1 == 'val1' and cond2 == 'val2' and cond3 == 'val3' and cond4 == 'val4'): 


But this isn't very pretty. :-)

Can you recommend an alternative way?

1 Answer

0 votes
by (107k points)

For multi-line, if statement you can use the following way:-

if(cond1 == 'val1' and \ 

cond2 == 'val2' and \ 

cond3 == 'val3' and \ 

cond4 == 'val4'):


If you will follow below-mentioned way then it will shave a few characters and makes it clear that there's no subtlety to the condition.

if all( [cond1 == 'val1', cond2 == 'val2', cond3 == 'val3', cond4 == 'val4'] ): 

if any( [cond1 == 'val1', cond2 == 'val2', cond3 == 'val3', cond4 == 'val4'] ):

Related questions

0 votes
1 answer
0 votes
2 answers
0 votes
1 answer
asked Sep 25, 2019 in Python by Sammy (47.8k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers !