Python Tutorial Section 1

I've just completed my setup for 6.01, by installing Idle, Emacs, and Python, and I'm digging into the tutorials...

What makes Python unique?

There are some differences between Python and the languages I've worked with in the past (Java, PHP, and Ruby) that need noting:

  • Python is sensitive to white space. For example, an if block must be started with a colon and indented.
#Example of a python if-else statement
if (true):  
    x = 2
else:  
    x = 3
  • Python doesn't require variable type declartions, and is a dynamically typed language.

Worksheet 1.1.1

if type(3.14) is float:  
  print "Correct"
else:  
  print "Wrong"

if type(-34) is int:  
  print "Correct"
else:  
  print "Wrong"

if type(True) is bool:  
  print "Correct"
else:  
  print "Wrong"

if type(None) is NoneType:  
  print "Correct"
else:  
  print "Wrong"

if type(3.0) is float:  
  print "Correct"
else:  
  print "Wrong"

Had a little confusion with which words were key words in Python, but I got them all. NoneType (as opposed to noneType) and bool (as opposed to boolean), were not immediately obvious.

Worksheet 1.1.2

if (6+12-3) == 15:  
  print "Correct"
else:  
  print "Wrong";

# Marked at wrong but due to some strange rounding issue...
if (2.2*3.0) == 6.6:  
  print "Correct"
else:  
  print "Wrong";

if (--4) == 4:  
  print "Correct"
else:  
  print "Wrong";

if (10/3) == 3:  
  print "Correct"
else:  
  print "Wrong";

if (10.0/3.0) == (3 + 1.0/3.0):  
  print "Correct"
else:  
  print "Wrong";

if (2+3)*4 == 20:  
  print "Correct"
else:  
  print "Wrong";

if (2 + 3 * 4) == 14:  
  print "Correct"
else:  
  print "Wrong";

if (2**3+1) == 9:  
  print "Correct"
else:  
  print "Wrong";

if 2.1**2.0 == 4.41:  
  print "Correct"
else:  
  print "Wrong";

This one highlighted some strangeness with arthimatic around floats. Not a direct explanation but read here for more info: http://stackoverflow.com/questions/455612/python-limiting-floats-to-two-decimal-points

Worksheet 1.1.3

def check_answer(question, answer):  
  if question == answer:
    print "Correct"
  else:
    print "Wrong"

check_answer(3 > 4, False)  
check_answer(4.0 > 3.999, True)  
check_answer(4 > 4, False)  
check_answer(4 >= 4, True)  
check_answer(2 + 2 == 4, True)  
check_answer(True or False, True)  
check_answer(False or False, False)  
check_answer(not False, True)  
check_answer(3.0 - 1.0 != 5.0 - 3.0, False)  
check_answer(3 > 4 or (2 < 3 and 9 > 10), False)  
check_answer(4 > 5 or 3 < 4 and 9 > 8, True)  
check_answer(not(4 > 3 and 100 > 6), False)  

Abstracted out all that extra work into a function. Also removed the extraneous semicolon after my wrong statement. Got them all right on the first try.

Worksheet 1.1.4

import math

def check_answer(question, answer):  
  if question == answer:
    print "Correct"
  else:
    print "Wrong"

check_answer(type(3 + 5.0), float)  
check_answer(type(5/2), int)  
check_answer(type(5/2 == 5/2.0), bool)  
check_answer(type(5/2.0), float)  
check_answer(type(round(2.6)), float)  
check_answer(type(int(2.6)), int)  
check_answer(type(math.floor(2.6)), float)  
check_answer(type(2.0 + 5.0), float)  
check_answer(type( 5 * 2 == 5.0 * 2.0), bool)  

Easy one - got them all right on the first try.

Worksheet 1.2.1

def check_answer(question, answer):  
  if question == answer:
    print "Correct"
  else:
    print "Wrong"

a = 3  
check_answer(a + 2.0, 5.0)  
check_answer(type(a + 2.0), float)

a = a + 1.0  
check_answer(a, 4.0)  
check_answer(type(a), float)

#check_answer(b, None)
#check_answer(type(b), NoneType)

check_answer(a == 5.0, False)  
check_answer(type(a == 5.0), bool)

b = 10  
c = b > 9  
check_answer(c, True)  
check_answer(type(c), bool)  

One of these perplexed me. I know b doesn't exist, but I'm not sure there is a way for me test the type of None, unless I use something clearly beyond the scope of this chapter. Will leave it for now... but I would probably start by evaluating if it existed with 'b' in locals().

Complete set of solutions are on Github