Skip to content

Instantly share code, notes, and snippets.

@shervinafshar
Last active April 27, 2021 19:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shervinafshar/11278760 to your computer and use it in GitHub Desktop.
Save shervinafshar/11278760 to your computer and use it in GitHub Desktop.
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)'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment