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.
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...
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...
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
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)
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)
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)
result = 1 + 1
print result
result = 2 / 3
print result
average = 37
print "The
average is " + str(average)
2.4 Data types
http://www.annedawson.net/02-01.py