Elements of Python programming: Difference between revisions
No edit summary |
|||
Line 13: | Line 13: | ||
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. | |||
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() | |||
For example, if 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. | |||
== Numbers, text, and data types == | == Numbers, text, and data types == |
Revision as of 04:30, 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.
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()
For example, if 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.
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.