Elements of Python programming: Difference between revisions
Line 223: | Line 223: | ||
mynewdata.close() | mynewdata.close() | ||
The close() is essential because without the computer's buffers may not flush the contents of the file to the disk. | The close() is essential because without the computer's buffers may not flush the contents of the file to the disk. | ||
Whenever you are writing data to a file, it may be formatted with the same techniques used in formatting displayed data. | |||
== Numbers, text, and data types == | == Numbers, text, and data types == |
Revision as of 06:51, 12 February 2013
Now that you have Python running, and have seen how it works interactively and with executable files, let's explore what we can do with simple useful programming. Some essential topics are
- Getting data into and out of a program
- Storing data as numbers and text
- Accessing data efficiently in lists, tuples, and dictionaries
- Performing logical and mathematical operations on data
- Controlling program flow (coming up in the next section)
Input and output
Python accepts data from the command line when it starts an application, locally stored files, files or other input from the web, through ports -- typically as serial or TCPIP data, or from attached instruments that communicate through specialized device drivers.
Input from a console and keyboard
To have a Python program accept data from a user at a console, include lines like these in Python 2.7
newtext = raw_input() print newtext
to take the raw input as text and print it. You can prompt for the input too
newtext = raw_input('Write what you like: ') print 'This is what I like: ', newtext
In Python 2.7 there is also a Python command "input()" which treats incoming text as Python code. With Python 3.0 this has changed, so some care is needed if you write for the new Python. In that case, rather than raw_input() you would use input(), and to get the effect of the old "input()", you would use eval(input()). You can see why using the newer Python 3.0 with older programs can raise some problems, though they are usually easy to fix.
The input is text, but suppose we want a number instead. If we know it's a number, then
newtext = raw_input('Input a number >> ') x = float(newtext) print 'My number was ', x
should do it. But, if you try this and input text that is not a number, the program will generate an error and respond with something like this
python input_number.py Input a number >> x Traceback (most recent call last): File "input_number.py", line 2, in <module> x = float(newtext) ValueError: could not convert string to float: x
How would we know if we have a number, given arbitrary text in the data, and avoid this error? One way is to use isdigit() --
newtext = raw_input('Input a number >> ') if newtext.isdigit(): x = float(newtext) print 'My number was ', x else: print 'That is not a number.'
In this you see that isdigit() tests whether newtext is a number. It returns a True or False which is used by the "if" statement to control what to do with the data. We will look at such flow control more later.
You may also want to read data by splitting a line of text into pieces, for example with something like this --
newtext = raw_input('Input the item name and quantity >>') print newtext.split()
When to this last one you input "eggs 12", newtext.split() will return ['eggs','12']. That is, it makes a list of the items that are on the line. You can now go through that list and look for the information you want, one entry at a time.
Printing to the display
When you have data or text to display, you'd use a "print" function to have the data appear on the console as the program executes. (In Windows, you may follow this with a raw_input() (in Python 2.7, or input() in Python 3) so that the console will not disappear before you read it.) In Linux or MacOS, you would usually run Python from a console, and the printed information appears on the display and remains visible after the program has finished. Unix-like environments "print" to the standard output, stdout, and may be redirected to a file. For example running a Python program from the command line that generates output, you could write
python myprogram.py >> myfile.txt
and the output would go into myfile.txt instead of displaying. Similarly, output can be parsed to send the error information to a separate file
python myprogram.py 1> myfile.txt 2>myerrors.txt
sends the stdout to myfile.txt and stderr to myerrors.txt . These options are not available in Windows.
To print text the command is
print 'This will print on the screen.\n'
where the quoted (' and " have the same effect) text is sent. The '\n' is a line feed.
To print variables simply use them in the print statement
x = 1 y = 2 z = 3 h = 'Help me!!' print x,y,z,h,'\n'
will display the values of x, y, or z regardless of whether they are numbers or text.
1 2 3 Help me!!
Of course, printing can be formatted. If you are familiar with C or Fortran, you'll find similarities that will help creating formatted output. I in this instance, it's also helpful to remember that the "print" function is converting internal data into displayed "text", so the formatting is really a way of controlling how some text and numerical data are mapped onto text that is then displayed.
Formatting is available in both versions 2.7 and 3 in two ways
- Formatting expressions that are like C's printf are commonly used.
- Formatting methods are unique to Python and use operators that act on text (strings).
Before we can really use these effectively we will need to explain what we mean by "strings", "integers", "floats" and other data types. But, here are a few examples that illustrate how this works. From Mark Lutz' Learning Python we have this summary
To format strings using expressions
- Insert a % "operator". To the left of it, put a string that that is operated on by the instruction that immediately follows.
- To the right of the % and its instruction, provide the objects that are inserted into the format string on the left.
Here's an example:
'That is a %d one %s cat! % (1, 'fluffy')
which will print
That is 1 fluffy cat!
Here %d is an integer format in the style of C, and %s is a string format conversion code. Other common type codes
s String d Integer "double" i Integer x Hex (also X for capital letters) e Exponent (also E) f Floating point
The general structure with formatting commands is
%[(name)][flags][width].[precision]typecode
For example, in interactive Python try
>> x=1.2345678901234567890
If you ask for "x"
>> x
Python will respond with
1.2345678901234567
to the precision of its floating point storage. You can format this by
>> '%6.2f'%x (or with spaces for clarity, %6.2f' % x but no spaces after the first %
to which Python will respond
' 1.23'
You see that it left 6 places for the text, used a precision of 2 decimal places, and right-adjusted the text to the field. If you ask for more precision than you've allowed, Python will expand the field as needed. To have the data left-adjusted, put a minus sign in the formatting like this
>> '%-6.2f'%x '1.23 '
In a program, rather than interactively, statements like these work in a print command
>> print '%6.5e'%x 1.2346e+00
The alternative is a new "format method" scheme that is being developed for Python 3. In this the method acts on string object to create a new string. Here's very simple example of what it looks like to format the division of 25 by 7
>> '{0:.4f}'.format(25. / 7.) 3.5714
The first "0" is a position, and often there will be many similar {} to hold the data in the following format. You see the familiar "f" character to tell Python how to treat the data.
>> '%.4f'% (25. / 7.)
would have the same effect.
Finally, there is yet another way to use a format method that is perhaps clearer
>> format(3.5714, '.2f') 3.57
which is neat if there's only one variable.
Generally the most commonly used is the % expression which is embodied in Python 2.7 and in Python 3.
Input from a file and writing to files
It's more likely you will want to input data to a program from a file on your computer. Opening and reading a file in Python is very easy --
mydata = open('datafile.dat', 'r')
opens the file named datafile.dat for read-only, and assigns it to the object "mydata". You can read the data as text
mytext = mydata.read()
and the entire file is now contained in mytext. If you do this on the Python command line, and then enter "mytext", you'll see the context of your file (with end of line characters like \n too).
As with any text, we can split it into parts with
mytext.split()
which generates a list of space-delimited data from the file, ignoring the end of line's.
When you are finished reading the file, you close it with
mydata.close()
Similarly, to write a file you would open it for writing
mynewdata = open('newdata.dat', 'w')
write text to it
mynewdata.write('This is a line of text.\n')
and continue with other lines
mynewdata.write('1 2 3\n')
until you are finished
mynewdata.close()
The close() is essential because without the computer's buffers may not flush the contents of the file to the disk.
Whenever you are writing data to a file, it may be formatted with the same techniques used in formatting displayed data.
Numbers, text, and data types
Lists, tuples, dictionaries, and statements
Mathematics
Examples
For examples of Python illustrating input, output, data types, lists, and dictionaries, see the examples section.
Assignments
For the assigned homework to use these ideas, see the assignments section.