Having learnt enough Swift to write a neat watch face app for Apple Watch this summer, I thought I’d turn my attention to learning some Python.
Firstly, I wanted a way to edit Python commands in a decent editor and run them. It appears that Xcode doesn’t support this out of the box, but this handy StackOverflow question gives the details on how to set it up.
Secondly, I wanted a series of challenges/tutorials to walk through the basic syntax of Python. HackerRank.com is very good for this sort of thing and has a decent set of exercises to work through.
My first bug was very revealing about the differences between Python and strongly-typed languages like F# and C++:
N = int(raw_input().strip()) if (n > 10): print( "big" ) elif ( N < 0 ): print( "negative" ) else: print( "normal" )
The Python Interpreter doesn’t give an error because of the typo “n > 10” instead of “N > 10” – it just carries on regardless!
Finally, Python Software Foundation seems like a good reference site with lots of examples.
See also the next post in this series.
You should get “NameError: name ‘n’ is not defined”
You’ll also get an invalid syntax error because you need a colon after the else. (Just one of those python idiosyncrasies).
Thanks Chris – agreed about both points. I’ve found that I get the “name ‘n’ is not defined” error running Python locally on my machine, but not in the online interpreter on HackerRank.com (it just ran and produced the wrong output).
Pingback: How to Learn Python II | musingstudio