Monday, June 27, 2011

Learning Python: #2 - Five Interesting Things

So, I'm going through the tutorial and have already found some things that I find interesting.
  1. Division Operators
    • 3/2 = 1.5
    • 3//2 = 1 - division that produces an integer by discarding the remainder
  2. The _ variable will have the last thing printed in the window assigned to it
  3. A \ at the end of a line will let the string literal span multiple lines
  4. Indexing of strings
    • "someString"[2:7] prints 'meStr'
    • "someString"[:3] prints 'som'
    • "someString"[-3:] prints 'ing'
    • "someString"[:-3] prints 'someStr'
  5. The built in function len() applies to both strings and lists
Bonus:
  • By default the print() function will end with a newline. You can specify a different ending with the keyword end
    • Syntax: print(someVariable, end="|")
    • The above used in a loop will create a pipe delimited list

No comments:

Post a Comment