Answers for "multiple if else in python in one line"

8

python if statement multiple lines

# ex. 1
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:
# ex. 2
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
Posted by: Guest on June-12-2020
0

multiple if else in python in one line

a = "neg" if b<0 else "pos" if b>0 else "zero"
Posted by: Guest on August-23-2021
0

multiple if else condition in single line in python

>>> i=100
>>> a = 1 if i<100 else 2 if i>100 else 0
>>> a
0
>>> i=101
>>> a = 1 if i<100 else 2 if i>100 else 0
>>> a
2
>>> i=99
>>> a = 1 if i<100 else 2 if i>100 else 0
>>> a
1
Posted by: Guest on May-05-2021

Code answers related to "multiple if else in python in one line"

Python Answers by Framework

Browse Popular Code Answers by Language