Comments on Tips for golfing in Python
Parent
Tips for golfing in Python
If you have any tips for golfing in Python, add them as answers to this post.
Use the unpacking `` instead o …
3y ago
If you print some string `s` w …
3y ago
Reorder expressions in order t …
3y ago
Combine conditionals Python …
3y ago
Replace `n+1` with `-n` and `n …
3y ago
Replace if/else expressions by …
3y ago
Use `a+=[b]` instead of `a.app …
3y ago
` == and` If you want to ch …
3y ago
Combine loops Suppose you h …
3y ago
Replace `range()` if $n …
3y ago
Assign `float`s while leaving …
3y ago
Renaming functions A funny, …
3y ago
Use `lambda`s instead of funct …
3y ago
`import` You can import som …
3y ago
Replace print loops by list un …
3y ago
Replace `for` loops with strin …
3y ago
If you have a loop or `if`, yo …
3y ago
`| == or` This bitwise oper …
3y ago
Post
Combine conditionals
Python has its fair share of comparison operators and can actually be used in a single conditional. For example:
x == 2 and y == 2
can be:
x==y==2
Used on Make $2 + 2 = 5$.
@celtschk also mentions that different operators can also enter a single conditional, as long as an order is applied.
For example:
a < b and b > c
Can be rewritten as:
a<b>c
0 comment threads