in Education by
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'): do_something 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'): do_something But this isn't very pretty. :-) Can you recommend an alternative way? Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
For multi-line, if statement you can use the following way:- if(cond1 == 'val1' and \ cond2 == 'val2' and \ cond3 == 'val3' and \ cond4 == 'val4'): Do_something 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
    I have a code: ab = 'off' if b.page == 'blog': ab = 'on' print ab Is there any other way to make it shorter in Python? Select the correct answer from above options...
asked Jan 25, 2022 in Education by JackTerrance
0 votes
    I'm trying to make a function that will compare multiple variables to an integer and output a string of three ... like this possible? Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    Myself and a colleague have a dispute about which of the following is more elegant. I won't say ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 10, 2022 in Education by JackTerrance
0 votes
    How do I perform an IF...THEN in an SQL SELECT statement? For example: SELECT IF(Obsolete = 'N' OR ... Saleable, * FROM Product Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    I have a very long query. I would like to split it in several lines in Python. A way to do it in JavaScript ... '\ 'def_id=' + def_id Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    I need to get a line count of a large file (hundreds of thousands of lines) in python. What is the most ... possible to do any better? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    I am originally a C programmer. I have seen numerous tricks and "hacks" to read many different arguments. What are ... can do this? Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    Can someone tell me any methods to read every line of a file in Python and store each line as an element in a ... till the end of list. Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    I have a JTextField and need to have 3 lines in it, such as these: HI MY name is mehdi How can ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 3, 2022 in Education by JackTerrance
0 votes
    I have a ListBox that scrolls images horizontally. I have the following XAML I used blend to create it. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    I wanted to make my own WebApp for testing so I will know what to do when I've got some serious ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    I'm curious about manipulating time in Python. I can get the (last modified) age of a file using ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    I have a fixed list of services in a Linux server. I want to check the status of these services ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    I want to test a 2x2 matrix of [[5,6],[7,8]] to see if it's a square. I run ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    In the os module in Python, is there a way to find if a directory exists, something like: os.direxists(os ... in pseudocode True/False Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
...