Last updated: Thursday 4th September 2008, 8:56 PT, AHD


And now for something
completely different . . .

Part 2

Introduction to Programming

using Python

 

2.0 Data processing with Python

2.1 A simple programming problem

2.2 Simple math statements

2.3 Simple output statements

2.4 Data types

 

http://www.python.org/about/quotes/

 

2.0 Data processing with Python

 

All data in a Python program has a data type. The most common types of data are text (e.g.  "Tuesday" and "Good day") and numbers (e.g. 56 and 76.9). Text has to be enclosed in matching quote marks (single, double or treble). For example, the following are all examples of valid text values:

 

 

'Hong Kong'

"Hong Kong"

'''Hong Kong'''

(note the treble quotes are typed in using three single quote marks)

 

A Python program might have the following statement:

 

print "Anne was here"

 

print is one of the keywords of the language, and if you type the line above in Python's IDLE editor window and save the file with a name ending in .py, or simply type the line into a Python Shell window, the word print is shown in orange text - which indicates that this word is one of the keywords of the Python language. Text (anything in quotes) is always shown in green. 

 

When the statement:

print "Anne was here"

is executed by the Python interpreter (i.e. the program runs), this is what is output:

 

Anne was here

 

You can also print out numbers.

Consider this Python statement:

print 45

 

When the statement:

print 45

is executed by the Python interpreter (i.e. the program runs), this is what is output:

 

45

 

Notice, Python always outputs results in a blue font.

 

Data can be input to a Python program and placed into a repository known as a variable:

 

#01-02.py

 

thetext = raw_input("Enter some text ")

print "This is what you entered:"

print thetext

 

In the program script above, thetext is the name of the variable. You can use any name you like for a variable's name, as long as you don't use one of Python's keywords, and the name has no spaces, and starts with a letter.  You can use the underscore character in a variable name, but not as the first character.

 

The following are valid (OK) variable names:

 

thetext

age

salary

tax1

tax2

height

weight

height_in_inches

Weight_In_Pounds

weight_in_pounds

 

The last two variable names are different variable names.

Don't use all capital letters for your variable names.

 

 

 

 

The following are invalid (not OK) variable names:

 

 

2tax                     invalid because starts with a number

weight in pounds   invalid because you cannot have spaces

_weight                 invalid because you cannot start a variable name with a _

 

 

If you've already installed Python on your home computer, or if you're working at the college, you can follow the instructions here to enter and run the following Python program script using Python's IDLE editor.  (Click here if you need to install Python at home.)

 

#01-02.py

 

thetext = raw_input("Enter some text ")

print "This is what you entered:"

print thetext

 

Note: in the program above, the first (red) line is a comment,

and has no effect on the execution of the program.

 

The second line is an input statement.

When the second line runs, Python will first output the text:

 

Enter some text

 

and will then wait for you to enter some text.

If you entered the words:  Hello world!

and then pressed the Enter key, Python then will place the text "Hello world!" in into the variable called thetext

Execution of the second line is then complete.

 

 

When the third line runs,

Python will print out to the screen the text:

 

This is what you entered:

 

When the last line runs, Python will print out to the screen the text:

 

Hello World!

 

 

Note: when a variable is used in a print statement such as:

 

print thetext

 

its value is printed out to the screen - not its name.

 

 

 

2.1 A simple programming problem

 

The Problem:

Find the average of three numbers

 

Before we can solve this problem using a computer program, you really have to understand what the problem is. Most of us understand how to find the average of three numbers.

 

Image you're trying to explain how to do this to someone who has never done it before. Then you have to break the solution down into simple steps, and place them in the correct order. We usually use pen and paper to do this.

 

Once you have your list of steps, you have produced what is known as an algorithm. Some algorithms are short, some are long, but each step is small...

 


Example algorithm to

find the average of three numbers

 

1. Tell the user to enter the first number

2. Add to a running total

3. Tell the user to enter the second number

4. Add to a running total

5. Tell the user to enter the third number

6. Add to a running total

7. Divide the total by 3

8. Print out the result

 

 

 

Once you've written your algorithm, the next step brings you closer to the actual computer program to solve the problem. The next step is to create pseudocode...

 

Pseudocode is a detailed but readable description of what a computer program or algorithm must do, expressed in a formally styled  language which is not an actual programming language. For example...

 

Pseudocode to find the average of three numbers

 

 

set total to 0

until there are no more numbers to process, do this:

            write "Enter a number"

            read number

            set total to total + number

set average to total / 3

write average

 

Algorithm and Pseudocode Example

 

 

Find the average of three numbers:

 

Python Program (02-01.py)

 

total = 0.0

number1=float(raw_input("Enter the first number: "))

total = total + number1

number2=float(raw_input("Enter the second number: "))

total = total + number2

number3=float(raw_input("Enter the third number: "))

total = total + number3

average = total / 3

print "The average is " + str(average)

 

All example Python programs

Using Python

How to Create and Run Python Programs using IDLE

Python Program (02-02.py)

 

number1=float(raw_input("Enter the first number: "))

number2=float(raw_input("Enter the second number: "))

number3=float(raw_input("Enter the third number: "))

total = number1 + number2 + number3

average = total / 3

print "The average is " + str(average)

 

All example programs

Find the average of three numbers

 

 

Python Program (02-03.py)

 

total = 0.0

count = 0

while count < 3:

    number=float(raw_input("Enter a number: "))

    count = count + 1

    total = total + number

average = total / 3

print "The average is " + str(average)


All example programs

Find the average of three numbers

 

 

2.2 Simple math statements

 

result = 1 + 1
result = 2 / 3
result = 3 * 2
students = 10
books = students * 4
x = 5**2
y = (5+9)*(15-7)

 

 

2.3   Simple output statements

 

result = 1 + 1

print result


result = 2 / 3

print result

average = 37

print "The average is " + str(average)

2.4   Data types

 

int       (integer, e.g. 12, 14, 101)
string    (text, e.g. "Anne", 'Anne', "Hello World!")
float     (floating point number, e.g. 3.142, 98.6)

Variables in Python

 

A variable is a name that refers to a value.

The assignment statement creates new variables and gives them values:

message = "Hello World!"
print message

Legal Variables names in Python:

 

message
result  
student1

student2
student_2
max_temperature
last_name
final_exam_score

Python's 28 keywords:

 

and  continue  else  for  import  not  raise
assert  def  except  from  in  or  return
break  del  exec  global  is  pass  try
class  elif  finally  if  lambda  print  while


Note: keywords cannot be used as variable names

This Presentation uses the following program files:

 


http://www.annedawson.net/02-01.py

http://www.annedawson.net/02-02.py

http://www.annedawson.net/02-03.py

 

All example programs

 

 

 

End of Python_Intro.htm