[Tutor] Some Basic Questions

Jeff Shannon jeff@ccvcorp.com
Wed Mar 26 20:15:02 2003


Brian Christopher Robinson wrote:

> I just began writing Python today and have a few basic questions:
>
> How can I read a single character from the standard input without 
> consuming
> any more input?
>
> How can I print to standard out without printing a newline?  It seems 
> that
> the print statement always adds a newline and I haven't found any other
> outputting functions yet. 


In the sys module there's two special objects, sys.stdin and sys.stdout. 
 They give you file-like access to standard input and output.  So you 
could, perhaps, use sys.stdin.read(1) to read a single character from 
standard input -- but note that your program will block here if there 
isn't a character waiting to be read, and I don't know how effective 
this would be for keyboard input.  Good access to the keyboard is 
platform-specific, and you can look in the curses module (unix) or the 
msvcrt module (Windows) for help there.

Similarly, you can use sys.stdout.write() to send text to standard 
output without any of the "helpful" formatting that print does.  The 
print statement is designed to be a convenience, rather than to be 
precise, so if you want byte-by-byte control of your output then 
sys.stdout.write() is the way to go.

> Is there any way to write a boolean function?  I wanted to write a simple
> is_alpha function (which may be in the standard library but I'm 
> leanring),
> and this is what I came up with:
>
> def is_alpha(c) :
>     letters = re.compile("[a-zA-Z]")
>     return letters.match(c)
>
> Instead of checking for true or false I check to see if None is returned,
> but that seems a little kludgy. 


Note that, in a boolean context, None evaluates to false so this would 
work with your function as-is:

if is_alpha(mystring):
    # do something

However, the typical method of indicating true or false is to return 1 
or 0, though pretty much anything that can conceptually be seen as 
"empty" will evaluate as false and everything else will evaluate as 
true.  Thus, an empty string '' is false, while a nonempty string 'abc' 
is true.  Recent versions of Python include a boolean type and built-in 
constants True and False...  but those constants are really just 
prettified 1s and 0s, and True + True = 2.

Also, isalpha() *does* already exist as a method of strings, along with 
isdigit(), isalnum(), and a few others.

 >>> x = 'abc'
 >>> x.isalpha()
1
 >>> x.isdigit()
0
 >>> x.isalnum()
1
 >>>

Even if I were to write my own is_alpha() function, I'd probably avoid 
using regular expressions -- I generally avoid them anyhow, actually. 
 Regexes are a lot more heavyweight than is necessary for many problems, 
and a lot more confusing too.  You'd be better off using constants from 
the string module:

 >>> string.ascii_letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
 >>>
 >>> def is_alpha(x):
...     from string import ascii_letters
...     for char in x:
...         if char not in ascii_letters:
...             return 0
...     return 1
...
 >>> is_alpha('spam')
1
 >>> is_alpha('123')
0
 >>> is_alpha('spam and eggs!')
0
 >>>

Hope this helps.  It's really useful to have some idea of what's in the 
standard library -- I know it's tedious, but I strongly recommend 
skimming through the Library Reference docs.  Don't worry about 
understanding everything, just a quick glance to see what kind of stuff 
is there.

Jeff Shannon
Technician/Programmer
Credit International