Stuff that occurs to me

All of my 'how to' posts are tagged here. The most popular posts are about blocking and private accounts on Twitter, also the science communication jobs list. None of the science or medical information I might post to this blog should be taken as medical advice (I'm not medically trained).

Think of this blog as a sort of nursery for my half-baked ideas hence 'stuff that occurs to me'.

Contact: @JoBrodie Email: jo DOT brodie AT gmail DOT com

Science in London: The 2018/19 scientific society talks in London blog post

Friday 26 August 2022

Thoughts on starting to learn Python - I am rather new to it

While my colleague was away on his hols recently I thought I'd surprise him by having a go at learning Python so signed up myself for a week-long course (at a different institution). Every year he and other colleagues teach hundreds of undergraduates how to program in Python and Java (subject knowledge), but he also teaches lots of schoolteachers how to teach programming too (pedagogy). 

I expect we're moving in the direction of undergraduates being taught how to teach programming as well, because one of the skills they'll probably need in the workplace is to be able to teach new colleagues how to use new task-specific software. Anyway...

 

Gosh I was right to have avoided learning Python for this long, it's like Very Hard Sums ;) Much harder than I was expecting. I don't seem to be much of a computational thinker alas (learning to program not only teaches the obvious 'how to program', but also 'how to think a bit more logically about things', but sadly I may be a lost cause). Also, I have no particular impetus for learning programming (beyond annoying myself and amusing my colleague) as I genuinely can't see to what use I'd ever put it (and this has generally discouraged me from taking it up in the past).

That said, I've always been 'good at computers' and gravitated towards the nerds wherever I've worked - it's a constant surprise to me that I don't already know how to program or that I'm not a SysAdmin. Doing the course felt a bit like 'retconning' a missing patch from earlier in my life that probably should have been installed during or shortly after I left university. Ironically I do actually have a university qualification in Computer Science because I studied it for one year on my modular course (Biology, Psychology and Computing). I remember spending my holiday in a library in Harrow reading newspaper articles about IBM and Token Rings. We learned to program in SQL and had to make a thing that would calculate how quickly a bath would fill and empty, ostensibly for a property management company. 

Anyway I enjoyed the recent short course but it was a bit fast for me (or perhaps I'm a bit slow for it) and I found that if I wrote anything down there was a risk I'd miss something, which I would later have to rely on, so I mostly just tried to keep up.  

I didn't find the concepts of variables or types or int (integer) / float (numbers with decimal points) or string hard though - the thing I found hardest was actually parsing the English instructions for the exercises. I always felt I was missing something and asked a LOT of questions. The tutor and other students were very helpful and didn't make me feel too much like a lame duck.


It reminded me of doing AO maths (an alternative O level that's slightly more advanced, but not much). There was lots of lovely mechanics involved and trajectories, using formula like s = ut + ½at² where 'u' is the initial velocity. People smarter than me will immediately realise that the starting speed of a ball about to be thrown is obviously zero, but I have to admit that I once asked for help with that as the instruction had 'missed' it out. I seemed to lack the cognitive wherewithal to work that out for myself - oh dear! It looks like I am not very good at extracting, or abstracting, information from a paragraph of text and really need bullet points.

So this was pretty much where I found myself with trying to work out what I was supposed to be doing, let alone trying to convert it into a program. It was interesting to see under the bonnet of my own cognition - to see exactly where the limits are of my own capacity for understanding.

I did learn a lot by taking the solution and testing permutations, basically to see what happens when I tweak different bits of it, but I would not say that I have the sort of brain that finds it easy. I'm clever enough and reasonably logical, but possibly not quite clever or logical enough for Python. Also I'm 52 so possibly this would have been easier 30 years ago. Now i just want an easy life ;)

One exercise involved using the remainder / modulo function which I couldn't get my head around at all. I spent at least half an hour that day being absolutely baffled that anyone would ever want to know what was left over after a division. Surely if you divide 236 by 17 the only answer you want is 13.88 and not '13' or '88'. It featured in an exercise about leap years that I didn't really manage to get to grips with.

Later I was doing some work for our Charlton and Woolwich Free Film Festival and had a flash of inspiration about film times and the mystery modulo. 

If I want to express the time, in hours and minutes, of a film lasting 138 minutes then I don't want to say that it's 138/60 = 2.3 hours, I want to say that it's 2 hours and however many minutes. Aha! AHA!!

I actually managed to create a program that worked and told me the answer!!

138 / 60 means "what is 138 ÷ 60", and gives the answer of 2.3.

138 // 60 means "what is the whole number that results from dividing 138 by 60?", and the answer is 2.

138 % 60 means "what's left over once you've finished dividing by (really, subtracting from) 60?", and it's 18.

So a 138min film = 2hr 18min.

This is my 4-line program (text version at the end)

film_length = int(input("Enter the length of the film in minutes, e.g. 123: "))
hours = film_length // 60
mins = film_length % 60
print("The film is this long:",hours,"hr",mins,"min")
Which results in being asked for the length of the film and, when a number entered, gives the result. Pleasing.

Enter the length of the film in minutes, e.g. 123: 138
The film is this long: 2 hr 18 min

Process finished with exit code 0 <-- this is good by the way

I was pretty pleased to have worked it out for myself and for it to work exactly as expected. Going to see if I can try and write a program to do it in reverse, so you enter 2hr 18min and it gives you 138 mins....

 

Further reading
I'm enjoying reading (very slowly) and comparing these two books / PDFs, published 18 years apart:

How to Think Like a Computer Scientist: Learning with Python 3 Documentation (Release 3rd Edition) by Peter Wentworth, Jeffrey Elkner, Allen B Downey and Chris Meyers (17 April 2020)

and the first edition 'How to Think Like a Computer Scientist: Learning with Python' by Allen Downey, Jeffrey Elkner, and Chris Meyers (2002) (there's also an interactive version).

Text of the program, which I called jo_first_program_31_july_film_length.py

film_length = int(input("Entere the length of the film in minutes, e.g. 123: "))
hours = film_length // 60
mins = film_length % 60
print("The film is this long:",hours,"hr",mins,"min")





No comments:

Post a Comment

Comment policy: I enthusiastically welcome corrections and I entertain polite disagreement ;) Because of the nature of this blog it attracts a LOT - 5 a day at the moment - of spam comments (I write about spam practices,misleading marketing and unevidenced quackery) and so I'm more likely to post a pasted version of your comment, removing any hyperlinks.

Comments written in ALL CAPS LOCK will be deleted and I won't publish any pro-homeopathy comments, that ship has sailed I'm afraid (it's nonsense).