Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'python.': 0.02; '(at': 0.04; 'string': 0.09; 'beginners': 0.09; 'indeed,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'runs': 0.10; 'python': 0.11; 'def': 0.12; 'windows': 0.15; 'behaviour.': 0.16; 'characters:': 0.16; 'different;': 0.16; 'finney': 0.16; 'invisible': 0.16; 'iteration': 0.16; 'looping': 0.16; 'loops': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'space)': 0.16; 'tab': 0.16; 'tutoring': 0.16; 'url:tutor': 0.16; ':-)': 0.16; 'code.': 0.18; 'basically': 0.19; 'separate': 0.22; 'header:User-Agent:1': 0.23; "aren't": 0.24; 'question': 0.24; 'define': 0.26; 'header:X-Complaints-To:1': 0.27; 'character': 0.29; 'characters': 0.30; 'is?': 0.30; 'url:mailman': 0.30; 'indentation': 0.31; 'writes:': 0.31; 'file': 0.32; 'run': 0.32; 'text': 0.33; 'url:python': 0.33; 'something': 0.35; 'definition': 0.35; 'objects': 0.35; 'there': 0.35; 'curious': 0.36; 'false': 0.36; 'returning': 0.36; 'url:listinfo': 0.36; 'url:org': 0.36; 'should': 0.36; 'subject:New': 0.37; 'too': 0.37; 'easily': 0.37; 'starting': 0.37; 'skip:o 20': 0.38; 'ben': 0.38; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'changed': 0.39; 'received:org': 0.40; 'space': 0.40; 'url:mail': 0.40; 'how': 0.40; 'skip:u 10': 0.60; 'easy': 0.60; 'is.': 0.60; 'forum': 0.61; 'url:3': 0.61; 'simple': 0.61; 'different': 0.65; 'here': 0.66; 'subject:. ': 0.67; 'products': 0.71; 'subject:For': 0.78; '2002': 0.81; 'engineered': 0.84; 'fin': 0.84; 'received:125': 0.84; 'shock': 0.84; 'treating': 0.84; 'differences': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Ben Finney Subject: Re: New to Python. For in loops curiosity Date: Wed, 14 May 2014 14:03:08 +1000 References: <2f08e970-1334-4e7f-ba84-14869708a73b@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: jigong.madmonks.org X-Public-Key-ID: 0xAC128405 X-Public-Key-Fingerprint: 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405 X-Public-Key-URL: http://www.benfinney.id.au/contact/bfinney-pubkey.asc X-Post-From: Ben Finney User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.4 (gnu/linux) Cancel-Lock: sha1:lQ93ZL82i8u/Zk0md2W4pwj8yGo= 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: 60 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1400040202 news.xs4all.nl 2923 [2001:888:2000:d::a6]:46717 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:71531 Leonardo Petry writes: > So I am starting with python and I have been working on some simple > exercises. You are welcome here. Congratulations on starting with Python! You may also be interested to know there is also a separate forum dedicated to tutoring beginners in Python. > Here is something I found curious about python loops > > This loop run each character in a string > > def avoids(word,letters): > flag = True > for letter in letters: > if(letter in word): > flag = False > return flag You should avoid using U+0009 TAB characters for indentation, since they render inconsistently and can easily result in invisible differences in changed code. Instead, use four-column indentation with space (U+0020 SPACE) characters. > The loop below (at the bottom) runs each line of the file > > fin = open('wordplay.txt'); > user_input = raw_input('Enter some characters: ') > count = 0 > for line in fin: > word = line.strip() > if(avoids(word, user_input)): > count += 1; > > This is just too convenient. Is that a complaint, or shock at how easy it is? :-) > Basically my question is: Why is python not treating the contents of > wordplay.txt as one long string and looping each character? Because the types of the objects are different; different types define different behaviour. Indeed, that is almost the definition of what a type is. A text string object supports iteration by returning each character . A file object supports iteration by returning each line from the stream . -- \ “Our products just aren't engineered for security.” —Brian | `\ Valentine, senior vice-president of Microsoft Windows | _o__) development, 2002 | Ben Finney