- Use Python interactive interpreter
- Introduce dir function
One of my favorites features of Python is the interactive interpreter. It allows you to easily play with Python. Try it for yourself. Open a terminal and simply type python. The interpreter will tell you what version it's running and give you a prompt for input:
Python 2.6.6 (r266:84292, Dec 27 2010, 00:02:40) [GCC 4.4.5] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>>
At the prompt, try typing the following:
>>> print 'hello world' >>> range(5) >>> sum(range(5))
Input a valid command and you'll usually get some kind of output in return.
My favorite thing about the interactive interpreter is it allows you to quickly answer questions about the language without having to waste time looking up stuff online or -- worse yet, like I used to have to do before the internet -- in books! For instance, how do you do exponentiation in Python: 3^2 or 3**2?
>>> 3^2 >>> 3**2
One very useful builtin python function is dir. It will give you a list of all the attributes and methods attached to an object. See the references section below for other builtin python functions. To learn a little about strings in python, try this:
>>> name = "guido" >>> dir(name) >>> name.capitalize()
>>> license >>> 'version %.1f of %s supports %s-like formatting' % (2, 'python', 'sprintf') >>> 1 % 2 >>> 2 % 2 >>> 2 / 0 >>> math.pow(5,2) >>> import math >>> math.pow(5,2) >>> import this
Any ideas as to what's happening in each case?
- Evidently the ^ character is not used for exponentiation. What does it do?
- Is sprintf-type formatting supported in version 3 of Python?