1) A first example "Hello World"

#!/usr/bin/env python
#
# This program says "hello" to the world

print "Hello World!"

To execute the python script copy the blue text from the screen and paste it into a file. Save the file, for example, under the name "hello.py". Then type at the Unix prompt:

python hello.py

Explanations
# indicates a comment; the line is ignored
print a command; prints the string between the quotes

Exercises

1.1) Add a second print statement to the script from the example. (For example print "How are you?".)

1.2) Make some random changes, for example, delete the "#" before "This program says ...". In most cases you will get an error message. Fix the errors until the script executes again successfully.

1.3) Type just python at the command line. This starts the interactive mode. You can now type commands. To leave the interactive mode press the control key followd by D. Note: in the interactive mode, you don't need the word print. Just typing "Hello World" will print it.

2) A second example

#!/usr/bin/env python
#
# A program for greeting people

name = raw_input ("What is your name? ")
print "Hello, " + name + "! How are you?"
print "My name is" , name

Explanations
name a variable for a "name"
= an assignment operator
raw_input input a string from the keyboard
+ concatenates strings
, separates elements in a print statement

Exercises:

2.1) What is the difference between using "+" and "," in a print statement? Try it!
2.2) Write a program that asks two people for their names; stores the names in variables called name1 and name2; says hello to both of them.

Operators for Numbers

#!/usr/bin/env python
#
# A program for numbers
#

a = 3 - 4 + 10
b = 5 * 6
c = 7.0/8.0
print "These are the values:", a, b, c
print "Increment", a, "by one: "
a = a +1
print a
print "The sum of", a, "and", b, "is"
d = a + b
print d
number = input("Input a number ")
print number

Exercises

3.1) Execute the script. Make sure you understand every line.
3.2) Write a script that asks a user for a number. The script adds 3 to that number. Then multiplies the result by 2, subtracts 4, subtracts twice the original number, adds 3, then prints the result.

Optional exercise

To execute a python script you can also change the program permissions to executable

chmod u+x program_name

and then execute it by simply typing the program_name.