This question already has answers here:
If and elif in Python for good programming practices
(2 answers)
Closed 4 years ago.
Does someone have a simple answer as to why you would use a If If rather than an If Elif?
Example:
def fun(item):
if item[0]<'M':
return 0
if item[0]<'Q':
return 1
return 2
With a setup like this, where you are returning a value inside each if block, the if statements behave exactly the same as an if elif structure since the return exits out of the function without checking the conditions below. So, when the functionality is the same, the choice should be made on readability. explicit is better than implicit.
There is no good reason to use if statements like this implying exclusively in the conditionals, when if elif statements makes the exclusivity explicit and is much more readable and easier to maintain.
Related
This question already has answers here:
Python Ternary Operator Without else
(8 answers)
Conditional return with no else
(2 answers)
Closed last month.
Quite frequently, I have written lines like
if arg == foo: return bar
It is naturally a one-liner, at the beginning of a function body. Notice there is no else, it just returns on a special value of the parameter, and proceeds with the normal flow of the function otherwise.
Still, it feels like the order is wrong. In perl it is possible to write (modulo some $'s)
return bar if arg == foo
which feels more natural. Matter of taste, I know.
Is there a pythonic way of writing something after a return word that would impose a condition on the return statement?
It is, of course, possible, that there is no way.
This question already has answers here:
if x:, vs if x == True, vs if x is True
(8 answers)
Closed 2 years ago.
When making an 'if' statement in Python, we have the option to use if boolean:, without making an explicit comparison (which is not mandatory)
The question is: what is the default comparison in this case? Is if x: equivalent to if x == True or if x is True?
Not sure if I understand your question, but you can use:
if True:
print("hi")
which will return "hi" once
If you're looking to make an infinite loop, try something like this:
while True:
#do whatever here and it will loop infinitely
This question already has answers here:
Simplify Chained Comparison
(2 answers)
Closed 5 years ago.
I have two integer value cnt_1 and cnt_2, and I write the following statements:
if cnt_1 < 0 and cnt_2 >= 0:
# some code
This statement gets underlined, and the tooltip tells me that I must:
simplify chained comparison
As far as I can tell, that comparison is about as simple as they come. What have I missed here?
The question is a little different from link, there are different variables in comparison.
Your expression can be rewritten as:
if cnt_1 < 0 <= cnt_2:
This is called comparison chaining.
Pycharm is trying to tell you that the equation can be simplified. If you want to know what PyCharm would prefer it to be, PyCharm will help automate this fix. If you navigate your cursor to the underlined code and do:
Alt + Enter -> 'Simplify chained expression'
PyCharm will change this to:
if cnt_1 < 0 <= cnt_2:
The warning will now be gone. If you prefer the original code and are just wanting the warning to go away you can place your cursor on the warning and do
Alt + Enter -> 'Ignore...'
And this type of error will no longer be flagged. You can also access both of these options on a global scale by doing.
Code->"Inspect code..."-> (Choose the scope you'd like to inspect) -> Ok
This will give you a list of all the warnings in the scope you've selected and provide you with an automated method to fix many of them.
This question already has answers here:
How to use "pass" statement?
(18 answers)
Closed 6 years ago.
What does pass mean in python? I have seen it used and I do not see why you need to use it or what it does? I guess I could say it passes over whatever function it is in, but why include it?
Quoth the doc:
The pass statement does nothing. It can be used when a statement is required syntactically but the program requires no action.
References:
https://docs.python.org/2/reference/simple_stmts.html#the-pass-statement
https://docs.python.org/2/tutorial/controlflow.html#pass-statements
"It is used when a statement is required syntactically but you do not want any command or code to execute.
The pass statement is a null operation; nothing happens when it executes. The pass is also useful in places where your code will eventually go, but has not been written yet (e.g., in stubs for example):"
https://www.tutorialspoint.com/python/python_pass_statement.htm
This question already has answers here:
Using pass on a non necessary else statement
(5 answers)
Closed 8 years ago.
Is there a preferred/proper style?
This:
def fx(Boolean):
if Boolean:
# Do stuff.
else:
pass
Or this:
def fx(Boolean):
if Boolean:
# Do stuff.
Is it preferred/proper to include else: pass if you don't want anything to happen?
I've read PEP 8 - Style Guide for Python Code and did not find anything concerning my question.
You should never include else: pass. It's superfluous. Just omit the else; it's intentionally an optional keyword.
If you don't need the else if there is no reason to add it. It will just confuse other people reading your code in the future (ie yourself a few months later).