Intro to Python: Hello World

In Programming, Python by timfanelliLeave a Comment

Well I’ve said once or twice that I’ve finally taken up learning Python… so far I’m really impressed with the language, although I’m not overly familiar with it yet. Since the purpose of my site was originally to post tutorials about the things that I’m learning — you’ll all get to learn Python with me. This serves the dual purpose of getting my site back on track as a technology and programming blog, as it was originally intended… unfortuantely since I completed my M.S. (effectively ending my J2EE research for the time being), this site has become a place to post about my cat. And I know none of you want to read about that.

So back to basics — today I wrote my first CGI Hello World in Python. I’ll go over the very basics of the language here and show a regular Hello, World! example, as well as some other code examples to illustrate some basics. Sorry to all you Planet Cosiites who already know this stuff.

The first major point about the Python language is that it is not freeform. Coming from the freeform world C/C++/Java i thought this would be annoying and inconveniencing, but it’s actually very natural and elegant. The statement delimiter (“;” in C/C++/Java) is a line-break in Python, and code-blocks are identified by their indentation. I think this is great — especially for new programmers; there’s no “remembering the semicolon” or getting lost finding the semi-colon in multi-line statements; and it forces you to tab your code. As a former TA of Software Engineering at Clarkson — I can attest to the fact that one of the most common mistakes in new-programmers is forgetting that damn statement-delimeter… and when they do remember it, their style is typically non-existent (left-aligned all the way… it’s like they’re using MS Word as a code-editor…).

The next thing to know about Python is that the language is dynamically typed, and strictly typed. This means that a variable’s datatype is determined at runtime by its value; and that an objects type can not be changed. To give examples of other languages: C, C++, and Java are statically typed languages* — a variables type is declared explicitly in the source. VB is not strictly typed, meaning that when you declare a variable intending to use it as a string, you can also use it as an int, or any other type, without casting.

* Yeah yeah, object-oriented languages support dynamic typing through polymorphism…. but we’re keeping it simple here, k?

So without further ado, my hello world 🙂

def sayHi( name ):
	print "Hello, " + str( name ) + "!"

sayHi( "Derf" )

def name declares a function called name. The parameter list is required, but may be empty; notice that the type of the name variable is not declared, that’s because it’s dynamically typed. The str( x ) function that you see takes any object, and makes a string out of it. This example would still work great without using str, but if I were to do something stupid and pass an integer type in, I’d get a runtime exception for trying to use the + operator on mixed types. The code outside of the method declarations is the “main” part of the program. This is analogous to writing the “main” method of a C or C++ program.

So that’s the very very basics. One more thing about the language for today, some basic data structures: lists and maps; Python has amazing built in support for these data structures. Since this isn’t an intro-to-programming tutorial, I’m just going to show some basic examples of how to use lists and maps, and also show you method delcarations and some basic looping, without the fuss:

def createList():
	mylist = []
	for x in range(10):
		mylist += [ x ]

	return mylist

def createMap():
	mymap = {}
	for x in createList():

squares = createMap()
for x in squares.keys():
	print str(x) + ": " + str( squares[x] )

When this program is run, the output is as follows:

Timothy-Fanellis-Computer:~ timfanelli$ python
0: 0
1: 1
2: 4
3: 9
4: 16
5: 25
6: 36
7: 49
8: 64
9: 81
Timothy-Fanellis-Computer:~ timfanelli$ 

Note that the function’s return type is also dynamically typed… you can return anything you want from a function; and what you return determines its type. the createList method returns a list, therefore I can can use it as shown in the createMap method as a list… Now me, I’ve always been a stickler for being explicit — I like declaring types statically; however I have to say, I’d almost prefer having meaningful names for methods that imply their type. I’m still kind of torn on this issue… Maybe I’ll post a commentary about it separately.

So there you have it, my very brief, overly simplified, intro to python. This originally started as my “Hello World, CGI” post, but it’s gotten kind of long winded. I’m going to end this one here and start a new one. Hopefully this will be the start of a long-running Python series for developers interested in getting started with the language.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.