473,388 Members | 1,327 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

what's the python for this C statement?

Hi there,
I'm relative new to Python and I discovered that there's one single way
to cycle over an integer variable with for:
for i in range(0,10,1)

which is equivalent to:
for (i = 0; i < 10; i++)

However, how this C statement will be translated in Python?

for (j = i = 0; i < (1 << H); i++)

Thanks
Oct 20 '08 #1
6 2218
On Mon, Oct 20, 2008 at 2:56 AM, Michele <mi*****@nectarine.itwrote:
Hi there,
I'm relative new to Python and I discovered that there's one single way
to cycle over an integer variable with for:
for i in range(0,10,1)
Actually, you want:

for i in range(10):

Since starting at 0 and using a step of 1 are the defaults.
>
which is equivalent to:
for (i = 0; i < 10; i++)

However, how this C statement will be translated in Python?

for (j = i = 0; i < (1 << H); i++)
There's no nice way to translate more complex 'for' statements like
that (unless you can calculate the end value somehow).
You basically just have to use a 'while' loop instead:

i = j = 0
while i < 1 << H:
#body goes here
i += 1

Since bit-shifting doesn't get used much in Python and iteration
through a generator or the items of a collection is more common, this
doesn't end up being a problem in practice.

Cheers,
Chris
--
Follow the path of the Iguana...
http://rebertia.com

>
Thanks
--
http://mail.python.org/mailman/listinfo/python-list
Oct 20 '08 #2
Michele <mi*****@nectarine.itwrites:
Hi there,
I'm relative new to Python and I discovered that there's one single way
to cycle over an integer variable with for:
for i in range(0,10,1)
Please use xrange for this purpose, especially with larger
iterations. range actually allocates a sequence.
However, how this C statement will be translated in Python?

for (j = i = 0; i < (1 << H); i++)
If H doesn't change during the loop, you can use:

j = 0
for i in xrange(1 << H):
...

If H can change, simply rewrite it into an obvious 'while' loop.
Oct 20 '08 #3
On Mon, 20 Oct 2008 12:34:11 +0200, Hrvoje Niksic wrote:
Michele <mi*****@nectarine.itwrites:
>Hi there,
I'm relative new to Python and I discovered that there's one single way
to cycle over an integer variable with for: for i in range(0,10,1)

Please use xrange for this purpose, especially with larger iterations.
range actually allocates a sequence.
Actually that doesn't really matter unless the loop is extremely big (a
million), since range is much faster than xrange for smaller values
(which might be the more typical case). And I think range will be an
iterator in the future, imitating the behavior of xrange. So it doesn't
really matter anyway.
>However, how this C statement will be translated in Python?

for (j = i = 0; i < (1 << H); i++)

If H doesn't change during the loop, you can use:

j = 0
for i in xrange(1 << H):
...

If H can change, simply rewrite it into an obvious 'while' loop.
I would suggest a different thing: rewrite it in python instead of
translating it.

Oct 20 '08 #4
Lie Ryan wrote:
(which might be the more typical case). And I think range will be an
iterator in the future, imitating the behavior of xrange. So it doesn't
really matter anyway.
In 3.0, range is a class and range(arg) is a re-iterable instance of
that class.
>>a = range(10,2,-3)
a
range(10, 2, -3)
>>list(a)
[10, 7, 4]
>>list(a)
[10, 7, 4]

Re-iterablility is handy if you want to iterate twice over the same
range, especially is the range object is computed elsewhere.

Map and filter, on the other hand, produce one-use iterators. Filter
takes one iterable as input and map takes many iterables. Both make no
presumption that the inputs are re-iterable rather than a one-time
iterators.

Terry Jan Reedy

Oct 20 '08 #5
On Mon, 20 Oct 2008 17:01:13 +0000, Lie Ryan wrote:
On Mon, 20 Oct 2008 12:34:11 +0200, Hrvoje Niksic wrote:
>Michele <mi*****@nectarine.itwrites:
>>Hi there,
I'm relative new to Python and I discovered that there's one single
way to cycle over an integer variable with for: for i in range(0,10,1)

Please use xrange for this purpose, especially with larger iterations.
range actually allocates a sequence.

Actually that doesn't really matter unless the loop is extremely big (>
a million), since range is much faster than xrange for smaller values
(which might be the more typical case).

That hasn't been true for some time now:
>>timeit.Timer('range(3)').repeat()
[1.8658630847930908, 0.89470076560974121, 0.88842916488647461]
>>timeit.Timer('xrange(3)').repeat()
[0.71410012245178223, 0.69949698448181152, 0.69640421867370605]
That's on Python 2.5.

--
Steven
Oct 20 '08 #6
Lie Ryan <li******@gmail.comwrites:
On Mon, 20 Oct 2008 12:34:11 +0200, Hrvoje Niksic wrote:
>Michele <mi*****@nectarine.itwrites:
>>Hi there,
I'm relative new to Python and I discovered that there's one single way
to cycle over an integer variable with for: for i in range(0,10,1)

Please use xrange for this purpose, especially with larger iterations.
range actually allocates a sequence.

Actually that doesn't really matter unless the loop is extremely big (a
million), since range is much faster than xrange for smaller values
(which might be the more typical case).
This used to be the case, but is no longer true as of (I think) Python
2.4. xrange is preferred over range, unless you actually need the
list, of course. You are right that it is changing, but that is in
Python 3, which only has range in the meaning of today's xrange.

$ python -m timeit 'for i in xrange(10): pass'
1000000 loops, best of 3: 1.13 usec per loop
$ python -m timeit 'for i in range(10): pass'
1000000 loops, best of 3: 1.72 usec per loop
Oct 21 '08 #7

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

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
54
by: Brandon J. Van Every | last post by:
I'm realizing I didn't frame my question well. What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump up in your chair and scream "Wow! Ruby has *that*? That is SO...
226
by: Stephen C. Waterbury | last post by:
This seems like it ought to work, according to the description of reduce(), but it doesn't. Is this a bug, or am I missing something? Python 2.3.2 (#1, Oct 20 2003, 01:04:35) on linux2 Type...
46
by: Reinhold Birkenfeld | last post by:
Hello, another Perl/Python question: the subject says it all. Perl is going to change dramatically to become a more powerful and easier to (read|write) language. Is Python taking a similar...
137
by: Philippe C. Martin | last post by:
I apologize in advance for launching this post but I might get enlightment somehow (PS: I am _very_ agnostic ;-). - 1) I do not consider my intelligence/education above average - 2) I am very...
19
by: Kalle Anke | last post by:
I'm confused, I want to read/write XML files but I don't really understand what library to use. I've used DOM-based libraries in other languages, is PyXML the library to use?
34
by: emrahayanoglu | last post by:
Hello Everyone, Now, I'm working on a new web framework. I tried many test on the other programming languages. Then i decided to use python on my web framework project. Now i want to listen...
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 428 open ( +6) / 3417 closed ( +2) / 3845 total ( +8) Bugs : 939 open ( +6) / 6229 closed (+17) / 7168 total (+23) RFE : 240 open...
26
by: brenocon | last post by:
Hi all -- Compared to the Python I know and love, Ruby isn't quite the same. However, it has at least one terrific feature: "blocks". Whereas in Python a "block" is just several lines of...
13
by: John Dann | last post by:
A Python newbie, but some basic understanding of how classes, objects etc work in eg VB.Net. However, I'm struggling a little to translate this knowledge into the Python context. I'm trying to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...

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.