01 Hello, world

[python] Lesson 1: Hello world

Welcome to Beginning Programming, with Python!

This course is intended for first-time programmers or people with
very little programming experience, though all levels are welcome.
Feel free to ask lots of questions! And if someone asks a question
and you think you know the answer, feel free to answer it or start
a discussion.

I'll be posting lessons once a week, on Friday, here on the
Courses list under the subject heading of [python].

When you post, try to make sure you include [python] in your subject line,
so anyone who's subscribed only to this course will see the discussion.
That's especially important if you're subscribed to the digest.

=============== Text editors and other tools =============

For this course, you'll be writing programs in a text editor.
You can't use a word processor, like OpenOffice, because it wants
to put in fancy formatting codes that won't make any sense to Python.

You'll hear programmers arguing endlessly about "vim versus emacs",
but don't worry about it. You can use any text editor you want.
You probably already have gedit installed if you're on a Gnome system,
or Kate if you're on KDE; or few other good options include nano,
leafpad and pico.

Since this is LinuxChix, I'll assume most people are on Linux. All the
Python examples should work on any platform, but I might make other
comments that you'll have to adapt for other platforms. For instance,
Mac or Windows users have different choices for text editor.

We'll be working from the command line for a lot of the course.
So fire up a terminal and let's get started!

=================== Python version ===================

First let's make sure Python is installed. In your terminal window, type:

python --version

You should see something like: Python 2.6.5
Any Python version starting with 2 is fine for this course.
Python 3 is not widely adopted yet, and has quite a few changes;
I'll be teaching Python 2 since that's what most people use.

If you have version 3, you may also have python 2 installed at the
same time. To find out, type at your shell prompt:
python
That's the word python immediately followed by a couple of tabs.
Your shell will autocomplete the command to show you all the python
versions you have available. If any of them start with 2, then you can
use that. For instance, when I say to type python --version, you
might type python2 --version or python2.6 --version.

If python3 is the only python you have, see if your distro has a
python 2.6 or 2.7 you can install (you can keep your 3.0 too).
If 3.0 is the only version available, speak up and I'll try to
include Python 3 equivalents in future lessons.

=============== Writing and running a program ===============

It's finally time to start programming! Bet you thought I'd never get there.

Fire up your text editor and make a file consisting of one line:

print "Hello, world!"

That's the whole program. I bet you can guess what it does. :-)

Save the file -- you might want to call it something like hello.py.
Then, in the shell, make sure you're in the same directory where you
just saved the file, and run it like this:

python hello.py

Congratulations -- you're now a Python programmer!

================= Variables =========================

A program that always prints the same thing is no fun. You need to
have values that can change. These are called variables.

A variable is just a way to stores a value. It might be a number,
or some text, or something else.

For instance, I might have a variable called name that stores my name:

name = "Akkana"

Then I could print it:

print "Hello,", name

Try adding those two lines under (or instead of) your Hello, world line.
Use your own name. :-)
Run it and verify that it works.

=============== Input ================================

You might even want to ask the user their name, so you could print
something different depending on who was running the program. Python
has a way to do that: raw_input.

name = raw_input("What is your name? ")
print "Hello,", name

Why is it raw_input instead of just input? For some reason, Python's
designers decided to use input to mean something more complicated
and, frankly, a lot less useful: it evaluates whatever you type in
as a Python command. You won't use input much, but raw_input is
great when you need to read some input from the user.

That's it for today -- I don't want to make the first lesson too long.
Stay tuned for next week's lesson, on loopy beer.

================= Homework =========================

Normally I'll have programming assignments for homework -- some easy,
some harder. But for this first lesson, I just have a few questions.
Post your answers to the list -- if someone beats you do it, don't
worry about it, you're not being graded on who answers.

1. I'm interested in hearing what version of Python everybody is running,
on which operating system, distro and version. Please post your results!
Especially if python3 is the default, or if you have any other problems
running the examples.

2. Why are there two commas in
print "Hello,", name
? What do you think the difference is between them?

3. Anyone know why the language was named Python?