Going to Learn Python Now : Pt. 2

Well, I went through Exercises 0 -> 6 yesterday from http://learnpythonthehardway.org/book/, and now it’s time to continue and learn some more!


Exercise 7: More Printing

  • -so you repeat a string multiple times in a row simply by multiplying it ie: print “.” * 10
  • -and printing things on multiple rows happens by default when using multiple prints, but a comma can be used after one line to append the next (albeit with a space separating the strings)
  • -apparently it’s bad style in python for a line to be longer than 80 characters

Exercise 8: Printing, Printing

  • -hmm… so it doesn’t seem to like the fact that there is a ‘ inside of a string… by doing that it quoted my string as using double quotation marks while all the rest used the single quotation mark.
  • -I guess %r is used for debugging and thus prints stuff “efficiently” rather than “verbatim”
  • -I don’t like that the author neglects to explain his stance on why Python 3 is bad to use, or why IDLE is bad to use. Simply saying “don’t do it” is weird. At any rate, I will co-operate for the sake of learning. The last time I tried learning Python, I was following a Python 2.5 tutorial for Houdini 10, but I was using Python 3 in Houdini 11. The tutorial was uncompletable. Will not repeat that mistake this time.

Exercise 9: Printing, Printing, Printing

  • -n can be used to create a “new line” in strings
  • -or we can use three double quotes “”” to be able to print an arbitrary number of lines in a string.

Exercise 10: What was That?

  • -so the that was taught previously is an escape character. We can use them with various symbols to get different effects.
  • — n does a new line
  • — t tabs/indents the line
  • — adds a backslash
  • — ” allows to include a quote inside a quote
  • —- “I am 6’2″ tall.” # escape double-quote inside string
  • — ‘ will behave the same as above assuming you’re in a single quoted string instead of a double quoted string.
  • — a ASCII bell (Bel)
  • — b ASCII backspace (BS)
  • — f ASCII formfeed (ff)
  • — N{name} Character name in the Unicode database (Unicode Only)
  • — r ASCII Carriage Return (CR)
  • — uxxxx Character with 16-bit hex value xxxx (Unicode Only)
  • — Uxxxxxxxx Character with 32-bit hex value xxxxxxxx (Unicode only)
  • — v ASCII vertical tab (VT)
  • — ooo Character with octal value ooo
  • — xhh Character with hex value hh
  • – I ran the “fun code” that was suggested:

    while True:
    for i in ["/","-","|","\","|"]:
    print "%sr" % i,

    and it goes into an infinite loop. Pressing ctrl+c in the shell will break the loop with a forced crash.

Exercise 11: Asking Questions

  • – x = int(raw_input()) which gets the number as a string from raw_input() then converts it to an integer using int().
  • – input() apparently has security problems, so should be avoided… use raw_input()

Exercise 12: Prompting People

  • raw_input("How old are you? ") is definitely more efficient than
    print "How old are you?",
    age = raw_input()
  • – x = int(raw_input()) which gets the number as a string from raw_input() then converts it to an integer using int().
  • – input() apparently has security problems, so should be avoided… use raw_input()
  • http://en.wikipedia.org/wiki/Pydoc is a documentation module for the programming language Python
  • python -m pydoc raw_input
  • – this takes long enough for the queary to complete, that an internet search would probably be better…
  • – should be aware of “open”, “file”, “os”, “sys”

Exercise 13: Parameters, Unpacking, Variables

  • – you can import modules into your script from the Python modules. argv is the “argument variable”
  • – If the user is to give your script inputs on the command line, then you use argv. If you want them to input using the keyboard while the script is running, then use raw_input().

And that’s it for another exciting session of learn stuff