Re: [Tutor] How does slicing work?
On 16/06/12 03:48, Kanone Seele wrote: Hello,I'm a beginner in python.I found some qustions in Chapter 9's Quiz of the book/Learning Python,4th ed/, There's a bunch of math behind slicing but one of the best descriptions I found when I was learning Python was to think of the slice action as a pair of blades that slice between the list entries. Thus zero is before the first entry. -1 is one back from the end - ie the last element. So applying that to your examples lets see what we get: L = [1, 2, 3, 4] L[-1000:100] [1, 2, 3, 4] One blade 1000 elements back from the end, the other 100 elements forward from the beginning. Between the blades we have the whole list... L[3:1] [] First blade in front of the second blade doesn't work. You can only cut 'backwards' using negative indexes (or the third index argument) eg: >>> L[-3:-1] [2,3] >>> L[3:1:-1] [4,3] Back to your examples: L[3:1] = ['?'] L [1, 2, 3, '?', 4] OK, This one is a bit more confusing. The [3:1] slice above returns an empty slice between 3 and 4. The assignment operation replaces the slice with the assigned list. So in this case we replace the empty slice with the question mark. Here are some more simple examples working up to yours: L = [1,2,3,4] L[:0] = [-1] # insert before list L [-1, 1, 2, 3, 4] L[-1000:0]=[-2] # and again L [-2, -1, 1, 2, 3, 4] L[100:]=[5] # insert after list L [-2, -1, 1, 2, 3, 4, 5] L[3:6] = [6] # replace multiple members L [-2, -1, 1, 6, 5] L = [1,2,3,4] # reset list L[3:3] # empty slice [] L[3:3]=[3.5] # insert after 3 L [1, 2, 3, 3.5, 4] L[3:1]=['?'] # your example is effectively the same L [1, 2, 3, '?', 3.5, 4] HTH, -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] getpass
On 16/06/12 06:44, Mike Nickey wrote: "Prompt the user for a password without echoing." -- http://www.python.org/doc//current/library/getpass.html However when I run it in Eclipse, the password is clear as day. Remember Eclipse is a development environment not aruntime environment. Like all IDEs it captures the stdin/stdout activity and displays 9or interprets!) it in its own way. Any time you are running I/O sensitive code (including GUIs) and you hit something strange always try it in the vanilla interpreter to make sure its not the IDE trying to be 'helpful' (or even just being buggy!!) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How does slicing work?
Thank you guys~ the two blades method really works well! And I think it'd be better to dive into the source code of Pytho.Well,it seems difficult for me now. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How does slicing work?
On 16/06/2012 13:16, Kanone Seele wrote: Thank you guys~ the two blades method really works well! And I think it'd be better to dive into the source code of Pytho.Well,it seems difficult for me now. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor I suggest you read the tutorial before starting on source code. -- Cheers. Mark Lawrence. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Simple Python Address Book (Advice welcome!)
Hello All, I'm currently in the midst of learning Python, and am absolutely loving the language thus far! I'm a .NET developer in my professional life, and have been recently influenced to learn Python - and I'm glad I was :) I am working on an address book application (input through raw_input(), nothing fancy) in the 2.7 redist. I'm essentially asking if someone wouldn't mind taking a look at what I have so far, and giving some advice on some of my weak areas, and areas I've marked with comments on how to better approach some of my solutions (in a more *Pythological* way :) ). I'm still familiarizing myself with Python, and I would greatly appreciate anyone's help in this task of mine :) Just a learning project for myself, one of many! I'm not sure how you would like the code, I'll simply paste it here since I imagine people are wear of opening attachments through email: import re # email validation import textwrap #wrapping text in raw_input (not sure if this is best way to do this?) # forward declarations addressBookPath = r'C:\temp\addresses.txt' addressBook = {} # address book held in memory before dumped to file emailFormatRegex = r'(\w[\w]*)@([\w]+\.[\w]+)' recordDelimiter = ' | ' # split name | email in file def LoadAddresses(): """ Loads all addresses from file and places them in tuple in form (bool, list), where the list is each row from the file (a person's name | email). """ success, lines = (False, []) try: f = open(addressBookPath, 'r') lines = f.readlines() f.close() success, lines = (True, lines) except IOError as e: print 'Error opening address book: ', e return (success, lines) def main(): (success, lines) = LoadAddresses() if not success: shouldMakeNewBook = raw_input(textwrap.fill("""You do not have an address book yet. Would you like to create one?""")) if shouldMakeNewBook in ('y', 'ye', 'yes'): f = open(addressBookPath, 'w') f.close() print 'New address book created.', addressBookPath else: print 'Exiting...' return else: # now that we have the file loaded into memory, # fill out the addressbook from file for line in lines: splitStr = line.split(recordDelimiter) addressBook[splitStr[0]] = splitStr[-1] # main input loop (break with 'q' or 'quit' during input) while True: newPersonNameInput = raw_input('Enter new person\'s name: (q/quit to stop):') if newPersonNameInput.lower() in ('q', 'quit'): break addressBook[newPersonNameInput] = newPersonNameInput while True: # loop until email is in valid format (x@y.z) newPersonEmailInput = raw_input('Enter new person\'s email: (q/quit to stop):') match = re.search(emailFormatRegex, newPersonEmailInput) if not match: print 'email validation failed... try again.' continue else: addressBook[newPersonNameInput] = newPersonEmailInput break # success RecordAddresses() print addressBook def RecordAddresses(): """ Writes out each address to the file in the form of name | email. """ f = open(addressBookPath, 'w') for k, v in sorted(addressBook.items()): #is there a better way to do this without placing a newline after each row? #it's causing multiple line separations when writing back out to file (because each # time we finish reading, it ends with a trailing newline). f.write("{0}{1}{2}\n".format(k, recordDelimiter, v)) f.close() if __name__ == '__main__': main() I appreciate any feedback! -catch ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor