I'm an open source hacker by trade and I love sharing knowledge. I thought I'd start a thread about the cool things you can do using the Python language.
I'll start off with some simple examples and later get into web frameworks, databasing, and eventually build an entire frameworked website. Questions are welcomed!
First, how is Python installed?
1. On Linux and Mac, it should be straight forward -- Python should be installed already on your machine.
2. Windows is almost as simple. You go to python . org /getit and install the 2.7.2 executable
How do you get started
1. In Linux or OSX, simply open up a shell / terminal and type the word 'python' with no quotes and press enter.
2. On Windows, you can either find Python 2.7.2 in your start menu, or open your good old cmd shell and run the Python program. Usually it's installed directly at the C: level and is easy to find.
If there are any questions on getting started, just let me know. I'm happy to explain.
Why Python
So here's a good question. Why Python? Many of the folks here seem to love the . net frameworks, which is well and good, but many factors should go into choosing a programming language: robustness, paradigm, speed, complexity, openness, API, potential for cross-platform development, interoperability with other languages, and the overall popularity of the language. Python is not extremely fast, that's alright, because there are several extremely optimized frameworks for python development, like Django, webpy, and pylons.
Why do I use Python? I have been programming for about eleven years and have tried C, C++, common lisp, scheme, clojure, java, c#, vb6, vbnet, javascript and nodejs, OCaml, python, perl, ruby, php, bash as well as a variety of other languages. In fact, I've written a lightweight language of my own based on OCaml for functional programming. I've learned first hand that different languages have different advantages.
The JVM is great because it is cross-platform, and thus it is worth knowing a language like Java, Scala, or Clojure which compiles to JVM byte code. If you're writing something which has to be highly optimized and support million of concurrent users or threats, C has its obvious advantages with low level memory management and fine tune-able MPI. If you're writing a simple administrative script for your shell, might as well code it in bash or perl. But what about web apps?
There's a reason I enjoy using Python for web apps, and it's not because ruby, php, perl, and asp can't accomplish similar tasks. I find Python to be the simplest with the cleanest conventions and syntax, the most powerful libraries, and the most developed web frameworks. Also, python is multi-paradigm which gives the programmer the ability to choose his or her own style without being restricted, be it functional, imperative, or strictly object oriented. I'll show you exactly how easy Python is to use.
Objective: Generate a list of the numbers between 50 and 100 and print them in steps of 5.
While it would be best syntactic practice to put 'print i' on a new line with proper indentation, this is a pretty compelling example of how much you can accomplish with very little code in python. Not only that, but the code makes sense. By the end of this entire tutorial, I hope people will be able to launch their own full-scale website. After all, the technology I will be discussing (webpy, open source and written by Aaron Swartz) was originally designed as the back-end framework for reddit and held up to thousands of users, very well. For now, let's move on to the basics!
Writing Something Useful
Let's write a practical program in python that will actually accomplish something useful. How about defining a simple list?
Notice we don't have to statically type any of our variables. We don't need to specify that x or y are a lists and furthermore, we don't have to tell the interpreter what kind of list x an y are. Meaning, we don't have to specify x is a list of type Integer and y is a list of type string (or char*, a character array if you're coming from C).
This is one huge advantage of a language like Python. It is dynamically typed like perl (although perl uses sigils to represent $ scalars, at symbol for arrays, and % for associative arrays) -- php does similar, ruby, and many other scripting languages.
What about iterating over a list? In Java or c++, your code would look something like this:
For python, loops automatically iterate over lists and grab one element from the list at a type (assuming the list is populated with only atoms and no sublists or tuples)
This is a very simply program which could have been written more compactly in one tiny line using a map reduce list comprehension as:
but the point is just to illustrate how the problem would be solved sequentially.
So far we've gone over the basics of the language, what the syntax looks like, why python may be a good choice for your next web project, and hopefully you're excited about its capabilities.
In my next post I hope to introduce dictionaries, explain the syntax further (for example, indentation does matter in python), and talk more about the type system.
Comments, suggestions, feedback, and questions -- as always -- would be greatly appreciated. Hope you learned something useful!
I'll start off with some simple examples and later get into web frameworks, databasing, and eventually build an entire frameworked website. Questions are welcomed!
First, how is Python installed?
1. On Linux and Mac, it should be straight forward -- Python should be installed already on your machine.
2. Windows is almost as simple. You go to python . org /getit and install the 2.7.2 executable
How do you get started
1. In Linux or OSX, simply open up a shell / terminal and type the word 'python' with no quotes and press enter.
2. On Windows, you can either find Python 2.7.2 in your start menu, or open your good old cmd shell and run the Python program. Usually it's installed directly at the C: level and is easy to find.
If there are any questions on getting started, just let me know. I'm happy to explain.
Why Python
So here's a good question. Why Python? Many of the folks here seem to love the . net frameworks, which is well and good, but many factors should go into choosing a programming language: robustness, paradigm, speed, complexity, openness, API, potential for cross-platform development, interoperability with other languages, and the overall popularity of the language. Python is not extremely fast, that's alright, because there are several extremely optimized frameworks for python development, like Django, webpy, and pylons.
Why do I use Python? I have been programming for about eleven years and have tried C, C++, common lisp, scheme, clojure, java, c#, vb6, vbnet, javascript and nodejs, OCaml, python, perl, ruby, php, bash as well as a variety of other languages. In fact, I've written a lightweight language of my own based on OCaml for functional programming. I've learned first hand that different languages have different advantages.
The JVM is great because it is cross-platform, and thus it is worth knowing a language like Java, Scala, or Clojure which compiles to JVM byte code. If you're writing something which has to be highly optimized and support million of concurrent users or threats, C has its obvious advantages with low level memory management and fine tune-able MPI. If you're writing a simple administrative script for your shell, might as well code it in bash or perl. But what about web apps?
There's a reason I enjoy using Python for web apps, and it's not because ruby, php, perl, and asp can't accomplish similar tasks. I find Python to be the simplest with the cleanest conventions and syntax, the most powerful libraries, and the most developed web frameworks. Also, python is multi-paradigm which gives the programmer the ability to choose his or her own style without being restricted, be it functional, imperative, or strictly object oriented. I'll show you exactly how easy Python is to use.
Objective: Generate a list of the numbers between 50 and 100 and print them in steps of 5.
Code:
for i in range(50,101)[::5]: print i
Writing Something Useful
Let's write a practical program in python that will actually accomplish something useful. How about defining a simple list?
Code:
x = [1,2,3,4]
y = ["hi", "there", "how", "are", "you"]
This is one huge advantage of a language like Python. It is dynamically typed like perl (although perl uses sigils to represent $ scalars, at symbol for arrays, and % for associative arrays) -- php does similar, ruby, and many other scripting languages.
What about iterating over a list? In Java or c++, your code would look something like this:
Code:
for (int x=0; x<100; x++) {
cout << x << endl; // for c++, assume std libs included
System.out.println(x); // java
}
Code:
mylist = range (100) # mylist is a list from 0 to 99, [0,1,2,...,98,99]
mysum = 0
for x in mylist:
mysum += x
print mysum
Code:
sum(range(100))
So far we've gone over the basics of the language, what the syntax looks like, why python may be a good choice for your next web project, and hopefully you're excited about its capabilities.
In my next post I hope to introduce dictionaries, explain the syntax further (for example, indentation does matter in python), and talk more about the type system.
Comments, suggestions, feedback, and questions -- as always -- would be greatly appreciated. Hope you learned something useful!
Last edited: