In Python, suppose I have a function:
def func(x):
if x%2 == 0:
j = x/2
print(j)
else:
j = x
print(j)
Do not mind the logic of the code, it's just a simple example.
The blocks of if and else are almost identical. Is there any way to avoid this repetition that, if frequent in code, can make it non-elegant? Something like:
def func(x):
j = x/2, if x%2 == 0, else j=x
print(j)