Zen of Python

TrustyTony 0 Tallied Votes 518 Views Share

From
http://stackoverflow.com/questions/228181/zen-of-python

Tells you what is Zen of Python

Actually this is enough to print the Truth:

import this

Loop version from http://stackoverflow.com/questions/3559124/dissecting-a-line-of-obfuscated-python

result = []
for c in this.s: ## fixed tonyjv
   if c in this.d:
      result.append(this.d[c])
   else:
      result.append(c)
print "".join(result)
import this
whatiszenofpython= "".join([c in this.d and this.d[c] or c for c in this.s])
TrustyTony 888 pyMod Team Colleague Featured Poster

Did my own version (generator and string concatenation):

import this ## prints zenofpython
print '-'*70
whatiszenofpython = "".join(this.d[c] if c in this.d else c for c in this.s)
zen = ''
for c in this.s:
    zen += this.d[c] if c in this.d else c
print zen
ultimatebuster 14 Posting Whiz in Training

already knew this

also

from __future__ import braces
jhrhew 0 Newbie Poster

The right way is
>>> print ''.join(this.d.get(c,c) for c in this.s)
as done in this.py file.

TrustyTony 888 pyMod Team Colleague Featured Poster

It is same as mine.. But I produce default value with logical operations or if..else instead of using default parameter of get. It is handy to remember the get alternative, of course.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.