Re: Nested for loops and print statements

2016-09-28 Thread Larry Hudson via Python-list

On 09/27/2016 09:20 PM, Steven D'Aprano wrote:

On Wednesday 28 September 2016 12:48, Larry Hudson wrote:


As they came through in the newsgroup, BOTH run correctly, because both
versions had leading spaces only.
(I did a careful copy/paste to check this.)


Copying and pasting from the news client may not be sufficient to show what
whitespace is actually used...

Exactly.  That's why I pointed out the sometime (mis)handling of tabs in newsreaders, and said 
these examples came through in MY reader (Thunderbird) as spaces only, so both examples ran 
correctly.


--
 -=- Larry -=-
--
https://mail.python.org/mailman/listinfo/python-list


Re: Nested for loops and print statements

2016-09-27 Thread Steven D'Aprano
On Wednesday 28 September 2016 12:48, Larry Hudson wrote:

> As they came through in the newsgroup, BOTH run correctly, because both
> versions had leading spaces only.
> (I did a careful copy/paste to check this.)

Copying and pasting from the news client may not be sufficient to show what 
whitespace is actually used. For example, I use KNode, the KDE news client on 
Linux, and for me, KNode converts tabs to spaces.

(If this is configurable, I don't know how.)

BUT if I choose the following menu command:


View > View Source

I see the raw, unmodified message as posted by the OP, complete with tabs and 
spaces.



> READ the error messages, they are important!

Indeed.

And *believe* the error messages.


-- 
Steven
git gets easier once you get the basic idea that branches are homeomorphic 
endofunctors mapping submanifolds of a Hilbert space.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Nested for loops and print statements

2016-09-27 Thread Larry Hudson via Python-list

On 09/26/2016 01:57 PM, Cai Gengyang wrote:

Ok it works now:


for row in range(10):

  for column in range(10):
   print("*",end="")




but how is it different from ---


for row in range(10):

   for column in range(10):
  print("*",end="")

SyntaxError: inconsistent use of tabs and spaces in indentation

Why does the example on top work and the example below doesn't work ? The only difference 
is that the "print" statement is one space different from each other. Forgive 
me if i can't explain things clearly over the forum



As they came through in the newsgroup, BOTH run correctly, because both versions had leading 
spaces only.  (I did a careful copy/paste to check this.)  Tabs are sometimes handled 
incorrectly/inconsistently in newsgroup postings.  But if you read the Traceback error message, 
it is telling you that you have a mix of tabs and spaces _in your original_.  READ the error 
messages, they are important!


--
 -=- Larry -=-
--
https://mail.python.org/mailman/listinfo/python-list


Re: Nested for loops and print statements

2016-09-27 Thread Gregory Ewing

Cai Gengyang wrote:
How are you running the interactive interpreter? Are you 
using IDLE, or are you running Python in a command window? --- IDLE


I don't normally use IDLE on MacOSX, so I had to try it to
find out. I think I know what your problem is now.

When you type a line into IDLE ending with a colon and press
enter, it automatically indents the next line, and it does
it using *tab* characters. You can see this by selecting the
indentation with the mouse. You'll notice that it highlights
in big chunks -- those are tab characters.

This means that any extra indentation you add yourself also
needs to be done by pressing the tab key, not the space bar,
otherwise you risk mixing up tabs and spaces in ways that
Python doesn't like.

--
Greg
--
https://mail.python.org/mailman/listinfo/python-list


Re: Nested for loops and print statements

2016-09-27 Thread Cai Gengyang
On Tuesday, September 27, 2016 at 2:14:05 PM UTC+8, Cai Gengyang wrote:
> We're trying to help, but we need to know more about the 
> environment you're using to enter your code. 
> 
> What operating system are you using? --- OSX Yosemite Version 10.10.2
> 
> How are you running the interactive interpreter? Are you 
> using IDLE, or are you running Python in a command window? --- IDLE
> 
> Are you typing code directly into the interactive interpreter,  Typing it 
> directly into IDLE
> or are you typing it into a text editor and then copying 
> and pasting it into the interpreter? 
> 
> If you're using a text editor, which one are you using? --- Not using a text 
> editor
> 
> What *exactly* are you typing on the keyboard to produce 
> your indentation? Are you pressing the space bar, the 
> tab key, or some combination of them?  Just the space bar, not the tab 
> key because it gives jumps in spaces that are too large for my liking.
> 
> 
> 
> 
> 
> 
> 
> 
> On Tuesday, September 27, 2016 at 1:20:17 PM UTC+8, Gregory Ewing wrote:
> > Cai Gengyang wrote:
> > > I'll still be asking for help here. Please help out a newbie.
> > 
> > We're trying to help, but we need to know more about the
> > environment you're using to enter your code.
> > 
> > What operating system are you using?
> > 
> > How are you running the interactive interpreter? Are you
> > using IDLE, or are you running Python in a command window?
> > 
> > Are you typing code directly into the interactive interpreter,
> > or are you typing it into a text editor and then copying
> > and pasting it into the interpreter?
> > 
> > If you're using a text editor, which one are you using?
> > 
> > What *exactly* are you typing on the keyboard to produce
> > your indentation? Are you pressing the space bar, the
> > tab key, or some combination of them?
> > 
> > -- 
> > Greg

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Nested for loops and print statements

2016-09-27 Thread Cai Gengyang
We're trying to help, but we need to know more about the 
environment you're using to enter your code. 

What operating system are you using? --- OSX Yosemite Version 10.10.2

How are you running the interactive interpreter? Are you 
using IDLE, or are you running Python in a command window? --- IDLE

Are you typing code directly into the interactive interpreter,  Typing it 
directly into IDLE
or are you typing it into a text editor and then copying 
and pasting it into the interpreter? 

If you're using a text editor, which one are you using? --- Not using a text 
editor

What *exactly* are you typing on the keyboard to produce 
your indentation? Are you pressing the space bar, the 
tab key, or some combination of them?  Just the space bar, not the tab key 
gives too jumps in spaces that are too large for my liking.








On Tuesday, September 27, 2016 at 1:20:17 PM UTC+8, Gregory Ewing wrote:
> Cai Gengyang wrote:
> > I'll still be asking for help here. Please help out a newbie.
> 
> We're trying to help, but we need to know more about the
> environment you're using to enter your code.
> 
> What operating system are you using?
> 
> How are you running the interactive interpreter? Are you
> using IDLE, or are you running Python in a command window?
> 
> Are you typing code directly into the interactive interpreter,
> or are you typing it into a text editor and then copying
> and pasting it into the interpreter?
> 
> If you're using a text editor, which one are you using?
> 
> What *exactly* are you typing on the keyboard to produce
> your indentation? Are you pressing the space bar, the
> tab key, or some combination of them?
> 
> -- 
> Greg
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Nested for loops and print statements

2016-09-26 Thread Gregory Ewing

Cai Gengyang wrote:

I'll still be asking for help here. Please help out a newbie.


We're trying to help, but we need to know more about the
environment you're using to enter your code.

What operating system are you using?

How are you running the interactive interpreter? Are you
using IDLE, or are you running Python in a command window?

Are you typing code directly into the interactive interpreter,
or are you typing it into a text editor and then copying
and pasting it into the interpreter?

If you're using a text editor, which one are you using?

What *exactly* are you typing on the keyboard to produce
your indentation? Are you pressing the space bar, the
tab key, or some combination of them?

--
Greg
--
https://mail.python.org/mailman/listinfo/python-list


Re: Nested for loops and print statements

2016-09-26 Thread breamoreboy
On Monday, September 26, 2016 at 9:57:52 PM UTC+1, Cai Gengyang wrote:
> Ok it works now:
> 
> >>>for row in range(10):
>   for column in range(10):
>print("*",end="")
> 
>  
> 
> 
> but how is it different from ---
> 
> >>> for row in range(10): 
>for column in range(10): 
>   print("*",end="") 
>
> SyntaxError: inconsistent use of tabs and spaces in indentation 
> 
> Why does the example on top work and the example below doesn't work ? The 
> only difference is that the "print" statement is one space different from 
> each other. Forgive me if i can't explain things clearly over the forum 
> 

Perhaps you'd be more comfortable on the tutor mailing list 
https://mail.python.org/mailman/listinfo/tutor

Kindest regards.

Mark Lawrence.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Nested for loops and print statements

2016-09-26 Thread Cai Gengyang
Sure, I just sent in a subscription request to it ... but I'll still be asking 
for help here. Please help out a newbie. When I master this language I can help 
other new users too (This is good for the world and for everyone involved). 
Ideally, Information and education should be free and not locked up by a few 
academics and institutions. 

On Tuesday, September 27, 2016 at 6:00:40 AM UTC+8, bream...@gmail.com wrote:
> On Monday, September 26, 2016 at 9:57:52 PM UTC+1, Cai Gengyang wrote:
> > Ok it works now:
> > 
> > >>>for row in range(10):
> >   for column in range(10):
> >print("*",end="")
> > 
> >  
> > 
> > 
> > but how is it different from ---
> > 
> > >>> for row in range(10): 
> >for column in range(10): 
> >   print("*",end="") 
> >
> > SyntaxError: inconsistent use of tabs and spaces in indentation 
> > 
> > Why does the example on top work and the example below doesn't work ? The 
> > only difference is that the "print" statement is one space different from 
> > each other. Forgive me if i can't explain things clearly over the forum 
> > 
> 
> Perhaps you'd be more comfortable on the tutor mailing list 
> https://mail.python.org/mailman/listinfo/tutor
> 
> Kindest regards.
> 
> Mark Lawrence.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Nested for loops and print statements

2016-09-26 Thread Marko Rauhamaa
Cai Gengyang :

> What is a tab and what is a space in python and what's the difference
> ?
>
> Which piece of code is indented with tabs and which one is indented
> with spaces ?

Key questions that Python gurus are having a hard time answering!
Equally confusing, you might run into this phantom phenomenon:

   >>> if True:
   ...    a = 3
 File "", line 2
     a = 3
   ^
   SyntaxError: invalid character in identifier


It might be useful to have a mode that echoes each input line as seen by
Python (repr).


Marko
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Nested for loops and print statements

2016-09-26 Thread Cai Gengyang
Ok it works now:

>>>for row in range(10):
  for column in range(10):
   print("*",end="")

 


but how is it different from ---

>>> for row in range(10): 
   for column in range(10): 
  print("*",end="") 
   
SyntaxError: inconsistent use of tabs and spaces in indentation 

Why does the example on top work and the example below doesn't work ? The only 
difference is that the "print" statement is one space different from each 
other. Forgive me if i can't explain things clearly over the forum 



On Tuesday, September 27, 2016 at 3:57:18 AM UTC+8, Larry Hudson wrote:
> On 09/26/2016 08:25 AM, Cai Gengyang wrote:
> > I just wanted to note that sometimes the code works, sometimes it doesn't. 
> > (even though both are exactly the same code) ... Weird , dum dum dum
> >
> 
> It is NOT weird.  Python is being consistent, YOU are not.
> 
> These examples are NOT "exactly the same code"!  The indenting is different.  
> Python (correctly) 
> treats them as being different.
> 
> YOU MUST USE CONSISTENT INDENTING.  You MUST always use spaces (the 
> recommended) or always use 
> tabs.  Never, ever, not at any time, can you mix them.
> 
> (Now, does that emphasize the point enough?)  Go back and REWRITE your code 
> with CONSISTENT 
> indenting.
> 
> -- 
>   -=- Larry -=-
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Nested for loops and print statements

2016-09-26 Thread Terry Reedy

On 9/26/2016 12:54 PM, Cai Gengyang wrote:


Which piece of code is indented with tabs and which one is indented with spaces 
?


I told you in my initial answer, where I said, referring to the two 
indented lines in one 'piece of code', "These indents are 4 spaces and 1 
tabs."  It is the mixture in one piece of code that is the problem.  The 
tab in your original post has since be converted to (4) spaces.  Tabs do 
not survive in email, sometimes just being deleted.



--
Terry Jan Reedy

--
https://mail.python.org/mailman/listinfo/python-list


Re: Nested for loops and print statements

2016-09-26 Thread Larry Hudson via Python-list

On 09/26/2016 08:25 AM, Cai Gengyang wrote:

I just wanted to note that sometimes the code works, sometimes it doesn't. 
(even though both are exactly the same code) ... Weird , dum dum dum



It is NOT weird.  Python is being consistent, YOU are not.

These examples are NOT "exactly the same code"!  The indenting is different.  Python (correctly) 
treats them as being different.


YOU MUST USE CONSISTENT INDENTING.  You MUST always use spaces (the recommended) or always use 
tabs.  Never, ever, not at any time, can you mix them.


(Now, does that emphasize the point enough?)  Go back and REWRITE your code with CONSISTENT 
indenting.


--
 -=- Larry -=-
--
https://mail.python.org/mailman/listinfo/python-list


Re: Nested for loops and print statements

2016-09-26 Thread Jussi Piitulainen
Cai Gengyang writes:

> What is a tab and what is a space in python and what's the difference
> ?

Try print('x\tx') in Python to see a tab character between the two x's.
For me it looks the same as seven spaces, for you it will also look like
some amount of whitespace but it might be a different amount.

The two-character sequence \t inside a Python string literal means tab,
same as \x09, same as \u0009, same as \N{TAB}, same as \N{HT}. (These
sequences need to be inside quotes.)

With print('xx\tx'), the tab looks, for me, the same as six spaces. It's
funny that way: it's a single character, but it's shown differently
depending on it's position on the line.

You may get a tab character by pressing the tab key, but it's usual for
that key to be smart in some way or used for some other purpose.

> Which piece of code is indented with tabs and which one is indented
> with spaces ?

I've replaced the tabs with # below, and the greater-than signs with @.
This may not have been exactly your code any more, but try to imagine a
tab in place of each # anyway.

  for row in range(10):
 @for column in range(10):
 @ #  print("*",end="")
 @ #
 @ SyntaxError: inconsistent use of tabs and spaces in indentation
  for row in range(10):
 @ #for column in range(10):
 @ #   print("*",end="")
 @

 It would be better for you to get a better editor that either prevented
you from getting into this mess in the first place, or at least showed
where the offending characters are.

It may be possible to configure your editor to be better. The details
depend on what exactly you are using. What are you using?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Nested for loops and print statements

2016-09-26 Thread jmp

On 09/26/2016 06:54 PM, Cai Gengyang wrote:

What is a tab and what is a space in python and what's the difference ?

Which piece of code is indented with tabs and which one is indented with spaces 
?


Please do not top-post in this list. Put your text after the message you 
quote.



Tabs and spaces are 2 different characters, tab ASCII code is 9, space 
ASCII code is 32.


You *need* to understand that they are 2 differents characters.

The problem come with their representation.

1/ a space is represented by 1 whitespace
2/ a tab is represented by x whitespaces. The number x depends on your 
text editor.


When you want to indent a python block, you put whitespaces on its left 
to indent it. You can indent using either tabs or spaces.


Python does not tell you which one to use, but it tells you to choose 
*one* method, and stick with it.


jm


--
https://mail.python.org/mailman/listinfo/python-list


Re: Nested for loops and print statements

2016-09-26 Thread Steve D'Aprano
On Tue, 27 Sep 2016 01:25 am, Cai Gengyang wrote:

> I just wanted to note that sometimes the code works, sometimes it doesn't.
> (even though both are exactly the same code) ... Weird , dum dum dum


They are not the same code. One of them mixes tabs and spaces for the same
indent level, the other does not.

Here is a hint how you can tell the difference between lines with tabs and
lines with spaces in the Python interactive interpreter:


- if you pressed the spacebar repeatedly to get the indent, like 
  SPACEBAR SPACEBAR SPACEBAR SPACEBAR, then the line will be indented
  with spaces;

- if you pressed the tab key to get the indent, like TAB TAB, then the
  line will be indented with tabs;

- if you pressed the tab key first, then spaces, like TAB SPACEBAR, 
  then the line will be indented with a mixture of tabs and spaces.


If you are copying code from a text editor, check your editor's settings.
Some editors will be configured to insert spaces when you press the tab
key. 


Your first example has:

1st line: no indent
2nd line: seven spaces
3rd line: tab + two spaces

Notice that the 2nd and 3rd line start with inconsistent indentation: one
uses spaces, the other uses tab. This is bad.

Your second example has:

1st line: no indent
2nd line: tab
3rd line: tab + three spaces


Notice that the 2nd and 3rd line start with the same indentation: both start
with a tab. This is better, it is enough to satisfy the interpreter, but it
would be better if you picked one (spaces) or the other (tabs) and ONLY
used that. Other wise you will just confuse yourself.

But you won't confuse the interpreter.




-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Nested for loops and print statements

2016-09-26 Thread Cai Gengyang
What is a tab and what is a space in python and what's the difference ?

Which piece of code is indented with tabs and which one is indented with spaces 
?




On Tuesday, September 27, 2016 at 12:40:16 AM UTC+8, MRAB wrote:
> On 2016-09-26 16:25, Cai Gengyang wrote:
> > I just wanted to note that sometimes the code works, sometimes it doesn't. 
> > (even though both are exactly the same code) ... Weird , dum dum dum
> >
>  for row in range(10):
> >for column in range(10):
> >   print("*",end="")
> > 
> > SyntaxError: inconsistent use of tabs and spaces in indentation
>  for row in range(10):
> > for column in range(10):
> >print("*",end="")
> >
> [snip]
> 
> They are not exactly the same code. They are indented differently (tabs 
> vs spaces), and that matters in Python.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Nested for loops and print statements

2016-09-26 Thread MRAB

On 2016-09-26 16:25, Cai Gengyang wrote:

I just wanted to note that sometimes the code works, sometimes it doesn't. 
(even though both are exactly the same code) ... Weird , dum dum dum


for row in range(10):

   for column in range(10):
  print("*",end="")

SyntaxError: inconsistent use of tabs and spaces in indentation

for row in range(10):

for column in range(10):
   print("*",end="")


[snip]

They are not exactly the same code. They are indented differently (tabs 
vs spaces), and that matters in Python.


--
https://mail.python.org/mailman/listinfo/python-list


Re: Nested for loops and print statements

2016-09-26 Thread jmp

On 09/26/2016 05:25 PM, Cai Gengyang wrote:

I just wanted to note that sometimes the code works, sometimes it doesn't. 
(even though both are exactly the same code) ... Weird , dum dum dum


for row in range(10):

for column in range(10):
  print("*",end="")

SyntaxError: inconsistent use of tabs and spaces in indentation

for row in range(10):

for column in range(10):
   print("*",end="")


Here,

from itertools import product
for row,column in product(range(10), range(10)): print("*", end="")


Problem solved :)

On a more serious note, they're not the same code, in the same way than

print("foo")
print("bar")

is not the same code than

print("foo")print("bar")

You're fooled by your editor which displays 2 different characters the 
same way. Tab or space does matter in python, get over it and move on 
with your python life. Actually python is a great debugger for 
misconfigured text editors.


jm

--
https://mail.python.org/mailman/listinfo/python-list


Re: Nested for loops and print statements

2016-09-26 Thread Cai Gengyang
I just wanted to note that sometimes the code works, sometimes it doesn't. 
(even though both are exactly the same code) ... Weird , dum dum dum

>>> for row in range(10):
   for column in range(10):
  print("*",end="")
  
SyntaxError: inconsistent use of tabs and spaces in indentation
>>> for row in range(10):
for column in range(10):
   print("*",end="")

   






On Monday, September 26, 2016 at 5:36:26 PM UTC+8, Steven D'Aprano wrote:
> On Monday 26 September 2016 18:32, Cai Gengyang wrote:
> 
> > These are my attempts ---
> 
> That's nice. Do you have a question?
> 
> 
> > SyntaxError: inconsistent use of tabs and spaces in indentation
> 
> When you indent, press TAB or SPACE but not both.
> 
> This error can only happen if you are use spaces for some lines and tabs for 
> other lines. Do not do that. Use spaces only or tabs only, never both.
> 
> 
> > SyntaxError: expected an indented block
> 
> You forgot to indent at all. You must indent with at least one space or one 
> tab.
> 
> > SyntaxError: inconsistent use of tabs and spaces in indentation
> 
> Use only spaces, or only tabs, never both. If you use spaces for one line, 
> then 
> always use spaces. If you use tabs for one line, then always use tabs.
> 
> > SyntaxError: inconsistent use of tabs and spaces in indentation
> 
> Same as above.
> 
> 
> 
> 
> 
> -- 
> Steven
> git gets easier once you get the basic idea that branches are homeomorphic 
> endofunctors mapping submanifolds of a Hilbert space.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Nested for loops and print statements

2016-09-26 Thread Steven D'Aprano
On Monday 26 September 2016 18:32, Cai Gengyang wrote:

> These are my attempts ---

That's nice. Do you have a question?


> SyntaxError: inconsistent use of tabs and spaces in indentation

When you indent, press TAB or SPACE but not both.

This error can only happen if you are use spaces for some lines and tabs for 
other lines. Do not do that. Use spaces only or tabs only, never both.


> SyntaxError: expected an indented block

You forgot to indent at all. You must indent with at least one space or one 
tab.

> SyntaxError: inconsistent use of tabs and spaces in indentation

Use only spaces, or only tabs, never both. If you use spaces for one line, then 
always use spaces. If you use tabs for one line, then always use tabs.

> SyntaxError: inconsistent use of tabs and spaces in indentation

Same as above.





-- 
Steven
git gets easier once you get the basic idea that branches are homeomorphic 
endofunctors mapping submanifolds of a Hilbert space.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Nested for loops and print statements

2016-09-26 Thread Jussi Piitulainen
Cai Gengyang writes:

> These are my attempts ---
>
 for row in range(10):
> for column in range(10):
>   print("*",end=" ")
>   
> SyntaxError: inconsistent use of tabs and spaces in indentation

What do you type yourself? Could it be that your software starts the
second line with a tab automatically and then you type spaces yourself?

When I type commands to the Python interpreter, it looks like this:

>>> if 1:
...print(1)
... 
1

The interpreter inserts the dots below the prompt, and a space, and here
I have inserted three more spaces for indentation. The interpreter then
ignores those dots and space, and it doesn't seem to let me use any tabs
anyway.

You are running the interpreter in a different way. That's fine. But
what exactly are you doing?

(If you are using the spacebar, try the tabulation key instead. If you
are using the tabulation key, try the spacebar instead.)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Nested for loops and print statements

2016-09-26 Thread Jussi Piitulainen
Steven D'Aprano writes:

> P.S. Hey Jussi, is the backspace key on your keyboard broken? Every
> time somebody bottom-posts without trimming, a pixie dies...

I was annoyed by the top-posted one-liner in response to the last line
of Terry's response. I responded in kind and then it was too late. I'm
sorry. It was a childish thing to do.

Backspace key is fine. I tend to use C-w and C-k for trimming, and they
are fine, too. I was the broken part.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Nested for loops and print statements

2016-09-26 Thread Cai Gengyang
These are my attempts ---

>>> for row in range(10):
for column in range(10):
print("*",end=" ")

SyntaxError: inconsistent use of tabs and spaces in indentation
>>> for row in range(10):
for column in range(10):
print("*",end=" ")

SyntaxError: expected an indented block
>>> for row in range(10):
for column in range(10):
 print("*",end=" ")
 
SyntaxError: inconsistent use of tabs and spaces in indentation
>>> for row in range(10):
for column in range(10):
print("*",end=" ")

SyntaxError: inconsistent use of tabs and spaces in indentation
>>> for row in range(10):
for column in range(10):
print("*",end=" ")


* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * * * * * * 


On Monday, September 26, 2016 at 4:16:37 PM UTC+8, Steven D'Aprano wrote:
> On Monday 26 September 2016 17:21, Jussi Piitulainen wrote:
> 
> > Cai Gengyang  writes:
> [snip 80 or so lines]
> > Reindent your lines.
> 
> In case Cai doesn't know what "reindent" means:
> 
> 
> It depends on your text editor. At worst, you have to delete all the indents, 
> and re-enter them, using ONLY spaces, or ONLY tabs, but never mixing them.
> 
> Some text editors may have a command to reindent, or clean indentation, or 
> fix 
> indentation, or something similar.
> 
> Or you can use the tabnanny.py program:
> 
> 
> python -m tabnanny path/to/file.py
> 
> 
> 
> P.S. Hey Jussi, is the backspace key on your keyboard broken? Every time 
> somebody bottom-posts without trimming, a pixie dies...
> 
> 
> 
> -- 
> Steven
> git gets easier once you get the basic idea that branches are homeomorphic 
> endofunctors mapping submanifolds of a Hilbert space.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Nested for loops and print statements

2016-09-26 Thread Steven D'Aprano
On Monday 26 September 2016 17:21, Jussi Piitulainen wrote:

> Cai Gengyang  writes:
[snip 80 or so lines]
> Reindent your lines.

In case Cai doesn't know what "reindent" means:


It depends on your text editor. At worst, you have to delete all the indents, 
and re-enter them, using ONLY spaces, or ONLY tabs, but never mixing them.

Some text editors may have a command to reindent, or clean indentation, or fix 
indentation, or something similar.

Or you can use the tabnanny.py program:


python -m tabnanny path/to/file.py



P.S. Hey Jussi, is the backspace key on your keyboard broken? Every time 
somebody bottom-posts without trimming, a pixie dies...



-- 
Steven
git gets easier once you get the basic idea that branches are homeomorphic 
endofunctors mapping submanifolds of a Hilbert space.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Nested for loops and print statements

2016-09-26 Thread Jussi Piitulainen
Cai Gengyang  writes:

> So what do i need to do to correct the error ?
>
> Regards
>
>
> On Monday, September 26, 2016 at 2:48:16 PM UTC+8, Terry Reedy wrote:
>> On 9/26/2016 1:59 AM, Cai Gengyang wrote:
>> > Why is it that you need a print() at the end to create the table for 
>> > example 1:
>> >
>> > Example 1 ---
>> >
>>  for row in range(10):
>> > for column in range(10):
>> > print("*",end=" ")
>> > # Print a blank line for next row
>> > print()
>> 
>> These indents are either 4 or 8 spaces.
>> 
>> The print provides a carriage return.
>> Each line ends with a space.
>> 
>> > * * * * * * * * * *
>> > * * * * * * * * * *
>> > * * * * * * * * * *
>> > * * * * * * * * * *
>> > * * * * * * * * * *
>> > * * * * * * * * * *
>> > * * * * * * * * * *
>> > * * * * * * * * * *
>> > * * * * * * * * * *
>> > * * * * * * * * * *
>> 
>> One can avoid both extra print and spaces with
>> 
>> for row in range(10):
>>  for column in range(10):
>>  print("*", end=" " if column<9 else '\n')
>> 
>> # or
>> for row in range(10):
>>  print(' '.join(['*']*10))
>> # or
>> print((' '.join(['*']*10)+'\n')*10)
>> 
>> # or
>> for row in range(10):
>>  print('* '*9 + '*')
>> # or
>> print(('* '*9 + '*\n')*10)
>> 
>> 
>> > but not for Example 2 ---
>> >
>> > for row in range(10):
>> > print("*",end=" ")
>> >
>> > * * * * * * * * * *
>> >
>> > When I try to do example 1 without the print() statement at the end, I get 
>> > this error :
>> >
>> > for row in range(10):
>> > for column in range(10):
>> >print("*",end=" ")
>> 
>> These indents are 4 spaces and 1 tabs.
>> 
>> > SyntaxError: inconsistent use of tabs and spaces in indentation
>> 
>> Because you mixed tabs and spaces.  Has nothing to do with print statement.
>> 
>> 
>> -- 
>> Terry Jan Reedy

Reindent your lines.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Nested for loops and print statements

2016-09-26 Thread Cai Gengyang
So what do i need to do to correct the error ?

Regards


On Monday, September 26, 2016 at 2:48:16 PM UTC+8, Terry Reedy wrote:
> On 9/26/2016 1:59 AM, Cai Gengyang wrote:
> > Why is it that you need a print() at the end to create the table for 
> > example 1:
> >
> > Example 1 ---
> >
>  for row in range(10):
> > for column in range(10):
> > print("*",end=" ")
> > # Print a blank line for next row
> > print()
> 
> These indents are either 4 or 8 spaces.
> 
> The print provides a carriage return.
> Each line ends with a space.
> 
> > * * * * * * * * * *
> > * * * * * * * * * *
> > * * * * * * * * * *
> > * * * * * * * * * *
> > * * * * * * * * * *
> > * * * * * * * * * *
> > * * * * * * * * * *
> > * * * * * * * * * *
> > * * * * * * * * * *
> > * * * * * * * * * *
> 
> One can avoid both extra print and spaces with
> 
> for row in range(10):
>  for column in range(10):
>  print("*", end=" " if column<9 else '\n')
> 
> # or
> for row in range(10):
>  print(' '.join(['*']*10))
> # or
> print((' '.join(['*']*10)+'\n')*10)
> 
> # or
> for row in range(10):
>  print('* '*9 + '*')
> # or
> print(('* '*9 + '*\n')*10)
> 
> 
> > but not for Example 2 ---
> >
> > for row in range(10):
> > print("*",end=" ")
> >
> > * * * * * * * * * *
> >
> > When I try to do example 1 without the print() statement at the end, I get 
> > this error :
> >
> > for row in range(10):
> > for column in range(10):
> > print("*",end=" ")
> 
> These indents are 4 spaces and 1 tabs.
> 
> > SyntaxError: inconsistent use of tabs and spaces in indentation
> 
> Because you mixed tabs and spaces.  Has nothing to do with print statement.
> 
> 
> -- 
> Terry Jan Reedy

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Nested for loops and print statements

2016-09-26 Thread Terry Reedy

On 9/26/2016 1:59 AM, Cai Gengyang wrote:

Why is it that you need a print() at the end to create the table for example 1:

Example 1 ---


for row in range(10):

for column in range(10):
print("*",end=" ")
# Print a blank line for next row
print()


These indents are either 4 or 8 spaces.

The print provides a carriage return.
Each line ends with a space.


* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *


One can avoid both extra print and spaces with

for row in range(10):
for column in range(10):
print("*", end=" " if column<9 else '\n')

# or
for row in range(10):
print(' '.join(['*']*10))
# or
print((' '.join(['*']*10)+'\n')*10)

# or
for row in range(10):
print('* '*9 + '*')
# or
print(('* '*9 + '*\n')*10)



but not for Example 2 ---

for row in range(10):
print("*",end=" ")

* * * * * * * * * *

When I try to do example 1 without the print() statement at the end, I get this 
error :

for row in range(10):
for column in range(10):
print("*",end=" ")


These indents are 4 spaces and 1 tabs.


SyntaxError: inconsistent use of tabs and spaces in indentation


Because you mixed tabs and spaces.  Has nothing to do with print statement.


--
Terry Jan Reedy

--
https://mail.python.org/mailman/listinfo/python-list


Re: Nested for loops and print statements

2016-09-26 Thread Jussi Piitulainen
Cai Gengyang writes:

> Why is it that you need a print() at the end to create the table for
> example 1:
>
> Example 1 ---
>
>>>> for row in range(10):
> for column in range(10):
> print("*",end=" ")
>  
> # Print a blank line for next row
> print()

[- -]

> When I try to do example 1 without the print() statement at the end, I
> get this error :
>
> for row in range(10):
> for column in range(10):
>   print("*",end=" ")
>   
> SyntaxError: inconsistent use of tabs and spaces in indentation

That error has absolutely nothing to do with nested loops, and
absolutely nothing to do with print statements. Nested loops and print
statements are entirely coincidental to the error.

You have managed to use a mixture of two different whitespace characters
to indent the lines: horizontal tabs (character code 9, hex 9) and
spaces (character code 32, hex 20). Different environments may interpret
and display the tabs as different amounts of indentation, depending on
their position in the line. That affects the meaning of a Python
program.

When you get this error, re-indent your lines.
-- 
https://mail.python.org/mailman/listinfo/python-list


Nested for loops and print statements

2016-09-26 Thread Cai Gengyang
Why is it that you need a print() at the end to create the table for example 1:

Example 1 ---

>>> for row in range(10):
for column in range(10):
print("*",end=" ")
 
# Print a blank line for next row
print()


* * * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 

but not for Example 2 ---

for row in range(10):
print("*",end=" ")

* * * * * * * * * *

When I try to do example 1 without the print() statement at the end, I get this 
error :

for row in range(10):
for column in range(10):
print("*",end=" ")

SyntaxError: inconsistent use of tabs and spaces in indentation
-- 
https://mail.python.org/mailman/listinfo/python-list


[issue27514] SystemError when compiling deeply nested for loops

2016-07-14 Thread Roundup Robot

Roundup Robot added the comment:

New changeset e6e7c8368c70 by Benjamin Peterson in branch '3.5':
make too many nested blocks be a SyntaxError instead of a SystemError (closes 
#27514)
https://hg.python.org/cpython/rev/e6e7c8368c70

New changeset 345ec7455b75 by Benjamin Peterson in branch '2.7':
make too many nested blocks be a SyntaxError instead of a SystemError (closes 
#27514)
https://hg.python.org/cpython/rev/345ec7455b75

New changeset d1da87d8b29c by Benjamin Peterson in branch 'default':
merge 3.5 (#27514)
https://hg.python.org/cpython/rev/d1da87d8b29c

--
nosy: +python-dev
resolution:  -> fixed
stage: commit review -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue27514] SystemError when compiling deeply nested for loops

2016-07-14 Thread Serhiy Storchaka

Serhiy Storchaka added the comment:

LGTM.

--
stage: needs patch -> commit review

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue27514] SystemError when compiling deeply nested for loops

2016-07-14 Thread Ammar Askar

Ammar Askar added the comment:

Amended patch with the comment fixed to refer to just the issue numbers 
according to advice from bitdancer.

--
Added file: http://bugs.python.org/file43726/nested_blocks.diff2

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue27514] SystemError when compiling deeply nested for loops

2016-07-14 Thread Ammar Askar

Ammar Askar added the comment:

This patch changes it to a SyntaxError instead of a SystemError.

--
keywords: +patch
nosy: +ammar2
Added file: http://bugs.python.org/file43725/nested_blocks.diff

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue27514] SystemError when compiling deeply nested for loops

2016-07-14 Thread Nick Coghlan

Nick Coghlan added the comment:

+1 for Serhiy's recommendation. It's OK for the compiler to say "Don't do 
that", but the correct error is SyntaxError, even when it's a limitation later 
in the code generation pipeline (rather than failing to comply with the 
language grammar)

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue27514] SystemError when compiling deeply nested for loops

2016-07-14 Thread Serhiy Storchaka

Serhiy Storchaka added the comment:

There is a static limit for the number of statically nested blocks. Getting rid 
of this limit looks not easy.

But SystemError is not an exception that should be raised. SystemError is for 
errors that can't be occurred in normal case. It should only be caused by 
incorrect use of C API or hacking Python internals. I think SyntaxError is more 
appropriate in this case (as in case of passing more than 255 arguments to a 
function).

--
stage:  -> needs patch
versions: +Python 3.5, Python 3.6

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue27514] SystemError when compiling deeply nested for loops

2016-07-14 Thread ppperry

New submission from ppperry:

The following code:
for a in range(26):
  for b in range(26):
   for c in range(26):
for d in range(26):
 for e in range(26):
  for f in range(26):
   for g in range(26):
for h in range(26):
 for i in range(26):
  for j in range(26):
   for k in range(26):
for l in range(26):
 for m in range(26):
  for o in range(26):
   for p in range(26):
for q in range(26):
 for r in range(26):
  for s in range(26):
   for t in range(26):
for u in range(26):
 for v in range(26):
   for w in range(26):
 pass
fails to compile with `SystemError:too many statically nested blocks`, which 
was described as a serious bug in issue27081.

--
components: Interpreter Core
messages: 270405
nosy: benjamin.peterson, brett.cannon, georg.brandl, ncoghlan, ppperry, 
serhiy.storchaka, yselivanov
priority: normal
severity: normal
status: open
title: SystemError when compiling deeply nested for loops
type: behavior
versions: Python 2.7

___
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue27514>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



How to jump out of nested 'for'-loops?

2009-09-28 Thread Peng Yu
Hi,

I want some command to jump out of nested loop. I'm wondering what is
the most convenient way to do so in python.

for i in range(10):
  print i = , i
  for j in range(10):
if i*10 + j == 50:
  print i*10 + j
  break # I want to jump out of the loops.

Regards,
Peng
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to jump out of nested 'for'-loops?

2009-09-28 Thread Zero Piraeus
:

 I want some command to jump out of nested loop. I'm wondering what is
 the most convenient way to do so in python.

http://www.google.com/search?q=python+nested+break

 -[]z.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to jump out of nested 'for'-loops?

2009-09-28 Thread r
On Sep 28, 10:09 pm, Peng Yu pengyu...@gmail.com wrote:
 Hi,

 I want some command to jump out of nested loop. I'm wondering what is
 the most convenient way to do so in python.

 for i in range(10):
   print i = , i
   for j in range(10):
     if i*10 + j == 50:
       print i*10 + j
       break # I want to jump out of the loops.

 Regards,
 Peng

if your code is in a function/method just use return
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to jump out of nested 'for'-loops?

2009-09-28 Thread Sean DiZazzo
On Sep 28, 8:09 pm, Peng Yu pengyu...@gmail.com wrote:
 Hi,

 I want some command to jump out of nested loop. I'm wondering what is
 the most convenient way to do so in python.

 for i in range(10):
   print i = , i
   for j in range(10):
     if i*10 + j == 50:
       print i*10 + j
       break # I want to jump out of the loops.

 Regards,
 Peng

Can you please give the people who answered your question a simple
thank you??

~Sean
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to jump out of nested 'for'-loops?

2009-09-28 Thread D'Arcy J.M. Cain
On Mon, 28 Sep 2009 22:09:37 -0500
Peng Yu pengyu...@gmail.com wrote:
 I want some command to jump out of nested loop. I'm wondering what is
 the most convenient way to do so in python.

Forget about jumping out of a loop. Put your loop into a function and
simply return.  Much cleaner and clearer.

 for i in range(10):
   print i = , i
   for j in range(10):
 if i*10 + j == 50:
   print i*10 + j
   break # I want to jump out of the loops.

def myfunc(li, lim, mul):
  for i in li:
print i = , i
for j in range(10):
  if i*mul + j == lim:
return i*mul + j

print myfunc(range(10), 50, 10)

-- 
D'Arcy J.M. Cain da...@druid.net |  Democracy is three wolves
http://www.druid.net/darcy/|  and a sheep voting on
+1 416 425 1212 (DoD#0082)(eNTP)   |  what's for dinner.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to jump out of nested 'for'-loops?

2009-09-28 Thread alex23
Peng Yu pengyu...@gmail.com wrote:
 I want some command to jump out of nested loop. I'm wondering what is
 the most convenient way to do so in python.

I'm sure I'm wasting my breath by saying this, but would it hurt you
to actually check the list before posting? You're not the only person
to ever have used Python, a lot of your questions have been answered
repeatedly in the past.

As it stands, this very question was discussed here no more than 2-3
days ago:
http://groups.google.com/group/comp.lang.python/browse_frm/thread/9145497c04ad5c1b#

As has been mentioned repeatedly, your apparent inability to think for
yourself combined with the lack of acknowledgement to those who do
help you is rapidly burning through your social credit here.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: nested for loops

2006-03-22 Thread John Salerno
Jesus Rivero - (Neurogeek) wrote:
 It is, but range(2,2) doesn't do anything
 
 Jesus Rivero - Neurogeek
 
 
 John Salerno wrote:
 
 Can someone tell me why 'n' in this example isn't 2?

 for n in range(2, 10):
  for x in range(2, n):
  print 'x =', x, 'n =', n

  
 x = 2 n = 3
  

 
Thanks guys! I was thinking range(2, 2) gave you 2, for some reason!
-- 
http://mail.python.org/mailman/listinfo/python-list


nested for loops

2006-03-21 Thread John Salerno
Can someone tell me why 'n' in this example isn't 2?

  for n in range(2, 10):
for x in range(2, n):
print 'x =', x, 'n =', n


x = 2 n = 3
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: nested for loops

2006-03-21 Thread Christoph Haas
On Tue, Mar 21, 2006 at 09:54:21PM +, John Salerno wrote:
 Can someone tell me why 'n' in this example isn't 2?
 
   for n in range(2, 10):
   for x in range(2, n):
   print 'x =', x, 'n =', n
 
   
 x = 2 n = 3

Assume n=2. Then the first inner loop will run in a range(2,2) which is an
empty list. So nothing will happen.

Next outer loop: n=3...

Actually n becomes 2. But you don't do anything with it. :)

 Christoph
-- 
~
~
.signature [Modified] 1 line --100%--1,48 All
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: nested for loops

2006-03-21 Thread Jesus Rivero - (Neurogeek)
It is, but range(2,2) doesn't do anything

Jesus Rivero - Neurogeek


John Salerno wrote:

Can someone tell me why 'n' in this example isn't 2?

  for n in range(2, 10):
   for x in range(2, n):
   print 'x =', x, 'n =', n

   
x = 2 n = 3
  


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: nested for loops

2006-03-21 Thread Peter Otten
John Salerno wrote:

 Can someone tell me why 'n' in this example isn't 2?
 
   for n in range(2, 10):
 for x in range(2, n):
 print 'x =', x, 'n =', n
 
 
 x = 2 n = 3

Hint:

 range(2, 2)
[]

Peter
-- 
http://mail.python.org/mailman/listinfo/python-list


[ python-Feature Requests-1210975 ] break and continue-ing out of nested 'for' loops

2005-05-29 Thread SourceForge.net
Feature Requests item #1210975, was opened at 2005-05-29 22:59
Message generated for change (Tracker Item Submitted) made by Item Submitter
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detailatid=355470aid=1210975group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Interpreter Core
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Geraint Luff (geraintluff)
Assigned to: Nobody/Anonymous (nobody)
Summary: break and continue-ing out of nested 'for' loops

Initial Comment:
I wasn't entirely sure whether this should go under
Interpreter Core or Parser/Compiler, but whatever.

At the moment, break and continue can only break
out of the innermost for loop. I would appreciate a
way of breaking out of multiple 'for' or 'while' loops.

This would be extremely useful for instance when you
have some 'for' loops cycling through several
combinations, and you are using another 'for' loop to
check their validity (in my example, checking that my
two values are co-prime). In this situation, the only
solution I have found is to create a boolean variable
that is assigned a value when the check fails, just
before 'break'-ing out of a 'for' loop, and immediately
after that 'for' loop, having a statement that checks
the status of the boolean variable, and then
'continue's or 'breaks' again accordingly. I'm not an
experienced programmer, but this strikes me as
inefficient :p

Sorry if the above explanation is muddled. I can send a
short (15 lines) bit of code which might explain it better.

BTW, PHP seems to do this with break 2; or continue
2; which is very useful.

--G

--

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detailatid=355470aid=1210975group_id=5470
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[ python-Feature Requests-1210975 ] break and continue-ing out of nested 'for' loops

2005-05-29 Thread SourceForge.net
Feature Requests item #1210975, was opened at 2005-05-29 17:59
Message generated for change (Comment added) made by rhettinger
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detailatid=355470aid=1210975group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Interpreter Core
Group: None
Status: Closed
Resolution: Rejected
Priority: 5
Submitted By: Geraint Luff (geraintluff)
Assigned to: Nobody/Anonymous (nobody)
Summary: break and continue-ing out of nested 'for' loops

Initial Comment:
I wasn't entirely sure whether this should go under
Interpreter Core or Parser/Compiler, but whatever.

At the moment, break and continue can only break
out of the innermost for loop. I would appreciate a
way of breaking out of multiple 'for' or 'while' loops.

This would be extremely useful for instance when you
have some 'for' loops cycling through several
combinations, and you are using another 'for' loop to
check their validity (in my example, checking that my
two values are co-prime). In this situation, the only
solution I have found is to create a boolean variable
that is assigned a value when the check fails, just
before 'break'-ing out of a 'for' loop, and immediately
after that 'for' loop, having a statement that checks
the status of the boolean variable, and then
'continue's or 'breaks' again accordingly. I'm not an
experienced programmer, but this strikes me as
inefficient :p

Sorry if the above explanation is muddled. I can send a
short (15 lines) bit of code which might explain it better.

BTW, PHP seems to do this with break 2; or continue
2; which is very useful.

--G

--

Comment By: Raymond Hettinger (rhettinger)
Date: 2005-05-29 20:43

Message:
Logged In: YES 
user_id=80475

Sorry, this was considered and rejected long ago.  Most
other languages have made a similar choice.  In Python, the
best alternatives include:

* try/except which can penetrate multiple layers of looping
and/or function calls.

* boolean flags

* enclosing the innermost loops in a function so that a
return statement exits all pending loops within the function.

*and try/finally statements which are not a general purpose
replacement but do cover a whole class of use cases.

For more reading on the subject, take a look at Knuth's
Structured Programming with go to Statements.  That is a
rather thorough analysis of various structured constructs
and their strengths and weaknesses.

--

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detailatid=355470aid=1210975group_id=5470
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



Re: performance of Nested for loops

2005-05-20 Thread Charles Krug
On 20 May 2005 15:35:10 -0700, [EMAIL PROTECTED] [EMAIL PROTECTED]
wrote:
 Is there a better way to code nested for loops as far as performance is
 concerned.
 
 what better way can we write to improve the speed.
 for example:
 N=1
 for i in range(N):
for j in range(N):
do_job1
for j in range(N):
do_job2
 

What do you see when you profile the code?  

Premature Optimization is the root of all manner of evil and all that
good stuff.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: performance of Nested for loops

2005-05-20 Thread Larry Bates
You can use xrange(N) that way Python doesn't have
to build the 1 item lists 2 times.  Other than
that one would need to know why you would call do_job1
and do_job2 1 times each inside a 1 iteration
loop.  Most VERY large performance gains are due to
better algorithms not code optimization.

Larry Bates


[EMAIL PROTECTED] wrote:
 Is there a better way to code nested for loops as far as performance is
 concerned.
 
 what better way can we write to improve the speed.
 for example:
 N=1
 for i in range(N):
for j in range(N):
do_job1
for j in range(N):
do_job2
 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: performance of Nested for loops

2005-05-20 Thread Andrew Dalke
querypk wrote:
 Is there a better way to code nested for loops as far as performance is
 concerned.
 
 what better way can we write to improve the speed.
 for example:
 N=1
 for i in range(N):
for j in range(N):
do_job1
for j in range(N):
do_job2

For this case compute the range once

range_1 = range(1)
for i in range_1:
  for j in range_1:
do_job1()
  for j in range_1:
do_job2()

Using xrange(1) may be faster but you would need to test
that out for your case.

Andrew
[EMAIL PROTECTED]

-- 
http://mail.python.org/mailman/listinfo/python-list