Today I have learnt to create an elephant at the touch of a button. And to find Tor in a list. Not to mention the delight of turning a string into a list.
Confused? Me too.
I was especially discombobulated (I am taking refuge in friendly words) by Tor's assumption that I would know how f(x)
worked (I associate it with differentiation and therefore with annoyance and pain, neither of which are conducive to happy learning).
Everything got much better once I managed to get him to admit that when he was talking about variables, he was really just talking about algebra (or at least algebra as it is defined in my brain; I always found it to be the friendliest branch of mathematics because it managed to avoid those pesky numbers, and I would ask you to not take this illusion away from me by explaining algebra beyond a high school level).
First thing's first: I set up my terminal to look
cool. I instantly felt like a hardcore hacker.
We decided I should learn Python, because apparently Python is friendly and nice and will give me warm, cuddly feelings any moment now.
Tor then explained about functions and variables, and how variables can be anything from text strings (he seems obsessed with 'elephant' as a word -- I got much more excited when I realised text strings could include whole works of literature) to numbers (of all shapes and sizes -- I am less excited about them than about the text strings) and lists (which I rather like).
At first I was a little worried about keeping track of all of this, but things got a lot more fun once I realised that I could use actual words instead of single letters as variables: Words are much easier to direct when you call them
word, rather than
a or
x.
Writing
for x in a:
if x=='Tor':
print x
is much more confusing and hard to keep track of than
for word in word_list:
if word=='Tor':
print word
I wrote
paragraph='I have always preferred books to computers, and my fear that the machine would die (possibly taking me with it) if I accidentally pushed the wrong sequence of buttons has entertained (and possibly frustrated?) Tor immensely. Much as I love technology that makes my life easier, I have had a (healthy) respect for the digital. You know where you stand with books. Unless you set fire to them or drop them in water, they will not accidentally get rid of your text; the worst they can do to you is give you a paper cut or fall on your head.'
which defined that text string (lifted from
here) as the variable named
paragraph. Then, I wrote
paragraph.split(' ')
which turned that paragraph into the following (slightly scary-looking) list:
['I', 'have', 'always', 'preferred', 'books', 'to', 'computers,', 'and', 'my', 'fear', 'that', 'the', 'machine', 'would', 'die', '(possibly', 'taking', 'me', 'with', 'it)', 'if', 'I', 'accidentally', 'pushed', 'the', 'wrong', 'sequence', 'of', 'buttons', 'has', 'entertained', '(and', 'possibly', 'frustrated?)', 'Tor', 'immensely.', 'Much', 'as', 'I', 'love', 'technology', 'that', 'makes', 'my', 'life', 'easier,', 'I', 'have', 'had', 'a', '(healthy)', 'respect', 'for', 'the', 'digital.', 'You', 'know', 'where', 'you', 'stand', 'with', 'books.', 'Unless', 'you', 'set', 'fire', 'to', 'them', 'or', 'drop', 'them', 'in', 'water,', 'they', 'will', 'not', 'accidentally', 'get', 'rid', 'of', 'your', 'text;', 'the', 'worst', 'they', 'can', 'do', 'to', 'you', 'is', 'give', 'you', 'a', 'paper', 'cut', 'or', 'fall', 'on', 'your', 'head.']
After which I saved this list as
word_list by writing
word_list=paragraph.split(' ')
I could probably have done that without the intermediary step, but Tor was being pedagogical.
At which point,
for word in word_list:
if word=='Tor':
print word
became possible.
In other words: I can now search a text for words using only the power of my coding skills. You may worship me. (If anyone mentions that it is not a very good search because it can be thrown off by punctuation, you will not get any cake.)
And to make matters even more interesting: I can
count each instance of the word by writing
i=0
for word in word_list:
if word=='Tor':
i=i+1
I get a number! Instead of
Tor written over and over.
Of course, this all depends on my ability to define things correctly, but while slightly daunting it is oddly familiar from writing academic texts.
I wonder whether I will remember any of this tomorrow.
Comments