What is the default comparison in a python's if declaration? [duplicate] - python

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

Related

Python conditional return [duplicate]

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.

When to use If If over an If Elif in Python [duplicate]

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.

Python: If-Else Statement That Depends on Calling Function [duplicate]

This question already has answers here:
Get function callers' information in python
(3 answers)
How to get the caller's method name in the called method?
(11 answers)
Closed 5 years ago.
I've been thinking about this question for a while, and I can't seem to find any related questions to it, probably because I don't know the proper terminology for what I'm looking for.
Is there any condition for an if-else statement in a function that is reliant on which other function is doing the calling? Such as:
def FUNC():
if func1 called FUNC:
statement
elif func2 called FUNC:
statement
def func1():
FUNC()
def func2():
FUNC()
I'm not sure what purpose there would be behind doing this, but I just wanted to know if it was an available option. Thank you.
Solution: pass an argument.
def FUNC(who_called_me):
if who_called_me == 'func1':
# statement
elif who_called_me == 'func2':
# statement
def func1():
FUNC(who_called_me='func1')
def func2():
FUNC(who_called_me='func2')
The argument (who_called_me) does not have to be of type string, but can also be an integer, boolean or an object.

Proper Python Syntax and Semantics: if, else, pass [duplicate]

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).

Why is assert a statement in Python? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
design of python: why is assert a statement and not a function?
In Python 3, print was made into a function. What are the benefits of having assert be a statement?
For optimization. If you run your Python script with the -O option no code will be generated for assert statements. This would not be possible if assert were a function.
See the documentation on assert, which references this behavior.

Categories

Resources