[Tutor] Questions about stdin, stdout, lists and tuples

D-Man dsh8290@rit.edu
Mon, 8 Jan 2001 11:29:25 -0500

On Mon, Jan 08, 2001 at 01:04:14PM -0800, Sheila King wrote:
| Python newbie, here. I come from a C++/Pascal/VB type of background.
| My first goal is to write some mail filtering scripts in Python to manage my
| e-mail spam. My e-mail scripts will be fed the incoming e-mail on stdin and I
| have to write my e-mails to stdout if I want them to be delivered through my
| MTA.

It would probably be easier to use procmail for this.  (Although the
syntax for the "recipes" is confusing -- I only use it to put list
mail in the right folder)  You can probably find some non-warranted
recipes on the web somewhere.

| I have a couple of questions I want to ask, just to make sure I'm
| understanding things clearly:
| Here is a short practice script I just wrote:
| -----------------------------------------
| import sys
| line = sys.stdin.readline()
| print line
| -----------------------------------------
| It reads a single line from stdin and prints it to stdout.
| My understanding, is that this is equivalent to the C++ program:
| ----------------------------------------
| #include <iostream.h>
| #include <string.h>
| int main()
| {
|    string line;
|    getline(cin,line);
|    cout << line;
|    return 0;
| }
| ----------------------------------------

I'm not familiar with the getline() fuction in C++ (I don't recall
seeing it in my C++ book, but it would have been useful many times).
If I can assume that it will read all characters on cin until it finds
a newline and inserts it into the string 'line', then that is the

To duplicate print, you need to add " << endl " to your 'cout' line.

| If anyone would like to confirm/deny/comment on my conclusions about
| stdin/stdout, I would be interested. Am I overlooking any Python peculiarities
| (such as the difference between "input" and "raw_input" or something along
| those lines.)?

Input is (nearly?) equivalent to eval( raw_input() ).  It will
evaluate the input and return the appropriate object (ie, int, list,
string, etc).

Raw_input doesn't evaluate the input, but returns the raw string for
you to deal with.

| Also,
| lists are like arrays, because I can reassign their values (they are not
| immutable), whereas tuples are like const arrays, their values cannot be
| altered?
| Have I got that right?

Lists are like arrays, but much better.  They can grow, be sorted,
reversed, and you can't overrun your memory bounds!

As far as semantics (except for index bounds errors) and syntax they
are basically the same.