Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the Python equivalent of x = (10<n) ? 10 : n;

Tags:

python

ternary

I was wondering what the equivalent in python to this would be:

n = 100
x = (10 < n) ? 10 : n;
print x;

For some reason this does not work in Python. I know I can use an if statement but I was just curious if there is some shorter syntax.

Thanks.

like image 265
David Avatar asked Nov 28 '22 07:11

David


1 Answers

x = min(n, 10)

Or, more generally:

x = 10 if 10<n else n
like image 141
Thomas K Avatar answered Dec 08 '22 06:12

Thomas K