A Quick and Dirty Introduction to Python
For all of high school C++ was my favorite language for writing general applications - probably because of the similarity to C and my laziness to try and learn an entirely new language.
For some reason, I didn't get to do a whole lot of C++ in college because of an EE major. I tried playing with libraries (OpenCV, OpenGL, wxWidgets) but it soon became clear that I won't be able to keep up for long. Besides, I still hadn't read up on many language constructs - vast, as the language was.
The search for an alternative language took me from MATLAB (expensive, bulky), Haskell (pretty cool but not for blockheads like me) until I finally landed on Python. (Trust me, Python isn't as big yet where I live as it is everywhere else)
For the uninitiated, Python is a dynamically typed, cross-platform, open-source language. Written entirely in C, it is one of the few languages with focus on clarity (Perl, anyone?), ease of use and rapid development (no long compile times).
Sales pitch apart, Python is fast, efficient and clean. Which is why you should try and learn it. The availability of scores of libraries - computer vision, natural language processing, scientific computation, GUI toolkits ... means that you can try and dabble with new areas and come up to speed quickly.
In a series of blog posts, I'll try and put down the bare essentials of getting started with Python. I'll be using Python 2.7 as that is one I've currently more experience with.
For more advanced material, there are excellent Python resources out there.
Just google 'Dive Into Python' (a slower but good, free ebook) or if you are a quick-read I suggest 'Building Skills in Python' (also free). All this besides the excellent python.org which shall remain the go-to reference in case of any dispute.
Let's jump right in. Go ahead and get Python on your machine. You can download it from here. Alternatively, if you use a Mac or Linux machine you might find Python bundled into your distro itself.
Python (at least for Windows) comes bundled with a simple IDE called IDLE. It has an interactive command prompt and a simple editor with basic debugging and code completion (Linux and Mac people may need to download it separately).
Alright now. Fire up Python (either in IDLE or from the terminal) and we're ready to go.
Let's start with baby steps first. You can go ahead and do basic things like calculations like this:
Initializing variables is also painless.Code (Text):
- >>> 51*23
- >>> 45*0.2
Which also introduces the Python print statement. Note that Python is pretty much steeped into the OOP philosophy so most of language constructs are, in some way or the other, objects of some class.Code (Text):
- >>> y = 20/5
- >>> y
- >>> x = "Welcome to Python!"
- >>> x
- 'Welcome to Python!'
- >>> print x
- 'Welcome to Python!'
It might sound funny when I say that in Python classes are objects or even functions are objects. So are many other things. Strange sounding? I know. Very weird? Not so much when you get to know it.
Let's circle back to the topic. Unlike everything else, the print is a statement and not an object. It can act upon another object but it is not an object itself. Take a moment here and we'll move along.
The next step would be introducing types.
Unlike C, Python does not need to be told the data type while initializing a variable. It figures out the type by magic and let's you worry only about what to do with it. If you're moving to Python from C or C++, you might find this little feature unsettling at first (as I did), but you'll probably quickly grow to like it (or at least live with it anyway).
This is why this sort of tomfoolery is perfectly legal (although not encouraged):
An earlier version of this post read:Code (Text):
- >>> var = "spam"
- >>> var = 5.0
- >>> var
Which I have since confirmed to be quite untrue. Python is a strongly-typed language as reflected in the language itself and asserted by numerous sources. The behavior above is a virtue of operator-overloading through which the * operator acts in a way illustrated when dealing with strings and int's.
Lesson: Python clearly distinguishes between various types but does not allow one kind to mutate into another.
I'll stop right here lest this post grow way out of proportions. I'll try and post the next entry soon where we'll examine the type system in more detail and get started with more interesting stuff.
PS: Please let me know if you find any errors (editing or technical) or omissions in this entry. I'll be happy to rectify them.
You need to be logged in to comment