Style, variables

posted by: Ms. Martin 21 September 2009 No Comment

I started off talking a little bit about the importance of making Python programs human-readable.  We talked about grouping functions at the top and code that Python executes at the bottom.

We talked about the print function which lets us display text to the user.  We also learned that Python is really good at doing math.  You can use + – * and / with it, but beware of division!  If you divide two whole numbers, the result is always a whole number because decimals are just truncated.  For example,

print(5 / 2)

would display 2, not 2.5 or 3.  That can cause some nasty bugs if you forget about it.  To print out the result with decimals, one of the two numbers must be a floating point number:

print(5.0 / 2)

I also discussed variables, a way to store information for later use.  When we were using for loops, the loop counter (i) was just a variable.

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>