Computer Science, asked by chhavi4944, 1 year ago

How to style multi-line conditions in 'if' statements in Python?

Answers

Answered by Anonymous
0

You don't need to use 4 spaces on your second conditional line. Maybe use:

if (cond1 == 'val1' and cond2 == 'val2' and

cond3 == 'val3' and cond4 == 'val4'):

do_something

Also, don't forget the whitespace is more flexible than you might think:

if (

cond1 == 'val1' and cond2 == 'val2' and

cond3 == 'val3' and cond4 == 'val4'

):

do_something

if (cond1 == 'val1' and cond2 == 'val2' and

cond3 == 'val3' and cond4 == 'val4'):

do_something

Both of those are fairly ugly though.

Maybe lose the brackets (the Style Guide discourages this though)?

if cond1 == 'val1' and cond2 == 'val2' and \

cond3 == 'val3' and cond4 == 'val4':

do_something

This at least gives you some differentiation.

Similar questions