96
votes
Tips for golfing in Python
Store lookup tables as magic numbers
Say you want to hardcode a Boolean lookup table, like which of the first twelve English numbers contain an n.
...
87
votes
Tips for golfing in Python
Collapse two numerical loops into one
Say you're iterating over the cells of an m*n grid. Instead of two nested for loops, one ...
75
votes
61
votes
How on earth did llhuii output the Evil Numbers in 42 bytes of Python?
This isn't the same solution as llhuii's, but it's also 42 bytes long.
n=0;exec'print n;n^=(n^n+2)%3/2;n+=2;'*400
Try it online!
Thanks to @JonathanFrech, we're ...
59
votes
Tips for golfing in Python
Extended iterable unpacking ("Starred assignment", Python 3 only)
The best way to explain this is via an example:
...
42
votes
Tips for golfing in Python
Choosing one of two numbers based on a condition
You already know to use the list selection [x,y][b] with a Boolean b for the ...
42
votes
How on earth did llhuii output the Evil Numbers in 42 bytes of Python?
Getting 39 bytes
This is an explanation of how I got a 39-byte solution, which Dennis and JonathanFrech found separately as well. Or, rather, it explains how one could arrive at the answer in ...
40
votes
Tips for golfing in Python
Use ~ to index from the back of a list
If L is a list, use L[~i] to get the i'th element ...
40
votes
Smallest python script to print even numbers 0 to 100
Python 2 - 12 characters
print 8**999
The decimal representation of all even numbers from 0 to 100 can be found in the output:
...
37
votes
Primality testing formula
43 bytes
(4**n+1)**n%4**n**2/n&2**(2*n*n+n)/-~2**n<1
Try it online!
The method is similar to Dennis' second (deleted) answer, but this answer is easier ...
36
votes
Accepted
How can I use cmp(a,b) with Python3?
Python 3 does not have cmp. For golfing, you can do
11 chars
(a>b)-(a<b)
which loses 3 chars over ...
33
votes
Tips for golfing in Python
PEP448 – Additional Unpacking Generalizations
With the release of Python 3.5, manipulation of lists, tuples, sets and dicts just got golfier.
Turning an iterable into a set/list
Compare the pairs:
...
33
votes
Tips for golfing in Python
Ceil and Floor
If you ever want to get the rounded-up result for a division, much like you'd do with // for floor, you could use ...
31
votes
Smallest python script to print even numbers 0 to 100
Python 3, 22 (Possibly not allowed)
If the challenge is "to create a python script which prints the even numbers from 0 to 100" and not "to create a python script which prints the even ...
31
votes
Tips for restricted source in Python
Avoid "normal" letters
Identifiers are normalized by the Python 3 parser. This implies that cursive (Unicode) letters such as 𝓪𝓫𝓬𝓓𝓔𝓕 are interpreted ...
28
votes
Tips for golfing in Python
Exploit Python 2 string representations
Python 2 lets you convert an object x to its string representation `x` at a cost of ...
28
votes
Tips for golfing in Python
Length tradeoff reference
I've think it would be useful to have a reference for the character count differences for some common alternative ways of doing things, so that I can know when to use which. ...
26
votes
Is there a shorter way to assign one of two variables in Python?
12 chars/assignment + 9 chars of overhead
V=vars() # do once at the start of the program
V["xy"[b]]=z
Note that this only works at global scope, it does not ...
26
votes
Accepted
How to solve the LCM in 50 bytes of Python
My solution to 50 bytes:
v=1
for x in input().split()*6**6:v+=-v%int(x)
print(v)
Explanation
The solution itself is relatively simple, but finding it was ...
25
votes
Tips for golfing in Python
Common helper functions
These are some golfed implementations of number theoretic functions that come up in challenges a lot. Many of these are due to xnor, especially the “Wilson’s theorem prime ...
25
votes
Tips for restricted source in Python
Avoid Numbers with Booleans
When performing arithmetic operations on Booleans, Python treats them as if they are the numbers 1 and 0. So for example
...
Community wiki
23
votes
Tips for golfing in Python
Arithmetic tricks
Here are some arithmetic tricks which are either shorter or are more useful due to precedence rules.
...
23
votes
Tips for golfing in Python
Printing a string without a trailing newline in Python 3
Suppose you have a string s, and need to print it without a trailing newline. The canonical way of doing ...
23
votes
Tips for golfing in Python
Assignment expressions
Assignment expressions are a powerful language feature introduced in Python 3.8 (TIO). Use the "walrus operator" := to assign a variable ...
23
votes
Smallest python script to print even numbers 0 to 100
Python2 26
i=0;exec"print i;i+=2;"*51
independent discovery of @bitpwner's solution
22
votes
Tips for restricted source in Python
Avoid Parens with List indexing
Parentheses are super useful for creating the correct operator precedence so it is a bummer when they are banned. However if [] are ...
Community wiki
21
votes
Tips for golfing in Python
The shortest infinite for comprehension
You may know the trick to easily create an infinite generator using the two-argument form of iter:
...
21
votes
Accepted
Golf Practice: Python
Total: 104 101 99 chars
Problem 1
Problem 2
Problem 3
Problem 4
Problem 5
Problem 6
Problem 7
Problem 8
Problem 9
Problem 10
21
votes
Accepted
FOIL Python's strong typing!
54 52 50 49 48 45 39 bytes
Removed 4 bytes thanks to Dennis.
The latest version is inspired by the "some reason" in xnor's answer.
...
21
votes
A game of dice, but avoid number 6
NeoBot
Instead, only try to realize the truth - there is no spoon
NeoBot peeks into the matrix (aka random) and predicts if the next roll will be a 6 or not - it can't do anything about being handed ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
python × 130code-golf × 78
tips × 58
king-of-the-hill × 24
game × 8
math × 5
string × 4
code-challenge × 4
popularity-contest × 4
primes × 4
fastest-code × 4
optimization × 4
programming-puzzle × 4
restricted-source × 3
palindrome × 3
card-games × 3
board-game × 3
kolmogorov-complexity × 2
decision-problem × 2
geometry × 2
cops-and-robbers × 2
test-battery × 2
error-message × 2
jelly × 2
array × 2