[Tutor] Just wondering

Remco Gerlich scarblac@pino.selwerd.nl
Thu, 19 Oct 2000 12:26:11 +0200


On Thu, Oct 19, 2000 at 06:16:20AM -0400, Pollock974@aol.com wrote:
> Hello,

Hi!

>     I am a complete newbie at Python and i have a couple of questions. First 
> of all i  bought a book called "Teach Yourself Python in 24 Hours" by Ivan 
> Van Laningham. I was just wondering if anybody's read it and if its worth 
> reading. Second, this is a program for printing the ascii table.
> 
> 1.    #!/usr/local/bin/python
> 2.
> 3.    i = 0
> 4.    while i < 256:
> 5.          print chr(i),
> 6.          if i != 0 and i % 8 == 0:
> 7.              print
> 8.          i = i +1
> 
>     Now i have 2 questions. First of all i may be wrong but isn't the 1st 
> line a location on a Unix platform? Im running Windoze 98 and was wondering 
> if i needed that line of code.

No, it's not used on Windows. That said, keeping the line makes it more
portable. You might want to give your code to someone else, who might run
a Unix. Some editors can also recognize it, and go into "Python-mode"
automatically.

> Second, when i look at the code it seems that 
> the 6th and 7th line aren't needed, but when i run it without it, it prints 1 
> character on each line. I was wondering how that tells python to print more 
> than 1 character on a line. Now this isnt a life or death situation, it's 
> just been on my mind. I looked in the book but i couldnt find anything. Any 
> help would be greatly appreciated.

The "print chr(i)," ends with a comma. Usually print goes to the next line
after the print, but the comma puts a space at the end and then stays
at the same line. Lines 6 and 7 say "if i isn't 0 and can be divided
by 8, print nothing and go to the next line". The effect is that you get
8 characters per line, with spaces in between.

Remco Gerlich