473,704 Members | 6,781 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

loops

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
Oct 18 '08 #1
17 1320
Gandalf <go******@gmail .comwrote:
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
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

Oct 18 '08 #2
On Oct 18, 12:39*pm, Duncan Booth <duncan.bo...@i nvalid.invalid>
wrote:
Gandalf <goldn...@gmail .comwrote:
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

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
thanks
Oct 18 '08 #3
On Oct 18, 12:39*pm, Duncan Booth <duncan.bo...@i nvalid.invalid>
wrote:
Gandalf <goldn...@gmail .comwrote:
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

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
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
Oct 18 '08 #4
Gandalf wrote:
On Oct 18, 12:39 pm, Duncan Booth <duncan.bo...@i nvalid.invalid>
wrote:
>Gandalf <goldn...@gmail .comwrote:
>>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
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
...
>
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
You'd not save code, but only lines (and clearness). You'd also
need more (non-space) characters

Python saves confusion and arbitrariness =you'll usually code
faster, because you don't think so much about voluptuous
multimulti..pos sibilites, not worth the play: one-ness of mind

If insistent, you could sometimes save lines like this ;-)

x=1
while x<=100: print x; x+=x
Robert
Oct 18 '08 #5
Gandalf wrote:
On Oct 18, 12:39*pm, Duncan Booth <duncan.bo...@i nvalid.invalid>
wrote:
>Gandalf <goldn...@gmail .comwrote:
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

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

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
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.

Oct 18 '08 #6
Gandalf wrote:
On Oct 18, 12:39 pm, Duncan Booth <duncan.bo...@i nvalid.invalid>
wrote:
>Gandalf <goldn...@gmail .comwrote:
>>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
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

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

Oct 18 '08 #7
On Oct 18, 11:31*am, Terry Reedy <tjre...@udel.e duwrote:
Gandalf wrote:
On Oct 18, 12:39 pm, Duncan Booth <duncan.bo...@i nvalid.invalid>
wrote:
Gandalf <goldn...@gmail .comwrote:
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
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
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
I agree that using range() for simple iterations is the way to go.
Here are some examples of python expressions you'd use in specific
situations:

# instead of for (i = 0; i < 100; i++)
for i in range(100): pass

# instead of for (i = 10; i < 100; i++)
for i in range(10, 100): pass

# instead of for (i = 1; i < 100; i += 2)
for i in range(1, 100, 2): pass

# instead of for (i = 99; i >= 0; i--)
for i in range(100)[::-1]: pass

There's always a way to do it, and it's almost always really simple :-D
Oct 18 '08 #8
wbowers <wi************ @gmail.comwrote :
I agree that using range() for simple iterations is the way to go.
except that as Terry said, "The large majority of use cases for iteration
are iterating though sequences"

I very rarely use range() in iterations.
Here are some examples of python expressions you'd use in specific
situations:
....
# instead of for (i = 99; i >= 0; i--)
for i in range(100)[::-1]: pass
or:
for i in xrange(99, -1, -1): pass
Oct 18 '08 #9
Aaron Brady wrote:
Gandalf wrote:
>On Oct 18, 12:39 pm, Duncan Booth <duncan.bo...@i nvalid.invalid>
wrote:
>>Gandalf <goldn...@gmail .comwrote:
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
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
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

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.

#and to learn even more about this, import this:
import this # ;-)
Oct 18 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
5114
by: Oleg Leschov | last post by:
Could there be means of exiting nested loops in python? something similar to labelled loops in perl.. I consider it irrating to have to make a flag for sole purpose of checking it after loop if we need to break once more... So maybe there should be an argument to break that is an int which is a number of loops to break. So it would default to 1 (or whatever means the current enclosing loop), not breaking any code...
15
6574
by: JustSomeGuy | last post by:
I have a need to make an applicaiton that uses a variable number of nested for loops. for now I'm using a fixed number: for (z=0; z < Z; ++z) for (y=0; y < Y; ++y) for (x=0; x < X; ++x)
4
2131
by: Dr. David Kirkby | last post by:
I have a program that loops through and changes all the elements on an array n times, so my code looks like this: for (n=1; n < n_max; ++n) for(i=imax; i >= 0; --i) { for(j=0 ; j < jmax; ++j) { /* main bulk of code goes here */ } }
46
9916
by: Neptune | last post by:
Hello. I am working my way through Zhang's "Teach yourself C in 24 hrs (2e)" (Sam's series), and for nested loops, he writes (p116) "It's often necessary to create a loop even when you are already in a loop." Then he goes on to portray a contrived example that doesn't tell me under what conditions a nested loop might be favoured as a solution? i.e. what are nested loops useful for? What kinds of algorithms are served by nested loops?...
6
1708
by: Scott Brady Drummonds | last post by:
Hi, everyone, I was in a code review a couple of days ago and noticed one of my coworkers never used for() loops. Instead, he would use while() loops such as the following: i = 0; while (i < n) { ...
17
3051
by: John Salerno | last post by:
I'm reading Text Processing in Python right now and I came across a comment that is helping me to see for loops in a new light. I think because I'm used to the C-style for loop where you create a counter within the loop declaration, for loops have always seemed to me to be about doing something a certain number of times, and not about iterating over an object. The reason for this distinction comes from the fact that I read a lot how...
10
3971
by: Putty | last post by:
In C and C++ and Java, the 'for' statement is a shortcut to make very concise loops. In python, 'for' iterates over elements in a sequence. Is there a way to do this in python that's more concise than 'while'? C: for(i=0; i<length; i++) python: while i < length:
2
2297
by: bitong | last post by:
I'm a little bit confuse with regard to our subject in C..We are now with the Loops..and I was just wondering if given a problem, can you use Do-while loops instead of a for loops or vise versa? are there instances that you must use a Do-while loops instead of a for loops or a while loop? or you can use any types of loops in any given problem?
3
2417
by: monomaniac21 | last post by:
hi all i have a script that retrieves rows from a single table, rows are related to eachother and are retrieved by doing a series of while loops within while loops. bcos each row contains a text field they are fairly large. the net result is that when 60 or so results are reitreved the page size is 400kb! which takes too long to load. is there a way of shorterning this? freeing up the memory say, bcos what is actually displayed is not...
8
7252
by: Nathan Sokalski | last post by:
I have several nested For loops, as follows: For a As Integer = 0 To 255 For b As Integer = 0 To 255 For c As Integer = 0 To 255 If <Boolean ExpressionThen <My CodeElse Exit For Next If Not <Boolean ExpressionThen Exit For Next If Not <Boolean ExpressionThen Exit For
0
8679
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9134
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9023
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8974
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7885
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6603
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5927
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2078
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.