Reputation: 31
I recently saw this if-else short hand in python and wondered is there a way to include multiple conditions and block statements in the if-else shorthand, if required.
a=int(input())
b=int(input())
print(a) if (a>b) else print("Equal") if (a==b) else print(b)
Can Something like the below code can be written in short-hand?
a=int(input())
b=int(input())
if (a>b):
print(a)
elif(a==b):
print("Equal")
elif(b>0) and (a>0):
c=a+b
print(c)
else:
print("#")
PS: The code is arbitrary just to include multiple conditions and statements.
Upvotes: 0
Views: 1339
Reputation: 105
If you want to write your block of code in a single line then this should work -
a=int(input())
b=int(input())
print(a) if (a>b) else print("Equal") if (a==b) else print(a+b) if (b>0) and (a>0) else print("#")
Although this doesn't reduce the codes in the original block of yours.
Upvotes: 1