Code Golf Techniques — Core Concepts

Why code golf techniques matter

Code golf is not about writing production code. It is about understanding a language so deeply that you can exploit every shortcut it offers. The techniques you learn carry over to real work — not as patterns to copy, but as mental models of how Python evaluates expressions, handles types, and organises its standard library.

Competitive code golf happens on sites like Code Golf Stack Exchange, Anarchy Golf, and code.golf. Players submit solutions and the shortest (in bytes) wins.

The essential toolbox

Shorter variable names

Every character counts. Use single-letter names, and prefer names that double as part of expressions:

# Normal
total = sum(numbers)
# Golf
t=sum(n)

Semicolons over newlines

Python allows multiple statements on one line with ;. This saves newline + indentation characters:

a=1;b=2;print(a+b)

Unpacking and starred expressions

# Instead of:
a=input().split();x=a[0];y=a[1]
# Use:
x,y=input().split()

Walrus operator (:=)

Assign and use a value in the same expression:

# Without walrus
n=int(input())
print(n*2)
# With walrus
print((n:=int(input()))*2)

Lambda as a function

Lambda saves characters over def:

# def version (29 chars)
def f(x):return x*x+2*x+1
# lambda version (22 chars)
f=lambda x:x*x+2*x+1

exec and eval

When you need a loop in a one-liner:

exec("print('hi');"*10)

This prints “hi” ten times without writing a for loop.

Common tricks by category

CategoryTrickExample
StringsMultiply to repeat"ab"*3"ababab"
ListsUnpacking with *[*range(5)][0,1,2,3,4]
BooleansTrue is 1, False is 03+True4
Math// for integer division7//23
I/Oopen(0).read() reads stdinShorter than sys.stdin.read()
Iterationmap returns iterator, no bracketsprint(*map(int,"1 2 3".split()))

Thinking like a golfer

The key mental shift: stop thinking about readability and start thinking about character equivalences. Is there a built-in that does what your three-line loop does? Can you replace an if/else with a conditional expression? Can you use string multiplication instead of a loop?

Example: print numbers 1 to 100, replacing multiples of 3 with “Fizz”, 5 with “Buzz”, both with “FizzBuzz”.

# Standard (115 chars)
for i in range(1,101):
    s=""
    if i%3==0:s+="Fizz"
    if i%5==0:s+="Buzz"
    print(s or i)

# Golfed (58 chars)
for i in range(1,101):print("Fizz"*(i%3<1)+"Buzz"*(i%5<1)or i)

The golfed version exploits string multiplication by a boolean (0 or 1) and the or operator’s short-circuit behaviour.

Common misconception

Many people think code golf is pointless because the code is unreadable. The readability is not the point. The value is in the process — the deep exploration of language features, operator precedence, and standard library corners that you would never visit in daily work. Competitive golfers consistently report that the habit makes them better programmers in their day jobs.

One thing to remember: Code golf is reverse engineering your language. Every character you remove teaches you something about how Python actually works under the hood.

pythonpuzzlesprogramming-challenges

See Also

  • Ci Cd Why big apps can ship updates every day without turning your phone into a glitchy mess — CI/CD is the behind-the-scenes quality gate and delivery truck.
  • Containerization Why does software that works on your computer break on everyone else's? Containers fix that — and they're why Netflix can deploy 100 updates a day without the site going down.
  • Python 310 New Features Python 3.10 gave programmers a shape-sorting machine, friendlier error messages, and cleaner ways to say 'this or that' in type hints.
  • Python 311 New Features Python 3.11 made everything faster, error messages smarter, and let you catch several mistakes at once instead of stopping at the first one.
  • Python 312 New Features Python 3.12 made type hints shorter, f-strings more powerful, and started preparing Python's engine for a world without the GIL.