[Tutor] append, list and variables

pk piotr-kam at o2.pl
Fri Jul 16 21:44:07 CEST 2010


Dnia 16-07-2010 o 19:54:01 Emile van Sebille <emile at fenx.com> napisaƂ(a):

> On 7/16/2010 8:37 AM Mary Morris said...
>> Thanks-that helps a lot.
>> The only question I have about your pseudocode is what the 'initialize
>> result container' does. I'm pretty new to python and scripting ingeneral
>> so I'm still trying to figure everything out.
>
> It creates an empty data storage container for use within the processing 
> loop -- if it wasn't defined before use, you'd get an error the first 
> time you tried to append to it. There's some good info in the tutorial--  
> see http://docs.python.org/tutorial/datastructures.html for details.
>
> Emile
>

The data structure talked about is a *list* and *  
['one','two'].append('e.g. a string') * is one of the list methods.
So it can be written like this:
a_result_container_aka_simply_a_list =  [ ]
a_result_container_aka_simply_a_list.append('something')
print a_result_container_aka_simply_a_list

In general, the shorter and more descriptive a variable name is, the  
better.

You can learn more about Python's data structures and the basic concepts  
of programming using the two books that I've found most useful - of course  
there are many more other good books that can be used.

These books are:
1. "Learn to Program" by Alan Gauld. It's more tutorial-like in style than  
the next one:
2. "How to Think Like a Computer Scientist" by Jeffrey Elkner, Allen B.  
Downey and Chris Meyers.
It's more like a school textbook with exercises at the end of each chapter  
and chapter 9 is called "Lists".
I've used them both to look at the same concepts from different  
perspectives because Alan's book gives more information about programming  
in general while the "How to Think..." is more dry, monotonous and  
impersonal which can be tiring at times.

"The Python Tutorial" by Guido van Rossum is okay too (it's short and  
comprehensive) but it's mainly directed to experienced programmers who can  
already program in at least one language - at least that's my impression.

Regards,
Piotr

>> On Thu, Jul 15, 2010 at 12:58 PM, Emile van Sebille<emile at fenx.com> 
>> wrote:
>>
>>> On 7/15/2010 11:32 AM Mary Morris said...
>>>
>>> Hi,
>>>> I'm working on a program that parses through all of our source codeat  
>>>> my
>>>> office and i need to get my code to print a list of the decorators. I
>>>> used
>>>> a find(@) to locate all the decorators, but I need to assign them to a
>>>> variable somehow to get it to print a list. How do I do this? How do I
>>>> assign a variable to all the indexed strings after the @ symbol?
>>>>
>>>>
>>> So, decorator lines start with an '@'. Source files end in '.py'.
>>>
>>> Pseudo code could be:
>>> ----
>>> initialize result container
>>>
>>> with each sourcefilename in globbed list:
>>> for eachline in opened(sourcefilename):
>>> if line.startswith('@'):
>>> append [sourcefilename, line] to result
>>>
>>> # print it
>>>
>>> for eachline in result:
>>> print eachline
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100716/c07f2ca1/attachment.html>


More information about the Tutor mailing list