An Overview of Python - Tutorial 1: Getting Python
Author |
Message |
apython1992
|
Posted: Sat Mar 19, 2011 3:21 pm Post subject: An Overview of Python - Tutorial 1: Getting Python |
|
|
In the hopes of seeing a few more Python programmers around, I decided to write a series of tutorials exploring the language, mainly to show users of more conventional languages like C and C++ the benefits of using a very high-level language like Python. So, let's get started! This first part of the series explains some features of high-level languages to attract programmers of these more conventional languages, and goes through the steps involved in getting Python. I know, it's boring. But it's obligatory. Don't worry, after this we'll start having some fun.
Wait...what do you mean by high-level?
For those of you who don't know, one way of quickly describing a programming language is to talk about its level, which is an indication of how closely the language interacts with hardware. Languages like C and C++ (especially C) are typically considered low-level, because they involve more direct hardware manipulation (pointers, for example). Low-level languages generally excel performance-wise (C and C++ can hardly be beaten here), but applications written in low-level languages are less portable because they have a greater dependency on the specific hardware used. They also require a lot more code to get less work done. Python by contrast is a high-level language, which means it involves less direct manipulation of hardware. High-level languages are a little bit slower than low-level languages, but in most situations this slight performance loss is far outweighed by the immense gains in programmer productivity and development speed - it is very difficult to do some of the most basic things in C (ever try working with strings?), while high-level languages like Python and Ruby provide intuitive interfaces for all kinds of tasks.
And now Python
Forgetting this high-level/low-level stuff for now, let's find out what Python's all about. Python is a dynamically-typed language that supports multiple programming paradigms - this means that you get some choice! Python has full support for imperative and Object-Oriented programming, and some limited support for functional programming as well (which I am actually looking into myself right now). As opposed to being compiled, Python is interpreted. This means you can just write out your program and run it, without the intermediate compilation step. Cool! One of the first recognizable advantages C/C++ programmers might notice in Python is the lack of need to declare variables and assign them with a type. All types are inferred in Python - if you say that myVar = 3, Python's interpreter is smart enough to deduce that myVar is an integer. Another very useful aspect of the language is the interactive interpreter, which runs through the console. It allows a user to write Python code one line at a time and immediately see the output; this is immensely useful for testing something to see if it works before incorporating it into a full program. Finally, Python has what some people call a "batteries included" philosophy. This means that its standard library is very large (but not bloated), eliminating the need to use third-party libraries in many applications.
I could go on and on about why Python is a great language to learn and use, but I'm sure I've already convinced you. So now that you're convinced, how can you get in on this Python deal?
Getting Python
Getting Python is very easy. If you're a Linux user, I'd bet money you already have it. Most Linux distributions already come with Python, and there's a very easy way to check. Open up a terminal window, and simply type the command "python". Example:
code: | andrew@ubuntu:~$ python
Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print "Hello, world!"
Hello, world!
>>> |
The "python" command invokes the interactive interpreter, and as you can see I made use of it. I told it to print "Hello, world!", and it sure did. Wonderful, right? If you're a Linux user and this command didn't invoke the interpreter, then that means you don't have Python installed. Just use either your favourite package manager or the terminal to get it and install it.
For Windows users, you might have to go through slightly more to get Python up and running, but it ain't that bad. First of all, I've heard that some Windows machines now already come with Python, so let's just check. Open up a command prompt window (if you don't know how to do that, open the start menu and search "Command", and click on the black box that says "Command Prompt" when the search is done), and just like above type in the "python" command. You should see something similar to the above. If not, then you do not currently have Python. You can find Windows installers for it here: http://www.python.org/getit/
I suggest you stick with Python 2.6 or 2.7; I haven't even used Python 3 yet and it's still quite new.
Now, once you have it, you might have to go through one more step. Try the "python" command in the Command Prompt again. If it still doesn't work, then you have to add Python's location to your PATH variable. PATH, you ask? It's simple. Your system's PATH variable includes a list of path locations in your system so that it can always know where to find them, wherever and whenever. So let's add Python to this list. Go to the directory that Python was installed (probably C:\Program Files\Python2x), and copy the path name up to Python's base directory --> "C:\Program Files\Python2x". Now, open up the Control Panel, navigate to System Properties -> Advanced System Settings (I think...), and click on the button that's labeled "Environment Variables". Find PATH, and click the Edit button. Paste Python's full path name, and be sure to separate it from other values in the list with a single semicolon (. And finally, try the command once more. Ta-dah! On older systems, you may need to restart your computer. I'm not sure.
And finally, for Mac users, you're on your own. I have never in my life used Mac, so I won't offer any suggestions on how to get Python. Downloading and installing it is real easy though, so I'm sure you can figure it out.
So now we have Python...it's time to learn how to use it! In the next tutorial we'll start using the interactive interpreter to get a feel for some of Python's syntax and basic data structures. In the meantime, it wouldn't hurt to play around with it on your own. Until next time! |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Tony
|
Posted: Sat Mar 19, 2011 3:54 pm Post subject: RE:An Overview of Python - Tutorial 1: Getting Python |
|
|
Yay Tutorials! +1 karma for you.
Some points though
Quote:
Languages like C and C++ (especially C) are typically considered low-level, because they involve more direct hardware manipulation (pointers, for example).
Not entirely true. At least not for typical use. The OS interacts with the hardware, and languages compile to applications that run on top of an OS. C/C++ are better build for direct syscalls though. Pointers are interesting in a way that you get a virtual memory address, which is managed by the OS. Even in Python one would care about pointers, in a way of pass by value vs. reference.
Though to be fair, one could write an OS in C/C++ (with a bit of assembly). Though on-hardware C is a very different game than on-OS C.
Quote:
All types are inferred in Python - if you say that myVar = 3, Python's interpreter is smart enough to deduce that myVar is an integer.
This is commonly referred to as Duck Typing -- http://en.wikipedia.org/wiki/Duck_typing |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
apython1992
|
Posted: Sat Mar 19, 2011 4:07 pm Post subject: RE:An Overview of Python - Tutorial 1: Getting Python |
|
|
Thanks Tony! And point taken - much of that is handled by the OS. Also true about pointers, I realize value vs reference is important everywhere.
That's good to know about Duck Typing, I wondered if there was a term for that. |
|
|
|
|
|
Sur_real
|
Posted: Sat Mar 19, 2011 5:24 pm Post subject: RE:An Overview of Python - Tutorial 1: Getting Python |
|
|
hmm...what about PHP?
I've never heard of duck typing so I assume its different for PHP but I do know that in PHP you don't need to define the type of the variable too...
Great intro btw, I might look into python sometime in the near future! |
|
|
|
|
|
SNIPERDUDE
|
Posted: Sat Mar 19, 2011 7:29 pm Post subject: RE:An Overview of Python - Tutorial 1: Getting Python |
|
|
Kudos and Bits to you my good sir. |
|
|
|
|
|
|
|