Simplify one line if statement

if-statement, python

Solution

The following is 39% shorter and in my opinion is simpler and more pythonic than other answers. But we should note that sometimes people get it wrong thinking that 1 is a lower bound being confused by `min` function when actually 1 is an upper bound for `var`.

var = min(var, 1.0)

Problem

I have a one liner `if` statement that looks like this: ``` var = var if var < 1. else 1. ``` The first part `var = var` looks a bit ugly and I'd bet there's a more pythonic way to say this.

Original source

Related problems