Latest Blog Post
[ View all ]
Today's lesson is a basic hello world in Python. Also, lesson 0 was unnecessary if you're on a Unix system, which means Ubuntu Linux, Mac OS X, etc., because those systems usually come with Python. That means you just need to open up a shell and enter:
$ python
1. Now enter:
$ print "hello world!"
2. Now enter:
$ print 'hello world!'
Notice that both double and single quotes work. The difference is, if you want to to use a double or single quote in the string, then you can avoid using an escape character if you're using the other one (since it's not a possible delimiter). That is, "hello guido's world!" and 'hello "guido" world' both do what the non-programmer would expect.
3. Open up an editor, preferably one with Python syntax highlighting, such as Kate or Kwrite or of course the editor that comes with Python if downloaded from python.org.
Eclipse also compiles Python, but that's not keeping it simple.
4. Create and save a new file helloworld.py with the contents:
print "hello world!"
.py is the conventional extension for Python, but .anything works.
5. Open up another shell, cd to the directory containing helloworld.py, and enter:
$ python helloworld.py
Ctrl+D will get you out of interpreted mode, but it's useful to leave it up for the ability to quickly test things out when working in compiled mode.
The program should have run and printed:
hello world!
6. Receive three lashes for not writing object-code or even using functions, one for every space-delimited string typed
7. Replace the contents of helloworld.py with:
def hello_world():
print "hello world! (from a function)"
Whitespace is mandatory here. This keeps functions logically-tabbed, and eliminates the need for curly braces. This is so important it should not be written in small font, but just like Python code, this lesson will adhere to strict style guidelines in the name of readability.
8. Save and return to the shell and enter:
$ python helloworld.py
The program should have run and printed nothing as nothing was run.
Code that is outside of functions, conventionally at the bottom of the file, works like the "main" function that runs when one calls the class. If no such code exists, then nothing will be called implicitly.
9. Replace the contents of helloworld.py with:
# the function that does the actual printing
def hello_world():
print "hello world! (from a function)" # useless comment to make clear how to write a comment
# the main line extra-functional program
hello_world()
The program should have run and printed:
hello world! (from a function)
10. Play around with the interpreted mode in the original shell (or recall that typing Python at most Unix based shells starts up the Python command line interface), creating variables and experimenting with types. Here is a sample run:
$ a = 10
$ a = string
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'string' is not defined
$ a = "string"
$ b = a + 10
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: cannot concatenate 'str' and 'int' objects
$ b = a + " concatenation is easy in Python"
$ b
'string concatenation is easy in Python'
$ a = 5
$ c = a + 1
$ c
6
$ d = [a, b, c]
$ d
[5, 'string concatenation is easy in Python', 6]
$ e = d # watch out for the shallow copy bug when doing this one
$ e
[5, 'string concatenation is easy in Python', 6]
$ e + d # array concatenation isn't hard either
[5, 'string concatenation is easy in Python', 6, 5, 'string concatenation is easy in Python', 6]
$ f
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'f' is not defined
Types are implicit in Python, and a variable can change types based on a new implication. If the result of an operation is not caught in a variable, then it is printed after the operation, otherwise the variable's value can be printed by entering it at the prompt.
$ python
1. Now enter:
$ print "hello world!"
2. Now enter:
$ print 'hello world!'
Notice that both double and single quotes work. The difference is, if you want to to use a double or single quote in the string, then you can avoid using an escape character if you're using the other one (since it's not a possible delimiter). That is, "hello guido's world!" and 'hello "guido" world' both do what the non-programmer would expect.
3. Open up an editor, preferably one with Python syntax highlighting, such as Kate or Kwrite or of course the editor that comes with Python if downloaded from python.org.
Eclipse also compiles Python, but that's not keeping it simple.
4. Create and save a new file helloworld.py with the contents:
print "hello world!"
.py is the conventional extension for Python, but .anything works.
5. Open up another shell, cd to the directory containing helloworld.py, and enter:
$ python helloworld.py
Ctrl+D will get you out of interpreted mode, but it's useful to leave it up for the ability to quickly test things out when working in compiled mode.
The program should have run and printed:
hello world!
6. Receive three lashes for not writing object-code or even using functions, one for every space-delimited string typed
7. Replace the contents of helloworld.py with:
def hello_world():
print "hello world! (from a function)"
Whitespace is mandatory here. This keeps functions logically-tabbed, and eliminates the need for curly braces. This is so important it should not be written in small font, but just like Python code, this lesson will adhere to strict style guidelines in the name of readability.
8. Save and return to the shell and enter:
$ python helloworld.py
The program should have run and printed nothing as nothing was run.
Code that is outside of functions, conventionally at the bottom of the file, works like the "main" function that runs when one calls the class. If no such code exists, then nothing will be called implicitly.
9. Replace the contents of helloworld.py with:
# the function that does the actual printing
def hello_world():
print "hello world! (from a function)" # useless comment to make clear how to write a comment
# the main line extra-functional program
hello_world()
The program should have run and printed:
hello world! (from a function)
10. Play around with the interpreted mode in the original shell (or recall that typing Python at most Unix based shells starts up the Python command line interface), creating variables and experimenting with types. Here is a sample run:
$ a = 10
$ a = string
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'string' is not defined
$ a = "string"
$ b = a + 10
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: cannot concatenate 'str' and 'int' objects
$ b = a + " concatenation is easy in Python"
$ b
'string concatenation is easy in Python'
$ a = 5
$ c = a + 1
$ c
6
$ d = [a, b, c]
$ d
[5, 'string concatenation is easy in Python', 6]
$ e = d # watch out for the shallow copy bug when doing this one
$ e
[5, 'string concatenation is easy in Python', 6]
$ e + d # array concatenation isn't hard either
[5, 'string concatenation is easy in Python', 6, 5, 'string concatenation is easy in Python', 6]
$ f
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'f' is not defined
Types are implicit in Python, and a variable can change types based on a new implication. If the result of an operation is not caught in a variable, then it is printed after the operation, otherwise the variable's value can be printed by entering it at the prompt.

just begin all sentences you write anywhere with '#' and that's a line of python (comment) every time ;)
i like what i hear about htis language, but i confess, i haven't yet written a single line of python...
Also, please add pictures of pythons, and any graphic that depicts a python eating a pearl.