Putting a simple if-then-else statement on one line [duplicate] - python

This question already has answers here:
Does Python have a ternary conditional operator?
(31 answers)
Closed 3 years ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
I'm just getting into Python and I really like the terseness of the syntax. However, is there an easier way of writing an if-then-else statement so it fits on one line?
For example:
if count == N:
count = 0
else:
count = N + 1
Is there a simpler way of writing this? I mean, in Objective-C I would write this as:
count = count == N ? 0 : count + 1;
Is there something similar for Python?
Update
I know that in this instance I can use count == (count + 1) % N.
I'm asking about the general syntax.

That's more specifically a ternary operator expression than an if-then, here's the python syntax
value_when_true if condition else value_when_false
Better Example: (thanks Mr. Burns)
'Yes' if fruit == 'Apple' else 'No'
Now with assignment and contrast with if syntax
fruit = 'Apple'
isApple = True if fruit == 'Apple' else False
vs
fruit = 'Apple'
isApple = False
if fruit == 'Apple' : isApple = True

Moreover, you can still use the "ordinary" if syntax and conflate it into one line with a colon.
if i > 3: print("We are done.")
or
field_plural = None
if field_plural is not None: print("insert into testtable(plural) '{0}'".format(field_plural))

count = 0 if count == N else N+1
- the ternary operator. Although I'd say your solution is more readable than this.

General ternary syntax:
value_true if <test> else value_false
Another way can be:
[value_false, value_true][<test>]
e.g:
count = [0,N+1][count==N]
This evaluates both branches before choosing one. To only evaluate the chosen branch:
[lambda: value_false, lambda: value_true][<test>]()
e.g.:
count = [lambda:0, lambda:N+1][count==N]()

<execute-test-successful-condition> if <test> else <execute-test-fail-condition>
with your code-snippet it would become,
count = 0 if count == N else N + 1

Related

Question based on operator precedence in Python

str = 'Welcome to the Jungle'
count = 0
for i in str:
if i=='a' or i=='e' not in i=='l' or i=='o' or i=='u':
count += 1
print(count)
Using the precedence order given on https://data-flair.training/blogs/python-operator-precedence/ , == and not in operators have the same precedence so I must go from left to right. According to that logic, since i=='e' evaluates to False for the i=0 that is the first letter W. So, we have False not in i(not in should be evaluated first according to left to right order) which would equate to True for all letters in str except i='e'. But, the python interpreter gives the value of count as 3 which is much less than what should come according to my logic.
Can someone please explain how to solve this problem? This is my first question on StackOverflow, so I apologize if I've written the question in wrong formatting.
Thank You.
#SaumilSood - you have asked how to solve this problem - I will assume you're interested in finding out how many vowels in the sentence in the Pythonic way, if yes, you could try this:
s = 'Welcome to the Jungle'
>>> vowels = 'aeiou'
>>> sum(1 for c in s if c in vowels)
7

Is there a cleaner way to write this boolean comparison in Python? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
The instructions for this assignment are:
We pass in 2 boolean inputs, cold and rainy.
You should output a single string: ('cold' or 'warm') ' and ' ('rainy'
or 'dry') based on these inputs.
('cold' or 'warm') means you should use on of the two words, depending
on the input boolean value.
for example False, True = 'warm and rainy'
The code I have put is:
# Get our boolean values from the command line
import sys
isCold= sys.argv[1] == 'True'
isRainy= sys.argv[2] == 'True'
# Your code goes here
condition = ""
if (isCold):
condition += "cold"
else:
condition += "warm"
if (isRainy):
condition += " and rainy"
else:
condition += " and dry"
print(condition)
The code is correct and outputs what it is supposed to, but I'm wondering is there a cleaner way to write this? I feel like there is but I can't quite figure it out.
You can combine conditional expressions with Python 3.6's f-strings to build the string with a single line of code:
condition = f"{'cold' if isCold else 'warm'} and {'rainy' if isRainy else 'dry'}"
In Python 2, the % string formatting operator can work as well:
condition = "%s and %s" % (
'cold' if isCold else 'warm',
'rainy' if isRainy else 'dry'
)
Use conditional expressions.
condition += "cold" if isCold else "warm"
condition += " and " + ("rainy" if isRainy else "dry")
I would recommend first using ternary expressions to get the string values for cold/warm and rainy/dry:
coldText = 'cold' if isCold else 'warm'
rainyText = 'rainy' if isRainy else 'dry'
Then use string interpolation to construct your final string, so that form and content are separated:
finalText = '%s and %s' % (coldText, rainyText)
print(finalText)
Another approach is to use dictionaries:
condition = {True: 'cold', False: 'warm'}[isCold] + ' and ' + {True: 'rainy', False: 'dry'}[isRainy]
Less pythonic, because less explicit:
one can use coercion of True and False to 1 and 0 and use it as index.
condition = ['warm', 'cold'][isCold] + ' and ' + ['dry', 'rainy'][isRainy]
I use coercion of True and False to 1 and 0 often when e.g. counting True values in a boolean list. (sum() over the list).

Linking up statements using the 'and' keyword [duplicate]

This question already has answers here:
Check if all values in list are greater than a certain number
(9 answers)
Closed 6 years ago.
I am doing this following:
if ycoords[0] > 0 and ycoords[1] > 0 and ycoords[2] > 0:
# do stuff
Can you shorten this code by doing something like:
if (ycoords[0] and ycoords[1] and ycoords[2]) > 0:
# do stuff
Yes, you could use all:
if all(x > 0 for x in ycoords):
or ycoords[:3] if ycoords has more than 3 elements.
No, you can however simply use min to simplify the test syntactically:
if min(ycoords[0],ycoords[1],ycoords[2]) > 0:
# do stuff
and given that ycoords exactly has three elements, even shorter:
if min(*ycoords) > 0:
#do stuff
you can here, as #Tagc says, omit the asterisk (*):
if min(ycoords) > 0:
#do stuff
but this will result in some overhead.
Another option is to use an all:
if all(x > 0 for x in [ycoords[0],ycoords[1],ycoords[2]]):
# do stuff
or again, if ycoords contains only these three elements:
if all(x > 0 for x in ycoords):
# do stuff
Something that is not intuitive is that and:
"neither and nor or restrict the value and type they return to False and True, but
rather return the last evaluated argument"
Just open a python terminal and do:
>> 4 and 3
3
Why is this important to take in account?
You might think that:
(ycoords[0] and ycoords[1] and ycoords[2]) > 0
is equivalent to:
ycoords[0] > 0 and ycoords[1] > 0 and ycoords[2] > 0
or that is equivalent to:
(bool(ycoords[0]) and bool(ycoords[1]) and bool(ycoords[2])) > 0
but it's instead equivalent to:
ycoords[2] > 0
So, be careful because the interpreter is not doing what you think is doing.

What does if-if-else one-line do in Python? [duplicate]

This question already has answers here:
Does Python have a ternary conditional operator?
(31 answers)
Closed 6 years ago.
I ran into this expression in a question here about knapsack problems:
def f(v, i, S):
if i >= len(v): return 1 if S == 0 else 0
count = f(v, i + 1, S)
count += f(v, i + 1, S - v[i])
return count
When I try to write out line two if i >= len(v): return 1 if S == 0 else 0 in a more general form I get an error:
In [3]: if test1 : print x if test2 else print y
File "<ipython-input-3-9d4131fa0c48>", line 1
if test1 : print x if test2 else print y
^
SyntaxError: Missing parentheses in call to 'print'
Here is a generalized form:
In [16]: if True : print("first") if True else print("second")
first
In [17]: if True : print("first") if False else print("second")
second
In [18]: if False : print("first") if True else print("second")
[nothing]
In [19]: if False : print("first") if False else print("second")
[nothing]
What do you call this?
I'm surprised you can just take out the second positive case for if...then...else and turn it into if...else.
UPDATE: Sorry bout the python3 noob mistake, I just wasn't paying attention. As noted, the answers don't make sense without the mistake, so I've striked out the erroneous code.
You have found the ternary operator, which is known as a Conditional Expression in Python. The expression x if condition else y means that if the condition (which can be a complex statement or function) evaluates to True, the expression returns x, and if the condition evaluates to False, the expression returns y.
It works like the following if-statement:
if test1:
if test2:
print(x)
else:
print(y)
Your error stems from not wrapping the print function arguments in a parentheses. This is a change made to Python 3, whereas in Python 2, your syntax would have been fine. Rewrite it to look like:
if test1: print(x if test2 else y)
and your error will go away.
I feel it is important to point out that what you describe (if-if-else) is not a conditional expression per se, but it does include one:
1 if S == 0 else 0 is a conditional expression
if i >= len(v): return 1 if S == 0 else 0 is a compound statement which comprises a simple if statement with a conditional expression.
So, if the first if evaluates to True, then the conditional expression will be evaluated and the appropriate element (in this case 1 or 0) returned to the preceding statement (return, here).
It is an if-expression: a if condition else b means: if condition is true, then the expression has value a, otherwise b
Your problem is unrelated, Seems you are using python 3, you got the example from python 2. In python 3 print is a function, so just add parentheses
if you are using python of version >3, then print should have parenthesis i.e ()

Putting an if-elif-else statement on one line?

I have read the links below, but it doesn't address my question.
Does Python have a ternary conditional operator? (the question is about condensing if-else statement to one line)
Is there an easier way of writing an if-elif-else statement so it fits on one line?
For example,
if expression1:
statement1
elif expression2:
statement2
else:
statement3
Or a real-world example:
if i > 100:
x = 2
elif i < 100:
x = 1
else:
x = 0
I just feel if the example above could be written the following way, it could look like more concise.
x = 2 if i>100 elif i<100 1 else 0 # [WRONG]
No, it's not possible (at least not with arbitrary statements), nor is it desirable. Fitting everything on one line would most likely violate PEP-8 where it is mandated that lines should not exceed 80 characters in length.
It's also against the Zen of Python: "Readability counts". (Type import this at the Python prompt to read the whole thing).
You can use a ternary expression in Python, but only for expressions, not for statements:
>>> a = "Hello" if foo() else "Goodbye"
Edit:
Your revised question now shows that the three statements are identical except for the value being assigned. In that case, a chained ternary operator does work, but I still think that it's less readable:
>>> 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
If you only need different expressions for different cases then this may work for you:
expr1 if condition1 else expr2 if condition2 else expr
For example:
a = "neg" if b<0 else "pos" if b>0 else "zero"
Despite some other answers: YES it IS possible:
if expression1:
statement1
elif expression2:
statement2
else:
statement3
translates to the following one liner:
statement1 if expression1 else (statement2 if expression2 else statement3)
in fact you can nest those till infinity. Enjoy ;)
Just nest another if clause in the else statement. But that doesn't make it look any prettier.
>>> x=5
>>> x if x>0 else ("zero" if x==0 else "invalid value")
5
>>> x = 0
>>> x if x>0 else ("zero" if x==0 else "invalid value")
'zero'
>>> x = -1
>>> x if x>0 else ("zero" if x==0 else "invalid value")
'invalid value'
There's an alternative that's quite unreadable in my opinion but I'll share anyway just as a curiosity:
x = (i>100 and 2) or (i<100 and 1) or 0
More info here: https://docs.python.org/3/library/stdtypes.html#boolean-operations-and-or-not
You can optionally actually use the get method of a dict:
x = {i<100: -1, -10<=i<=10: 0, i>100: 1}.get(True, 2)
You don't need the get method if one of the keys is guaranteed to evaluate to True:
x = {i<0: -1, i==0: 0, i>0: 1}[True]
At most one of the keys should ideally evaluate to True. If more than one key evaluates to True, the results could seem unpredictable.
yes you can by doing this :
i = int(input('type your num here : '))
x = 2 if i > 100 else ( 1 if i < 100 else 0)
print (x)
if i > 100:
x = 2
elif i < 100:
x = 1
else:
x = 0
If you want to use the above-mentioned code in one line, you can use the following:
x = 2 if i > 100 else 1 if i < 100 else 0
On doing so, x will be assigned 2 if i > 100, 1 if i < 100 and 0 if i = 100
The ternary operator is the best way to a concise expression. The syntax is variable = value_1 if condition else value_2. So, for your example, you must apply the ternary operator twice:
i = 23 # set any value for i
x = 2 if i > 100 else 1 if i < 100 else 0
Nested ternary operator is the best solution --
Example case -
4 = 1
3 = 2
2 = 3
1 = 4
a = 4
prio = 4 if a == 1 else (3 if a == 2 else (2 if a == 3 else 1))
People have already mentioned ternary expressions. Sometimes with a simple conditional assignment as your example, it is possible to use a mathematical expression to perform the conditional assignment. This may not make your code very readable, but it does get it on one fairly short line. Your example could be written like this:
x = 2*(i>100) | 1*(i<100)
The comparisons would be True or False, and when multiplying with numbers would then be either 1 or 0. One could use a + instead of an | in the middle.
It also depends on the nature of your expressions. The general advice on the other answers of "not doing it" is quite valid for generic statements and generic expressions.
But if all you need is a "dispatch" table, like, calling a different function depending on the value of a given option, you can put the functions to call inside a dictionary.
Something like:
def save():
...
def edit():
...
options = {"save": save, "edit": edit, "remove": lambda : "Not Implemented"}
option = get_input()
result = options[option]()
Instead of an if-else:
if option=="save":
save()
...
You can use nested ternary if statements.
# if-else ternary construct
country_code = 'USA'
is_USA = True if country_code == 'USA' else False
print('is_USA:', is_USA)
# if-elif-else ternary construct
# Create function to avoid repeating code.
def get_age_category_name(age):
age_category_name = 'Young' if age <= 40 else ('Middle Aged' if age > 40 and age <= 65 else 'Senior')
return age_category_name
print(get_age_category_name(25))
print(get_age_category_name(50))
print(get_age_category_name(75))
MESSAGELENGHT = 39
"A normal function call using if elif and else."
if MESSAGELENGHT == 16:
Datapacket = "word"
elif MESSAGELENGHT == 8:
Datapacket = 'byte'
else:
Datapacket = 'bit'
#similarly for a oneliner expresion:
Datapacket = "word" if MESSAGELENGHT == 16 else 'byte' if MESSAGELENGHT == 8 else 'bit'
print(Datapacket)
Thanks

Categories

Resources