0

This could obviously be done with an if-elif-else logic, but I wish to ask for a better - more pythonic way to implement it. eg.

a,b = 14,73
if a>b:
    print('a')
elif a<b:
    print('b')
else:
    print('a=b') #not_required_though
AanisNoor
  • 25
  • 3
  • 1
    you can use dictionary – deadshot Sep 21 '20 at 06:07
  • 1
    if you need the variable name, most likely there is problem with your design ([Keep data out of your variable names](https://nedbatchelder.com/blog/201112/keep_data_out_of_your_variable_names.html). As @deadshot mention - you may use `dict` for example. – buran Sep 21 '20 at 06:13

1 Answers1

1

You can do if else in one line

print('a' if a>b else 'b')

or you can also assign it to variable

res = 'a' if a>b else 'b' if b>a else 'a==b'
Deadpool
  • 33,221
  • 11
  • 51
  • 91