I will start out by saying this title is a little misleading but I’ve been trying to learn some Python lately and I have figured out some things that will help you get well on your way to learning Python in about 5 minutes. I won’t say that I know Python well at this point but what I DO know is how to figure a lot of things out without cracking open the manual. For starters, get Python installed. I’ll leave that part up to you. After that, you’ll want to open the Python interpreter by typing python at the command line in your particular operating system. You can’t save your code while you are in the interpreter but you can safely play around in your own personal sandbox.

First lesson – Strings: At the prompt, type in something like:

>>> s=’Spam!’

That will put a value into memory that you can play with.

Second lesson – dir() & help(): Not only is “s” a string, it’s also an object.  This lends itself to all kinds of neat things that I will let you figure out on your own but one of the coolest features is that you can tack string attributes on it very easily.  How do you know which attributes will work?  This is where dir() comes in.  At the interpreter prompt, type:

>>> dir(s)

This will show you a huge list of attributes that can perform operations on your string.  For instance, you can use:

>>> s.swapcase()

This will print something like ‘sPAM!’ at on your screen.  If you want to change the variable, you can use:

>>> s=s.swapcase()

Now what if you see another attribute in the list who’s function isn’t self explanatory?  This is where help() comes in.  Say that you don’t know what the “zfill” attribute of a string means…  Type:

>>> help(s.zfill)

You’ll get the exact explanation of what the attribute does right at your prompt.  You can also simply do a help(s) but you’ll get pages of help that will scroll off your screen.

There is a LOT more to python than this of course but these couple of bits of knowledge will help you learn a lot of python’s tricks just by playing around with code at the interpreter prompt.  Of course you can’t go wrong reading the python documentation either.  It’s pretty good and thorough.  I wouldn’t suggest buying a book until you give that a shot.  If you do still need a book, I like Learning Python: Powerful Object-Oriented Programming.



If you like the content on this site, please support it by using this link to order from Amazon. You know you were going to go there and buy stuff anyhow so why not help me pay the hosting bill.