# Tag Info

### 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. ...

### 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 ...

### Tips for golfing in Python

For integer n, you can write n+1 as -~n n-1 as ...

### 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 ...

### Tips for golfing in Python

Extended iterable unpacking ("Starred assignment", Python 3 only) The best way to explain this is via an example: ...

### 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 ...

### 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 ...

### 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 ...

### 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: ...

### 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 ...
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 ...

### 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: ...

### 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 ...

### 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 ...

### 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 ...

### 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 ...

### 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. ...

### 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 ...
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 ...

### 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 ...

### 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 ...

### Tips for golfing in Python

Arithmetic tricks Here are some arithmetic tricks which are either shorter or are more useful due to precedence rules. ...

### 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 ...

### 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 ...

### 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

### 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 ...

### 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: ...
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