shervinafshar
4/25/2014 - 5:43 AM

Adventures in Pythonland.

Adventures in Pythonland.

Python Matrices:

M = [[1,2,3], [4,5,6], [7,8,9]]
# diagonal
[M[i][i] for i in len(0, len(M))]
# reverse diagonal
[M[i][-(i+1)] for i len(0, len(M)]

Translating strings

from string import maketrans
inTable = "abcde"
outTable = "12345"
t = maketrans(inTable, outTable)
'abracadabrace'.trabslate(t)    # '12r131412r135'

Is it a prime?

n = 356 - 189; print(len([i for i in range(2, n / 2 + 1) if n % i == 0])) == 0 # It is A PRIME!

Run no REPL

python -c 'print(True);print(False)'