Download Data types - NSW Department of Education

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Reading: Apply script language syntax and concepts
Apply script language syntax and
concepts
Contents:
Data types
2
Numerical types
4
The string data type
5
Data structures
7
Functions
8
Objects and methods
11
Code libraries
12
Interfacing with the operating system
13
Summary
18
1683_reading.doc
© State of New South Wales, Department of Education and Training, 2006
1
Reading: Apply script language syntax and concepts
Data types
All practical programming languages need to deal with different ‘types’ of
data. For example:
Integers like 1, 2, -10 are used to count things, or for simple integer
arithmetic:
seconds_since_midnight = 32334
Numbers like 1.23 are used for engineering or commercial calculations:
purchase_price = 34.99
Often we need to store text:
address = ‘26 Mount Street’
Boolean values can represent whether some condition is True or False:
high_temperature_alarm = True
Sometimes we need to store multiple items
shopping = [‘eggs’, ‘newspaper’, ‘coffee’]
Sometimes we want to store a range of information about the same thing:
patient.name = ‘Fred Bloggs’
patient.age = ‘34’
patient.condition = ‘diabetes’
In the study of programming languages, the notion of a data type is very
central. The types of a language determine what sorts of values can be used
in a program, and the legal operations that can be performed.
For example, two of the basic data types in Python are:

Integer

String
This means that we can use values of these types, and assign them to
variables. In the example below, the variable "city" is initialised with a
string, and the variable "year" contains an integer:
city = 'Sydney'
year = 2006
# city currently holds a string
# year contains an integer
Note that the type defines what operations are legal. So we can use the
addition operator on integers:
print year + 1
# will print 2007
But if we try to add an integer to a string, a runtime error will result:
print 'abc' + 1
# gives a type error
The error printed will be something like:
TypeError: cannot concatenate 'str' and 'int' objects
2
1683_reading.doc
© State of New South Wales, Department of Education and Training, 2006
Reading: Apply script language syntax and concepts
The various operator symbols like ‘+’ and ‘*’ may have different meanings
for different types. For example, the ‘+’ operator can be used to combine
two strings, but the meaning is concatenation (joining strings) rather than
numerical addition:
print 'abc' + 'def' # will print abcdef
Similarly, in Python it is legal to ‘multiply’ a string by an integer, but the
meaning is of course different from numerical multiplication. The code
below will print the string ‘abcabcabc’, in other words, three copies of ‘abc’
joined together:
my_str = 'abc'
my_int = 3
print my_str * my_int
In addition to the built-in types, most languages also allow the programmer
to create abstract data types of their own, and define operations upon them.
This is one aspect of object-oriented programming.
Dynamic versus static typing
Some languages such as C++ and Java are statically typed, which means
that the types of all variables must be stated when the variable is defined.
For example:
int k;
// create an integer called k
Note that this means that the variable itself is given a type, so that we cannot
now go:
k = 'abc';
// error - k can only hold integers
Python and most other scripting languages are dynamically typed, which
means that variables do not have a fixed type. The type of a variable at any
time is determined by the type of the value it contains. So in Python it is
perfectly legal to assign different types to the same variable during the
program:
k = 2
k = 'abc'
k = True
# k contains an integer
# k contains a string
# k contains a Boolean
Dynamic typing has the advantage of convenience in that you don’t have to
define every variable first, and it gives more flexibility and generality. On
the other hand, static typing allows the compiler to detect type errors at
compile time, and helps to prevent bugs caused by incorrect types.
Note that the trivial example above is not a good example of the advantages
of dynamic typing. To use the one variable for different purposes during a
program is bad practice. Here is an example that gives some idea of the real
flavour of dynamic typing, and its advantages for rapid development. Don’t
worry if you don’t understand it in detail:
myList = [1, 'abc', True, 1+3j, ('abbot', 'costello')]
for thing in myList:
print thing
1683_reading.doc
© State of New South Wales, Department of Education and Training, 2006
3
Reading: Apply script language syntax and concepts
The list called myList contains 5 different types of things. Each of these
values is assigned to variable ‘thing’ in turn, and printed.
For more information about data types, start at the Wikipedia article ‘Type
Systems’.
Numerical types
Consider the Python code below:
x = 2
y = 2.0
z = x + y
There is quite a lot going on behind the scenes in this example when the
code is interpreted:

The Python interpreter ‘understands’ that 2 is a number, specifically
an integer, and it has a way to represent integers in binary.

The interpreter allocates a place in memory to put the variable ‘x’.

The variable x has to hold an integer value, so its type is integer in
this case.

The interpreter will represent the number 2.0 differently to 2, in a
format called ‘floating point’, which allows non-integer decimal
values to be stored.

Thus variable y must be of type floating point, so it can hold the
value 2.0

The interpreter must know how to add the values in x and y. In this
case, the integer value in x is first converted to a floating-point
value, then the two values are added together to give the new
floating-point value 4.0.

This new value is then stored in z, which must be a floating-point
type.
Some languages like Pascal, ADA, and Java are known as ‘strongly typed’
languages. This entails a few things, including:
4

Variables must be declared to be of a particular type before they are
used.

Only data of the specified type can be stored in a variable.

Conversions between types (for example integer and floating point)
must be explicit. The compiler or interpreted will not make any
guesses about what you want to do.
1683_reading.doc
© State of New South Wales, Department of Education and Training, 2006
Reading: Apply script language syntax and concepts
Scripting languages may allow data of different types to be stored in the
same variable. To do this, the interpreter determines and redefines the type
of a variable based upon the type of the data being stored.
Most scripting languages are ‘dynamically typed’. This means that they will
make decisions at run-time about automatic type conversions and so on. So
for example, Python will allow expressions such as:
x = 1
# x is an integer with value 1
x = x + 0.23
# x is now a floating point 1.23
x = str(x) + 'abc' # x is now the string '1.23abc'
A note about the division operator in Python
What do you think will be printed by the following python code?
print 4 / 2
print 5 / 2
# result is 2
# result is 2
The first answer is 2 (as you might expect), but the second answer is also 2
(you may not have expected this). What is happening here is called ‘integer
division’, which means the quotient and remainder type division you would
have learnt in school. This happens because both numbers are integers. If we
want to get a floating point result, we must make one of the numbers a
floating point number as shown below:
print 5 / 2.0
print 5.0 / 2
print float(5) / 2
# prints 2.5
# prints 2.5
# explicit conversion - prints 2.5
Now the above behaviour is quite familiar to programmers, but it can cause
lots of problems for beginners. Therefore the designers of Python have
decided to change this behaviour in the near future. In the next version of
Python, the operation 2/5 will produce a floating point number, and if you
really want integer division, you will have to use a new operator ‘//’ like
this:
print 5//2
# new integer division operator (prints 2)
For more details on numeric types and expressions supported by Python,
see:

The Python Tutorial  3 Informal Introduction  3.1.1 Numbers

Non-Programmer's Tutorial  Hello, World  Expressions

Python Library Reference  2.3 Built-in Types  2.3.4 Numeric
Types
The string data type
Strings are text data made up of a sequence of characters. A string is
declared by enclosing the characters in quotation marks.
1683_reading.doc
© State of New South Wales, Department of Education and Training, 2006
5
Reading: Apply script language syntax and concepts
For example, here we make a variable called country, and assign to it the
string ‘Australia’:
country = 'Australia'
Python strings can use either single quotes (‘) or double quotes (‘). This
allows us to have single quotes, (or apostrophes) inside a string by enclosing
the string in double quotes, and vice-versa:
script = 'Jan said "Hello" and "Welcome" '
saying = "It's a mad world, that's for sure"
Here are some typical operations we perform with strings. We can print this
string in the same way as a number:
print country
To find the length of a string, use the len() function.
myString = 'abcd'
length = len(myString)
print length # prints 4
We can concatenate strings by using the ‘+’ operator. To concatenate means
to form a longer string by placing one string after another. Note that this
operation is very different to adding two numbers. The interpreter knows the
type of any variable or expression, so it knows which operation to perform.
str1 = 'Hello'
str2 = 'World'
str3 = str1 + ' ' + str2
print str3
# will print 'Hello World'
Parts of strings can be accessed using indexing or slices. For example:
str =
print
print
print
'The rain in Spain'
str[0]
# will print 'T'
str[4]
# will print 'r'
str[1:5]
# will print 'he r'
To check whether a string contains an occurrence of some other string, we
can use the find method. This method returns the index at which the other
string begins:
str = 'The rain in Spain'
location = str.find('Spain')
print location
# prints 12
There are many more methods that can be applied to strings. For more
information about strings, see:
6

The Python Tutorial  3 Informal Introduction  3.1.2 Strings

Non-Programmer's Tutorial  Hello, World

Non-Programmer's Tutorial  Revenge of the Strings

Python Library Reference  2.3 Built-in Types  2.3.6 Sequence
Types
1683_reading.doc
© State of New South Wales, Department of Education and Training, 2006
Reading: Apply script language syntax and concepts
Data structures
Data structures allow us to collect different data values together in a unified
structure.
The most important Python data structures are lists and Dictionaries.
Lists
Python lists are somewhat like arrays in Java, but more flexible. We can
store different types of values in a list, and the list can grow in size as we
add more items.
Python lists allow us to store multiple data items in one place. Strings,
numbers and other data objects can be elements of the same list. The
following example defines a list with 4 elements; the first two are strings
and the last two are numbers.
myList = ['spam', 'eggs', 100, 1234]
To access individual elements in a list, an index is used. The following
prints the third item (the number 100) in the list. Note that the first item has
index 0.
print a[2]
# print 100
Lists can have items added to the end of them using the append function, or
by using the ‘+’ operator with a second list.
groceries = ['apples', 'eggs']
groceries.append['spam']
# groceries now contains ['apples', 'eggs', 'spam']
groceries += ['bread', 'sugar']
# groceries now contains:
#['apples', 'eggs', 'spam', 'bread', 'sugar']
For more information about Python lists, see:

The Python Tutorial  3 Informal Introduction  3.1.4 Lists

The Python Tutorial  5 Data Structures  5.1 More on lists

Dive Into Python  3 Native Datatypes  3.1 Introducing
Dictionaries

Non-Programmer's Tutorial  Dictionaries

Python Library Reference  2.3 Built-in Types  2.3.6 Sequence
Types
1683_reading.doc
© State of New South Wales, Department of Education and Training, 2006
7
Reading: Apply script language syntax and concepts
Dictionaries
Dictionaries are sometimes called ‘associative arrays’ or ‘hashes’. The idea
is that each value we place in the dictionary has an associated key. For
example, let’s say we want to store the telephone numbers of friends, and be
able to find a number using the name of the friend. This is simple with a
dictionary—we simply use the names as the keys, and the phone numbers as
the values to store.
# define an initial phone list
phoneList = { 'jack':23445, 'jill':12334, 'mack':56778 }
# add a new number
phoneList['Sara'] = 56678
# retrieve a number
print phoneList['jack']
# print phoneList.keys()
# print phoneList.values()
The above code will print:
23445
['Sara', 'jill', 'mack', 'jack']
[56678, 12334, 56778, 23445]
Note that the keys and values in the dictionary are not stored in any
particular order. All that matters is that you can retrieve the value associated
with any particular key.
For more information about Python dictionaries, see:

The Python Tutorial  5 Data Structures  5.4 Dictionaries

Dive Into Python  3 Native Datatypes  3.1 Introducing
Dictionaries

Non-programmer's Tutorial  Dictionaries

Python Library Reference  2.3 Built-in types  2.3.7 Mapping
Types
Functions
Computer algorithms are easier to understand and implement if they are
broken down into manageable, meaningful parts. The idea is that we will
write a block of code, and give it a name, so that we can execute this block
of code just by mentioning the name somewhere else in the program. In
Python we call this block of code a ‘function’, and when we execute the
function by mentioning its name, we say that we are ‘calling’ the function.
8
1683_reading.doc
© State of New South Wales, Department of Education and Training, 2006
Reading: Apply script language syntax and concepts
Below is a simple example of a function that prints two lines. Note that the
def keyword indicates the beginning of a function definition:
def lumberJackChorus():
print "I'm a lumberjack and I'm OK"
print 'I sleep all night and I work all day'
Whenever we want to print those lines, we simply call the function like this:
lumberJackChorus()
Note that we can call the function from anywhere in our program, and as
many times as we like. A function like this has the advantage that we can
simplify our code. Whenever we need to print the chorus, we simply call
this function as shown.
We can pass values to functions
You probably noticed that the function definition above, and the call, used
parentheses after the function name with nothing in between, like this:
lumberJackChorus(). You always need these brackets when defining
or calling a function.
Usually however, the brackets will not be empty but will contain a list of
input values for the function to work with. Here is an example (note that the
backslash ‘\’ at the end of the fourth line just allows us to continue the
statement on the next line.):
# lumberjack chorus with parameters
def lumberJackChorus(dayTimeActivity, nightTimeActivity):
print "I'm a lumberjack and I'm OK"
print 'I ' + nightTimeActivity + ' all night' \
+ and I ' + dayTimeActivity + ' all day'
# different versions of the chorus
lumberJackChorus('work', 'sleep')
lumberJackChorus('sleep', 'party')
Now, we have a more useful chorus function, because we can modify the
day time and night time activities of our lumberjack! So the first call to our
function will print:
I'm a lumberjack and I'm OK
I sleep all night and I work all day
But the second will print:
I'm a lumberjack and I'm OK
I party all night and I sleep all day
Let's have a closer look at what is happening here. The first line of the
function definition defines two 'formal parameters', dayTimeActivity and
nightTimeActivity. These parameters are much like variables that will be
initialised when the function is called.
def lumberJackChorus(dayTimeActivity, nightTimeActivity):
1683_reading.doc
© State of New South Wales, Department of Education and Training, 2006
9
Reading: Apply script language syntax and concepts
So when the function is called like this:
lumberJackChorus('work', 'sleep')
...the parameter dayTimeActivity will be a string with value 'work', and the
parameter nightTimeActivity will be a string with value 'sleep'. These
parameters are used within the function to print the appropriate value.
Functions can return a value
Not only can we pass values to a function as parameters, but a function can
return values back to the ‘place’ where it was called. The classic examples
of this are the math functions such as sin, or sqrt (square root), or abs
(absolute value). So for example, we can calculate the square root of four by
using:
# The sqrt function is found in the math module,
# so first we need to import it at the top of our program
from math import sqrt
# now print the square root of 4
print sqrt(4.0)
In this example, the sqrt function has accepted the value 4 and will return
the value 2.0, which will in turn be printed by the print statement. Note the
effect of the line:
print sqrt(4.0)
is just as if we had written:
print 2.0
Now let's write our own function that returns a value.
# add three to any number
def addThree(x):
return x + 3
The return statement returns (sends back to the caller) a result. In this case,
the result returned is simply a number that is three more than the parameter
x. So for example if we call the function like this:
print addThree(5)
...then the value printed will be 5 + 3 = 8.
Note that the effect of this call is the same as if we had written '8' instead of
'addThree(5)'. The expression 'addThree(5)' has the value 8. Thus we can
use function calls just like any other variable in an expression. Here is
another example using our addThree function:
a = 2
b = addThree(a)
print 'a =', a, '
b =', b
In this case, the value of variable 'a' (that is, 2) is passed to the function, and
the value 2+3=5 is returned. This value is then assigned to variable 'b'. So at
the end of this code, 'a' has the value 2, and 'b' has the value 5. Note
10
1683_reading.doc
© State of New South Wales, Department of Education and Training, 2006
Reading: Apply script language syntax and concepts
carefully that the value of variable 'a' is not changed by the function. If we
want to modify the variable 'a', we would need to go:
a = addThree(a)
Functions can contain arbitrary code
So far, we have just shown some very simple calculations within a function.
By you can define variables, and put any statements you like inside the
function. You can even define more functions inside a function if you need
to!
Let’s look at a few more examples of functions. The example below defines
a function that finds the maximum of two numbers. Here we have used an
if-statement to check which of the two parameters is largest.
# find the maximum of two numbers
def maximum(a, b):
if a > b:
return a
else:
return b
print 'The maximum of 3 and 4 is:', maximum(3, 4)
For more information about functions, see:

The Python Tutorial  4 More control flow tools  4.6 Defining
functions

Non-programmer's Tutorial  Defining functions
Objects and methods
The most significant development in programming practice in the last
decade is the widespread adoption of object-oriented programming. This
unit does not cover the details of object-oriented programming, however
most modern scripting languages do support object-oriented programming.
You can avoid object-oriented programming in Python if you wish (that is,
you don’t have to define your own classes), but you will need to use objects
when using the Python library, or other modules written for specific
purposes.
For our purposes, you can think of an object as a collection of attributes
(data items) and methods (function calls). The most important thing to need
to know to work with objects is how to access their methods and attributes.
This is very simple to do using dot (‘.’) notation.
For example, you will need to use the built-in file type to open and close
data files on your computer. To open a file called ‘my_data.txt’ for reading,
you can use:
1683_reading.doc
© State of New South Wales, Department of Education and Training, 2006
11
Reading: Apply script language syntax and concepts
f = file('myData.txt', 'w')
f.write('abc')
f.close()
# open the file
# write some data
# close file
In the first line, a new file object is created. This causes the 'myData.txt' file
to be opened for writing (or created if it did not already exist). We can then
write to the file by calling the write method of object, and then close the file
by calling the close() method.
For simple use of objects, you don't need to know much more than this.
When faced with programming tasks with objects, you will typically follow
these steps:

Read the documentation about the class, and find out how you can
make it do your bidding by accessing its methods and attributes.

Import the module containing the class code into your program using
an import statement at the top of your program.

Create an object of the required type. For example, if the class is
called 'dog', and it exists in module called 'animals', you could create
a new dog object by calling animals.dog() with the required
parameters, for example:
myDog = animals.dog('labrador')

Once you have your object, you can call the methods of the object,
or access any of its public attributes. For example, lets assume our
dog class has methods to bark, and to set the colour of the dog:
myDog.bark(2)
# bark twice
myDog.setColor('black') # now we have a black labrador
For more information about object oriented programming, see:

The Python Tutorial  9 Classes
Code libraries
Much of the power of a programming language comes from the libraries that
other programmers have already written for you. Python comes bundled
with a large set of general purpose modules, including those for math
functions, for string manipulation, operating system services, multimedia,
cryptography, web programming and so on. Many other modules are
available for download from the Python website, and from other locations
on the Web.
A code library is a collection of program code sections defined as functions
or sub programs that may be used in the development of a script. The
functions are often bundled together as a package that may be referred to by
a script to access the individual functions contained in that package.
12
1683_reading.doc
© State of New South Wales, Department of Education and Training, 2006
Reading: Apply script language syntax and concepts
You must use the import statement at the top of a script to allow your code
to use the functions and objects defined in that module. The following
example uses the today() function that is contained in the datetime library.
from datetime import date
now = date.today()
print now
For more information about the Python code library, consult the Python
library Reference.
Interfacing with the operating system
To accomplish any practical programming task, our programs must work
with the operating system that they are running under. So far, our programs
have interacted with the operating system in rather trivial ways, by printing
text to the console window, and reading text typed on the keyboard. In this
section, you will learn about manipulating things that are beyond your
program’s internal data and code.
Command line arguments
A ‘command line interface’ is an old-style textual interface to the operating
system functions. On windows, you can get a command line interface by
choosing ‘Run…’ from the start menu, and typing ‘cmd’. This will open a
console window where you can type in commands and see the results.
For example, we can copy one directory to another using the xcopy
command. In this case, the command is ‘xcopy’, and its arguments are ‘aaa’
and ‘bbb’:
xcopy aaa bbb
Here’s what happens when the operating system sees this command. First it
works out that the command is called ‘xcopy’. Then it finds and loads the
executable ‘xcopy.exe’ into memory, sets it up and starts it running. The
operating system makes the command line arguments available so the xcopy
program can get the filenames it needs to do its job. The exact mechanism is
different for each operating system, but we don’t need to worry about these
details.
Often we want to write scripts that take command line arguments, and work
in a similar way to operating system commands. The data (numbers, text,
filenames, etc.) that the script requires is passed to the script at the time the
script is executed rather than being ‘hard-coded’ in the script, or being
entered by the user after script has started. This allows for flexible scripts
that can do their work on a variety of input data instead of working with the
same data each time the script is executed. It also allows for the script to be
passed the arguments by another process (eg another Python script or
operating system command script).
1683_reading.doc
© State of New South Wales, Department of Education and Training, 2006
13
Reading: Apply script language syntax and concepts
Lets say that we wrote the xcopy program as a Python script, instead of
relying upon the standard windows command. Here is how the command
line would look when we invoke the script:
python xcopy.py aaa bbb
There are a few differences between running executable programs and
running a script. An executable program can be run directly, but a script
must be interpreted. So in the above command line, the executable program
is actually the interpreter itself (python.exe), and our script filename
(xcopy.py) is a command line argument to the interpreter!
The interpreter, in turn, makes its command line available to the script. In
Python, we can access the command line arguments using a list variable
called ‘argv’ in the ‘sys’ (system) module: in other words, ‘sys.argv’.
Here is a little two-line program that prints the sys.argv variable. We have
called it ‘arguments.py’.
# program arguments.py
import sys
print sys.argv
If we run this program with the command:
python arguments.py aaa bbb ccc ddd
...then we would see something like:
['arguments.py', 'aaa', 'bbb', 'ccc', 'ddd']
Note that the square brackets indicate that a whole list is being printed. Each
item in the argv list is a string, and the first element is the name of the script
itself.
If we want to access individual arguments, we must use their indexes within
the argv list. For example to access the second argument (bbb), we need to
access the third element of argv, which has index 2 (remember that the first
element of a list has an index of zero).
# program arguments.py
import sys
print sys.argv[2]
The important fact is that the arguments passed to a script on the command
line are available to the script. By using this mechanism, we can write
scripts that can behave differently depending upon the options chosen.
File operations
Files are essentially data that is given a name and stored on some secondary
storage medium like hard disks and flash drives. Some operating systems
allow quite complex operations on files
The operating system provides operations that allow programmers to do
various things with files. The most common operations are listed below.
14
1683_reading.doc
© State of New South Wales, Department of Education and Training, 2006
Reading: Apply script language syntax and concepts
Opening a file
This means that you specify the name of a file that you want to work with,
and the type of operations you intend to perform (read or write). The
operating system checks whether it’s OK for you to open the file in the way
you want, and returns a token if it approves.
To open a file in Python, use the file() function. In the example below, we
open a file called ‘myFile.txt’ for writing. The second parameter is called
the IO mode; in this case the ‘w’ means that the file should be opened for
writing.
f = file('my_file.txt', 'w')
f.close()
If you open a file for writing which does not exist, it will be created for you.
Therefore if you execute this one-line program, you should see the file
‘my_file.txt’ appear in the current directory where you ran the script from.
File opening modes
There are a number of file opening mode strings. Here are the main ones:
Mode String
Meaning
‘w’
Write. Open the file for writing. If the file does not
exist, it will be created. If the file exists, it will be
truncated to 0 bytes.
‘r’
Read. The file must exist or an exception is raised.
‘a’
Append. Similar to write, except that the file is not
truncated, and the strings written are appended to the
end of the file. Can be used to accumulate data as in
log files.
‘rb’, ‘wb’,
‘ab’
Same as read write or append, but with files where
there is no special handing of end of line characters as
with text files. Used for files containing data that is not
to be interpreted as text. (Note that the ‘b’ stands for
‘binary’ but this is a misnomer—all data is ultimately
stored as binary.)
Closing a file
It is important to close a file after you have finished with it, as we have done
in the previous example. The close operation ensures the actual physical file
is updated with your changes, and releases any resources that the open file
was using. If you fail to close a file, the following bad things can happen:

Operating System Resources (memory etc) associated with the file
may not be released.
1683_reading.doc
© State of New South Wales, Department of Education and Training, 2006
15
Reading: Apply script language syntax and concepts

The operating system may prevent another process from opening the
file.

Any changes you have made to the file may be lost. (Or worse, some
changes may be lost, and some earlier ones may be kept.)
Writing to a file
Once we have opened a file for writing, it is a simple matter to write text
into it:
f = file('my_file.txt', 'w')
f.write('This is some text')
f.write('Another line of text')
f.close()
If you execute this example, then open the ‘my_file.txt’ file with a text
editor, you will find the text that was written by the program.
Reading from a file
Let’s now make a program read the lines of text we wrote in the previous
example.
There are a few ways to achieve this. First, we can read the whole file in one
gulp:
f = file('my_file.txt', 'r')
buffer = f.read()
# read the whole file
print buffer
# print everything
f.close()
In Python, the lines of a file can be accessed very conveniently using an
iterator as shown below. We don't even have to mention a read statement!
f = file('my_file.txt', 'r')
for line in f:
print line
f.close()
We could also do this using the readline() method, but this is not quite so
elegant:
f = file('my_file.txt', 'r')
while(True):
# loop forever
str = f.readline()
# read the next line
if str == '':
# end of file ?
break;
# yes - exit loop
print str,
# print this line (without a newline)
f.close()
In this version the while(True) loop would continue to loop forever if we did
not provide another way to exit the loop. We must exit when the end of the
file is reached, and we can tell when this happens because the readline()
method will return an empty string ('').
16
1683_reading.doc
© State of New South Wales, Department of Education and Training, 2006
Reading: Apply script language syntax and concepts
Moving around in a file
A file is conceptually just a simple sequence of bytes. The system keeps
track of the current location in the file. When a file is first opened for
reading or writing, the current location is at the beginning of the file.
Opening a file for appending sets the current location to the end of the file.
To move to a particular place in a file directly without reading everything up
to that place, you can use the seek function. The following program seeks
forward three bytes and reads the next four bytes. So if the file begins
‘abcdefghi…’, then the program will print ‘defg’.
f = file('my_file.txt', 'r')
f.seek(3)
bytes = f.read(4)
print bytes
f.close()
Line handling in different operating systems
It is an unfortunate fact that there are several different ways to mark the end
of a line. UNIX-like systems use the newline character, Macintosh systems
use a carriage return character, and Microsoft Windows or MS-DOS
systems use both a carriage return followed by a linefeed.
When reading text files, Python can be made to accept any of these
combinations as an end of line marker, by using the ‘Universal’ reading
mode ‘U’ instead of ‘r’.
Writing numbers to files
Numbers require conversion to a string before they can be written to a text
file. So if you try something like:
f = file('my_file.txt', 'w')
my_data = 3
f.write(my_data)
# error: 3 is not a string!
f.close()
you will get a runtime type error. You can solve this by first converting the
number to a string using str() like this:
f = file('my_file.txt', 'w')
f.write(str(my_data))
# error: 3 is not a string!
f.close()
Alternatively, the print statement can be used to write to a file using the
following syntax:
print >> file, data1, data2, data3
…where file is a file object, and data1 etc. are the values or variables
that you want to print, separated by commas. For example:
f = file('my_file.txt', 'w')
my_data = 4
1683_reading.doc
© State of New South Wales, Department of Education and Training, 2006
17
Reading: Apply script language syntax and concepts
print >> f, 'hello', my_data, 7.8
f.close()
If you want even finer control over the format of your strings or text files,
you can use special formatting functions. Search for ‘sprintf’ in the Python
documentation.
Summary
18

Numbers in Python are either integers or floating point numbers.

Text values are represented by the string data type.

Objects methods and attributes are accessed using the ‘dot’ syntax
object.method() or object.attribute.

The Python data structures are lists and dictionaries.

Python lists are similar to arrays, except they can contain values of
any type, and their length is not fixed.

Functions are sections of code defined with a function name and are
used facilitate the reuse of code. They may be available as a built-in
function, defined in the current script, or part of a library or package.

Command line processing provides a method for passing parameters
to scripts when the script is executed.

Files processing operations include opening files in various modes,
reading data from a file, and writing data to a file.
1683_reading.doc
© State of New South Wales, Department of Education and Training, 2006