On 6/26/2016 8:13 PM, Elizabeth Weiss wrote:

Hi There,

What is the point of this code?:

word=[]
print(word)

The result is []

When would I need to use something like this?

Thank you!

Sometimes you need to assign an empty list to a variable prior to using it in a looping structure.

Here's the load function from the BASIC interpreter/compiler I'm working on in Python.

def load(filename):
    tokens = []
    with open(filename) as source:
        for line in source:
            tokens.append(tokenizer(line))
    return tokens

Here's the BASIC file I'm loading:

10 PRINT "HELLO WORLD!"
20 GOTO 10

This function assigns an empty list to tokens, reads a line of text from a file, and appends a tokenized list to tokens, and return tokens as a list of lists.

[['10', 'PRINT', '"HELLO WORLD!"'], ['20', 'GOTO', '10']]

Chris R.



--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to