G
Gandalf
how can I do width python a normal for loop width tree conditions like
for example :
for x=1;x<=100;x+x:
print x
thanks
for example :
for x=1;x<=100;x+x:
print x
thanks
What you wrote would appear to be an infinite loop so I'll assume you meant
to assign something to x each time round the loop as well. The simple
Python translation of what I think you meant would be:
x = 1
while x <= 100:
print x
x += x
If you really insist on doing it with a for loop:
def doubling(start, limit):
x = start
while x <= limit:
yield x
x += x
...
for x in doubling(1, 100):
print x
What you wrote would appear to be an infinite loop so I'll assume you meant
to assign something to x each time round the loop as well. The simple
Python translation of what I think you meant would be:
x = 1
while x <= 100:
print x
x += x
If you really insist on doing it with a for loop:
def doubling(start, limit):
x = start
while x <= limit:
yield x
x += x
...
for x in doubling(1, 100):
print x
Gandalf said:...
I was hopping to describe it with only one command. most of the
languages I know use this.
It seems weird to me their is no such thing in python. it's not that I
can't fined a solution it's all about saving code
Gandalf said:I was hopping to describe it with only one command. most of the
languages I know use this.
It seems weird to me their is no such thing in python. it's not that I
can't fined a solution it's all about saving code
Gandalf said:I was hopping to describe it with only one command. most of the
languages I know use this.
It seems weird to me their is no such thing in python. it's not that I
can't fined a solution it's all about saving code
Python: 'makes common things easy and uncommon things possible'.
The large majority of use cases for iteration are iterating though
sequences, actual and virtual, including integers with a constant step
size. Python make that trivial to do and clear to read. Your example is
trivially written as
for i in range(11):
print 2**i
Python provide while loops for more fine-grain control, and a protocol
so *reuseable* iterators can plug into for loops. Duncan showed you
both. If you *need* a doubling loop variable once, you probably need
one more than once, and the cost of the doubling generator is amortized
over all such uses. Any Python proprammer should definitely know how to
write such a thing without hardly thinking. We can squeeze a line out
of this particular example:
def doubling(value, limit):
while value <= limit:
yield value
value += value
Terry Jan Reedy
Aaron said:Do you anticipate reusing it? You could make something a little more
extendable.
for x in iexpression( 'x', 1, 100, 'x+x' ):
print x
or
for x in iexpression( lambda x: x+x, 1, 100 ):
print x
I'm assuming you don't want or have a closed form, in this case x= 2**
_x.
Shouldn't the upper limit be exclusive in order to be Pythonic?Python: 'makes common things easy and uncommon things possible'.
The large majority of use cases for iteration are iterating though
sequences, actual and virtual, including integers with a constant step
size. Python make that trivial to do and clear to read. Your example is
trivially written as
for i in range(11):
print 2**i
Python provide while loops for more fine-grain control, and a protocol
so *reuseable* iterators can plug into for loops. Duncan showed you
both. If you *need* a doubling loop variable once, you probably need
one more than once, and the cost of the doubling generator is amortized
over all such uses. Any Python proprammer should definitely know how to
write such a thing without hardly thinking. We can squeeze a line out
of this particular example:
def doubling(value, limit):
while value <= limit:
yield value
value += value
MRAB said:Shouldn't the upper limit be exclusive in order to be Pythonic?
I was hopping to describe it with only one command. most of the
languages I know use this.
It seems weird to me their is no such thing in python. it's not that I
can't fined a solution it's all about saving code
for x in (2**i for i in xrange(10)):
print x
This is by far the most concise solution I've seen so far.
And it should never be about conserving code.
Also, Python IS NOT C (to be more specific: Python
is not a C-class language).
making your code easy to read and easy to maintain is far more important.
for x in (2**i for i in xrange(10)):
print x
will also print 1, 2, 4, 8, ... up to 1000.
James Mills said:This is by far the most concise solution I've seen so far.
On Oct 19, 2:30Â pm, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com.au> wrote:
[snip]making your code easy to read and easy to maintain is far more
important.
for x in (2**i for i in xrange(10)):
  print x
will also print 1, 2, 4, 8, ... up to 1000.
I would say up to 512; perhaps your understanding of "up to" differs
from mine.
Easy to read? I'd suggest this:
for i in xrange(10):
print 2 ** i
Steven said:On Oct 19, 2:30 pm, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com.au> wrote:
[snip]making your code easy to read and easy to maintain is far more
important.
for x in (2**i for i in xrange(10)):
print x
will also print 1, 2, 4, 8, ... up to 1000.
I would say up to 512; perhaps your understanding of "up to" differs
from mine.
Well, mine is based on Python's half-open semantics: "up to" 1000 doesn't
include 1000, and the highest power of 2 less than 1000 is 512.
Steven said:On Oct 19, 2:30Â pm, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com.au> wrote:
[snip]
making your code easy to read and easy to maintain is far more
important.
for x in (2**i for i in xrange(10)):
  print x
will also print 1, 2, 4, 8, ... up to 1000.
I would say up to 512; perhaps your understanding of "up to" differs
from mine.
Well, mine is based on Python's half-open semantics: "up to" 1000
doesn't include 1000, and the highest power of 2 less than 1000 is 512.
We're talking about an English sentence, not a piece of Python code.
When you say "I'm taking the train to X", do you get off at the station
before X, as in "getting off at Redfern"?
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.