Re: [Tutor] python books
I'd recommend Core Python Programming by Wesley Chun.. On Sat, Apr 25, 2009 at 9:45 PM, Dayo Adewunmi contactd...@gmail.comwrote: chinmaya wrote: On Mon, Apr 13, 2009 at 11:07 PM, sudhanshu gautam sudhanshu9...@gmail.com mailto:sudhanshu9...@gmail.com wrote: I am new in python , so need a good books , previously read python Bible and swaroop but not satisfied . so tell me good books in pdf format those contents good problems also ___ Tutor maillist - Tutor@python.org mailto:Tutor@python.org http://mail.python.org/mailman/listinfo/tutor I would say start with python tutorial, its nice decent starting material. There is no better way to learn language than to practice it as you read. Most of the tutorials out there are not written for 3.0, so you may want to install 2.6. I also recommend Dive Into python, its very beginner friendly, but remember it does not cover all (not all major) libraries never-the-less its one of the best beginner tutorial. Also install ipython its very powerful. And once you learn the interface its very easy to find documentation and library references. Also you can look at 100s of python videos in showmedo.com http://showmedo.com -- chinmaya sn ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor I'm currently reading Think Python http://www.greenteapress.com/thinkpython/thinkpython.html Regards Dayo --- ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- Lloyd Dube ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Working with lines from file and printing to another keeping sequential order
On Sat, Apr 25, 2009 at 2:11 PM, Dan Liang danlian...@gmail.com wrote: Hi Bob and tutors, Thanks Bob for your response! currently I have the current code, but it does not work: ListLines= [] for line in open('test.txt'): line = line.rstrip() ListLines.append(line) This could be written with a list comprehension: ListLines = [ line.rstrip() for line in open('test.txt') ] for i in range(len(ListLines)): if ListLines[i].endswith(yes) and ListLines[i+1].endswith(no) and ListLines[i+1].endswith(no): print ListLines[i], ListLines[i+1], ListLines[i+2] elif ListLines[i].endswith(yes) and ListLines[i+1].endswith(no): print ListLines[i], ListLines[i+1] elif ListLines[i].endswith(yes): print ListLines[i] elif ListLines[i].endswith(no): continue else: break You only need to test for ListLines[i].endswith('yes') once. Then you could use a loop to test for lines ending with 'no'. for i in range(len(ListLines)): if not ListLines[i].endswith('yes'): continue for offset in (1, 2): if i+offset len(ListLines) and ListLines[i+offset].endswith('no'): print ListLines[i+offset] You could adapt the above for a variable number of 'no' lines with something like for offset in range(1, maxNo+1): I get the following error: Traceback (most recent call last): File test.py, line 18, in module if ListLines[i].endswith(yes) and ListLines[i+1].endswith(no) and ListLines[i+1].endswith(no): IndexError: list index out of range That is because you have a 'yes' line at the end of the file so the check for 'no' tries to read past the end of ListLines. In my code the test for i+offset len(ListLines) will prevent that exception. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Lists in a file
Hi, I am trying to create a program where I open a file full of lists and process them. However when the program does not recognize the lists as lists. Here is the relevant code :- def open_filedef(): text_file =open (voteinp.txt,r) lines = text_file.readlines() for line in lines: print line print line[0] text_file.close() And an example of the type of text :- ['a','b','c'] ['e','d','f'] Any ideas? Thanks in advance David Holland ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Lists in a file
David, You are processing a text file. It is your job to treat it as a file comprised of lists. I think what you are saying is that each line in the text file is a list. In that case for line in fileobject: listline = list(line) Now, listline is a list of the text items in line. Hope this helps. Robert Berman David Holland wrote: Hi, I am trying to create a program where I open a file full of lists and process them. However when the program does not recognize the lists as lists. Here is the relevant code :- def open_filedef(): text_file =open (voteinp.txt,r) lines = text_file.readlines() for line in lines: print line print line[0] text_file.close() And an example of the type of text :- ['a','b','c'] ['e','d','f'] Any ideas? Thanks in advance David Holland ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Lists in a file
On Sun, Apr 26, 2009 at 2:18 PM, Robert Berman berma...@cfl.rr.com wrote: David, You are processing a text file. It is your job to treat it as a file comprised of lists. I think what you are saying is that each line in the text file is a list. In that case for line in fileobject: listline = list(line) Now, listline is a list of the text items in line. listline will be a list of all the characters in the line, which is not what the OP wants. If the list will just contain quoted strings that don't themselves contain commas you can do something ad hoc: In [2]: s = ['a','b','c'] In [5]: [ str(item[1:-1]) for item in s[1:-1].split(',') ] Out[5]: ['a', 'b', 'c'] In Python 3 you can use ast.literal_eval(). import ast s = ['a','b','c'] ast.literal_eval(s) ['a', 'b', 'c'] You can use these recipes: http://code.activestate.com/recipes/511473/ http://code.activestate.com/recipes/364469/ Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Lists in a file
Le Sun, 26 Apr 2009 18:03:41 + (GMT), David Holland davholla2...@yahoo.co.uk s'exprima ainsi: Hi, I am trying to create a program where I open a file full of lists and process them. However when the program does not recognize the lists as lists. Here is the relevant code :- def open_filedef(): text_file =open (voteinp.txt,r) lines = text_file.readlines() for line in lines: print line print line[0] text_file.close() And an example of the type of text :- ['a','b','c'] ['e','d','f'] Any ideas? You're making a confusion between text items and what could be called running program items. And also you're assuming that python reads your mind -- a feature not yet implemented but in project for py4000. More seriously, a list (or any running program item) is something that is produced in memory when python runs your code, like when it runs l = [1,2,3] after having parsed it. What you're giving it now is *text*. Precisely text read from a file. So it regards as text. It could hold I am trying to create a program where Right? Python has no way to guess that *you* consider this text as valid python code. You must tell it. Also, you do not ask python to run this. So why should it do so? Right? If you give python a text, it regards it as text (string): t = [1,2,3] ; [7,8,9] t '[1,2,3] ; [7,8,9]' type(t) type 'str' A text is a sequence of characters, so that if you split it into a list, you get characters; if you ask for the zerost char, you get it: list(t) ['[', '1', ',', '2', ',', '3', ']', ' ', ';', ' ', '[', '7', ',', '8', ',', '9', ']'] t[0] '[' If you want python to consider the text as if it were code, you must tell it so: listtext1 = t[0:7] listtext1 '[1,2,3]' list1 = eval(listtext1) list1 [1, 2, 3] list1[1] 2 Now, as you see, I have a list object called list1. But this is considered bad practice for numerous good reasons. Actually, what you were doing is asking python to read a file that you consider as a snippet of program. So why don't you write it directly in the same file? Or for organisation purpose you want an external module to import? Denis -- la vita e estrany ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] How to run a .py file or load a module?
I'm looking at recursion in Think Python, and this is the bit of code: #!/usr/bin/env python def countdown(n): if n = 0: print 'Blastoff!' else: print n countdown(n-1) I've typed that in vim and saved as countdown.py, but I'm not sure how to run it. I've had other python files, where the triggering function didn't take any arguments, so I would just put a `foo()` at the end of the .py file. However with this particular function that requires an argument, I'm not sure how to run it. I've had to type it out in the python prompt and then call the function with an argument. That works, naturally. I've also tried this: import countdown countdown(10) but this is the error I get: Traceback (most recent call last): File stdin, line 1, in module NameError: name 'countdown' is not defined How can I a) Open my shell, and do something like: $ python countdown.py but have it take an argument and pass it to the function, and execute. b) Import the function in the interactive interpreter, and call it like so: countdown(10) without getting the abovementioned error. Thanks. Dayo ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to run a .py file or load a module?
Dayo, I modified the code a little bit to make things work the way I think you meant it to work(hopefully), and I changed the name of the function so that its' not the same name as the python file itself, but hopefully this answers your questions. Here is my countdown.py def launchme(n): while n 0: print n n -= 1 else: print 'Blastoff!' #uncomment to run from the shell #launchme(7) So, assuming we're running the interpreter from the same folder that countdown.py is in. You have to call module.function(parameter) import countdown countdown.launchme(4) 4 3 2 1 Blastoff! If we uncomment the #launchme(7) line, and run it from the shell: $ python countdown.py 7 6 5 4 3 2 1 Blastoff! On Sun, Apr 26, 2009 at 3:35 PM, Dayo Adewunmi contactd...@gmail.comwrote: I'm looking at recursion in Think Python, and this is the bit of code: #!/usr/bin/env python def countdown(n): if n = 0: print 'Blastoff!' else: print n countdown(n-1) I've typed that in vim and saved as countdown.py, but I'm not sure how to run it. I've had other python files, where the triggering function didn't take any arguments, so I would just put a `foo()` at the end of the .py file. However with this particular function that requires an argument, I'm not sure how to run it. I've had to type it out in the python prompt and then call the function with an argument. That works, naturally. I've also tried this: import countdown countdown(10) but this is the error I get: Traceback (most recent call last): File stdin, line 1, in module NameError: name 'countdown' is not defined How can I a) Open my shell, and do something like: $ python countdown.py but have it take an argument and pass it to the function, and execute. b) Import the function in the interactive interpreter, and call it like so: countdown(10) without getting the abovementioned error. Thanks. Dayo ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to run a .py file or load a module?
2009/4/26 Dayo Adewunmi contactd...@gmail.com: I'm looking at recursion in Think Python, and this is the bit of code: #!/usr/bin/env python def countdown(n): if n = 0: print 'Blastoff!' else: print n countdown(n-1) I've typed that in vim and saved as countdown.py, but I'm not sure how to run it. However with this particular function that requires an argument, I'm not sure how to run it. I've had to type it out in the python prompt and then call the function with an argument. That works, naturally. I've also tried this: import countdown countdown(10) When you import it lile this the function countdown is part of the module countdown. So you call it like countdown.countdown(10). Or import it like from countdown import countdown and then your example will work. but this is the error I get: Traceback (most recent call last): File stdin, line 1, in module NameError: name 'countdown' is not defined How can I a) Open my shell, and do something like: $ python countdown.py but have it take an argument and pass it to the function, and execute. Look at sys.argv which returns a list with the first value being the script name and the second are the command line argument(s). http://docs.python.org/library/sys.html b) Import the function in the interactive interpreter, and call it like so: countdown(10) without getting the abovementioned error. See above. The script would then look like: #!/usr/bin/env python import sys times = int(sys.argv[1]) # The argument given on the command line def countdown(n): if n =0: print 'Blast off!' else: countdown(n-1) countdown(times) Greets Sander ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to run a .py file or load a module?
On Mon, Apr 27, 2009 at 12:07 AM, Sander Sweers sander.swe...@gmail.com wrote: 2009/4/26 Dayo Adewunmi contactd...@gmail.com: I'm looking at recursion in Think Python, and this is the bit of code: #!/usr/bin/env python def countdown(n): if n = 0: print 'Blastoff!' else: print n countdown(n-1) I've typed that in vim and saved as countdown.py, but I'm not sure how to run it. However with this particular function that requires an argument, I'm not sure how to run it. I've had to type it out in the python prompt and then call the function with an argument. That works, naturally. I've also tried this: import countdown countdown(10) When you import it lile this the function countdown is part of the module countdown. So you call it like countdown.countdown(10). Or import it like from countdown import countdown and then your example will work. but this is the error I get: Traceback (most recent call last): File stdin, line 1, in module NameError: name 'countdown' is not defined How can I a) Open my shell, and do something like: $ python countdown.py but have it take an argument and pass it to the function, and execute. Look at sys.argv which returns a list with the first value being the script name and the second are the command line argument(s). http://docs.python.org/library/sys.html b) Import the function in the interactive interpreter, and call it like so: countdown(10) without getting the abovementioned error. See above. The script would then look like: #!/usr/bin/env python import sys times = int(sys.argv[1]) # The argument given on the command line def countdown(n): if n =0: print 'Blast off!' else: countdown(n-1) countdown(times) Don't forget to add: if __name__ == '__main__': launchme(times) Greets Sander ___ Tutor maillist - tu...@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to run a .py file or load a module?
Norman Khine wrote: On Mon, Apr 27, 2009 at 12:07 AM, Sander Sweers sander.swe...@gmail.com wrote: Here is another one for fun, you run it like python countdown.py 10 #!/usr/bin/env python import sys from time import sleep times = int(sys.argv[1]) # The argument given on the command line def countdown(n): try: while n != 1: n = n-1 print n sleep(1) finally: print 'Blast Off!' countdown(times) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor