![]() |
|
Re: ** Python Support Thread **
Quote:
I wrote the code in python command line, then saved it as a .py, then ran it through IDLE, I clicked run --> check module-- then I get the syntax error. |
Re: ** Python Support Thread **
Ok, there is the problem. Saving the text from the command line includes text that is not part of the code that runs.
Delete any line that doesn't have ">>>" before it. Then delete all of the instances of ">>>". You should end up with something very close to what Gazillion posted. That is the actual Python code. Everything else is there as part of the interactive python shell. |
Re: ** Python Support Thread **
Lol, yes - very much what Irishthug said :)
It would be a really good idea to not get into the habit of copying the output of the command line into a text file and saving it as a .py Think of them as two seperate entities. Just use the command line if you want to quickly experiment with new commands etc. For actually writing a piece of code that you wish to execute at will, always do it from a blank file in a text editor or IDLE and save it as a .py The command line will have a tonne of stuff that is specific to it and it alone, including all the stuff that you've already discovered to be problematic. |
Re: ** Python Support Thread **
madmax, I'm just learning now to program and am working through the tutorial here: http://learnpythonthehardway.org/ the book is free if you view it online. what you're doing made me cringe. I think you are a step or two ahead of yourself and should maybe check this out.
|
Re: ** Python Support Thread **
That MIT class ~is~ the hard way to learn Python. :p I haven't read the code in that book but the author is pretty entertaining.
|
Re: ** Python Support Thread **
what course number are you referring to? I found it earlier, but it looked like I missed the start date or something and didn't investigate past that. Now I can't find the link.
|
Re: ** Python Support Thread **
Pretty cool trick I just learned, using an else statement with a for loop...
Instead of: Code:
found = FalseCode:
for count in xrange(10): |
Re: ** Python Support Thread **
I always mix up how that works, I can't decide if I should claim it's bad style or just get more used to it.
|
Re: ** Python Support Thread **
If more languages supported it, it would be fine. The fact that most people don't know what it means, is what causes the issue.
|
Re: ** Python Support Thread **
Quote:
Just an interesting tidbit about xrange(): xrange is syntatically replaced by range() in Python3 so that range() has the same function as xrange(). range is (in many cases) supposed to be written as list(range()). |
Re: ** Python Support Thread **
Quote:
I'm still using Python 2.6, and there xrange() is more efficient memory-wise than range(). It always confused me though, so I'm glad to hear in 3.x that the two get consolidated. |
Re: ** Python Support Thread **
I love the loop-else construct so much that I use it even when it's not necessary (because the "break" was actually a function return statement). That way I find it easier to remember what I did for some reason.
|
Re: ** Python Support Thread **
Quote:
range() works by turning the iterator into a list object first and then computing, and considering that many programmers don't know about xrange(), this only serves to slow down the programs written with python. xrange() simply counts out the iterators and computes on it one after another. So, unless you really need a list of values, range() isn't very useful. What Python3 did is default range to act like xrange(). So now if you want your iterators to become lists (make range() act like the old range()), you have to make it explicit that you do. list(range()) Of course, this opens up a whole can of worms about the "best" way to do things, but for the most part, the difference in range() in 2 and 3 is superficial and I don't think most programmers moving from 2 to 3 will ever know the difference. I suspect that this was done on purpose by the Python overlords. |
Re: ** Python Support Thread **
Quote:
Also, in response to a different discussion, either they didn't remove reduce from Python3 or they changed their minds: http://docs.python.org/release/3.1.3...nctools.reduce |
Re: ** Python Support Thread **
Quote:
Sorry, I'm already forgetting 2. |
Re: ** Python Support Thread **
np, I was just puzzled more than anything
btw, in case you're still using int() to fix division results, a shorter way to get around the integer division problem is to use // (integer division) instead of / (division). So, this: Code:
x = 10//2 |
Re: ** Python Support Thread **
Quote:
|
Re: ** Python Support Thread **
I've been programming in Python for a few years now and never learned list comprehensions. They seem to pop up a lot in other people's code, but so far I've never had a problem getting things done other ways. What are their benefits over just a standard for-loop to process data? Are there any times where I *really* should be using list comprehensions?
Edit: Map and filter too, I've never used those. Worth learning? |
Re: ** Python Support Thread **
You should be using them anywhere that their use makes your code shorter/more concise without sacrificing clarity of purpose/readability.
I'd rather see: Code:
rands = [random.random() for x in range(10000)]Code:
rands = []I'd often use map for something like: Code:
some_string = "1,2,3,4"Code:
some_string = "1,2,3,4"Code:
odds = [x for x in range(10) if x%2 != 0]Code:
odds = filter(lambda x: x%2!=0,range(10)) |
Re: ** Python Support Thread **
Thanks.
So looking at my code, this seems like a good candidate... Code:
header = ['Column A','Column B','Column C']Code:
header = [book for book in book_list if book in settings['book']]Code:
header = ['Column A','Column B','Column C'].extend([book for book in book_list if book in settings['book']]) |
Re: ** Python Support Thread **
Quote:
Code:
header = ['Column A','Column B','Column C'] + [book for book in book_list if book in settings['book']]Code:
header = [book for book in book_list if book in settings['book']]Code:
header = ['Column A','Column B','Column C'] |
Re: ** Python Support Thread **
Quote:
Are you saying you want: [columnA [bookA, bookB], columnB....] or [columnA, columnB, columnC, bookA, bookB, bookC...] Because these are two very different questions, but if you want option 2, you shouldn't have to do anything. List are ordered objects, so if you put 1 in, then you put it in spot 1. Code:
>>> a = [] |
Re: ** Python Support Thread **
Yes I want option 2, but as a list comprehension instead of a for loop. Think I got it figured out thanks to Neko.
Here's another for loop I'm trying to substitute with a list comprehension: Code:
row_data = []Code:
row_data = [[] for key in dict] |
Re: ** Python Support Thread **
But he didn't tell you which one is the 'Pythonic' way. :D
|
Re: ** Python Support Thread **
Code:
row_data,row_color = [[[] for key in dict] for x in xrange(2)] |
| All times are GMT -4. The time now is 04:26 PM. |
|
Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
Copyright © 2008-2020, Two Plus Two Interactive