Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.020 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; '(python': 0.07; '(so': 0.07; 'string': 0.09; 'expression:': 0.09; 'subject:How': 0.10; 'translate': 0.10; 'def': 0.12; '"o"': 0.16; 'between.': 0.16; 'consonant': 0.16; 'exercise:': 0.16; 'helps.': 0.16; 'literal,': 0.16; 'substituted': 0.16; 'header:User-Agent:1': 0.23; 'guys': 0.24; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'function': 0.29; 'character': 0.29; "doesn't": 0.30; "i'm": 0.30; 'code': 0.31; 'question:': 0.31; 'text': 0.33; 'but': 0.35; "i'll": 0.36; 'should': 0.36; 'example,': 0.37; 'step': 0.37; 'thank': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'how': 0.40; 'hope': 0.61; 'making': 0.63; 'evaluate': 0.72; 'subject:this': 0.83; 'pardon': 0.84; 'received:195.238': 0.84; 'received:195.238.6': 0.84; 'received:belgacom.be': 0.84; 'received:isp.belgacom.be': 0.84 X-Belgacom-Dynamic: yes X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AtgIAOW41VFbtpxX/2dsb2JhbAANTcFOgm8DAQKBGYMYAQEEMgEFUQshJQ8CRhMIAq9XiXyIBo8MZhaDVwOXSZRY Date: Thu, 04 Jul 2013 20:05:57 +0200 From: Antoon Pardon User-Agent: Mozilla/5.0 (X11; Linux i686; rv:10.0.12) Gecko/20130116 Icedove/10.0.12 MIME-Version: 1.0 To: python-list@python.org Subject: Re: How is this evaluated References: <81f466df-5203-48a7-97a8-643b70d9dd16@googlegroups.com> In-Reply-To: <81f466df-5203-48a7-97a8-643b70d9dd16@googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 54 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1372961165 news.xs4all.nl 15876 [2001:888:2000:d::a6]:35010 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:49891 Op 04-07-13 19:20, Arturo B schreef: > I'm making this exercise: (Python 3.3) > > Write a function translate() that will translate a text into "rövarspråket" (Swedish for "robber's language"). That is, double every consonant and place an occurrence of "o" in between. For example, translate("this is fun") should return the string "tothohisos isos fofunon". > > So I tried to solved it, but I couldn't, so I found many answers, but I selected this: > > def translate(s): > consonants = 'bcdfghjklmnpqrstvwxz' > return ''.join(l + 'o' + l if l in consonants else l for l in s) > > print(translate('hello code solver')) > > > OUTPUT: > 'hohelollolo cocodode sosololvoveror' > > ______________________________________________________________ > So I want to question: > How is the > > if 'h' in consonants else 'h' for 'h' in s > > part evaluated? (step by step please :P ) > > ''.join('h' + 'o' + 'h' if 'h' in consonants else 'h' for 'h' in s) > > Thank you guys This doesn't make much sence because you substituted a varible for a character literal, so I'll go with the original. l + 'o' + l if l in consonants else l for l in s Evaluate this expression: l + 'o' + l if l in consonants else l for each l in s (so for each letter that in s). l + 'o' + l if l in consonants else l if l is part of the consonants produce l + 'o' + l otherwise just produce l. Hope this helps. -- Antoon Pardon