Difference between zip() functions in Python 2 and Python 3 [duplicate] - python

This question already has answers here:
Getting a map() to return a list in Python 3.x
(11 answers)
Closed 4 years ago.
I was wondering what the difference is between the zip() function in python 2 and python 3 is. I noticed when using the timeit module on both functions that the python 3 function was a lot faster. Thanks so much in advance :)

Difference between Python 2 and Python 3 is Python 3 returns an iterators. Idea of this saving memory.

In Python 3, the zip() function returns an iterator, meaning that you can only exhaust elements once, whereas Python 2 returns an iterable itself.
see here:
Python 2 Doc, Python 3 Doc

Related

Different result for simple math in python 2 and 3 [duplicate]

This question already has answers here:
Division in Python 3 gives different result than in Python 2
(3 answers)
Closed 1 year ago.
Can someone help me figure out why the same simple calculation gets different answers in python 2 and 3? The expression is ​(0.2**(-2)-1)**(1/2).
When I use python 2 in a Canopy IDE, I get 1.
When I use python 3 in google colab, I get 4.98.
In both cases I am literally running exactly the above expression. Any ideas?
Integer division works differently in Python 2 and 3.
For example (1/2) will return
0 in Python 2, and
0.5 (a float) in Python 3.

Zip in Python: How to return tuples instead of a zip object [duplicate]

This question already has answers here:
Python range() and zip() object type
(3 answers)
Closed 2 years ago.
I am currently using Python 3.7.3 and am relatively new. My understanding was that the zip() function returns a list of tuples, however in practice I have found it returns a 'zip' object. Is there a way that I can get this function to return a list of tuples or a similar function that can accomplish my purpose?
Thanks so much!
I don't know if that was automatic or a mod did that, but thank you!
All you need to do is convert it to a list.
list(zip(....))

Why print(a++) has syntax errors in python, but not print(++a) [duplicate]

This question already has an answer here:
Preincrement operators in python
(1 answer)
Closed 5 years ago.
I am a beginner in python and I am using Python 3.5. The python console complains invalid syntax for the below statement:
a = 5
print(a++)
But print(++a) works fine. Can anyone help me understand the difference?
Btw, it seems that print(a+=1) also doesn't work.
Thanks!
++a is just the same as doing (+(+a)). I.E: You're using the mathematical addition operator on the variable a (with implied zeroes). So the result is a
a++ is not valid python syntax (unlike other languages).
a += 1 is an assignment. It is equivalent to a = a + 1 - you can not print an assignment

lower versus length syntax in python? [duplicate]

This question already has answers here:
Why does Python code use len() function instead of a length method?
(7 answers)
Closed 7 years ago.
I'm new to Python and I have a question about the string operations. Is there an over-arching reason that I should understand as to why the lower operation is written as 'variable.lower()' while another one, say length, would be written as 'len(variable)'?
lower is a string method, that is, a function built in to the string object itself. It only applies to string objects.
len is a built in function, that is, a function available in the top namespace. It can be called on many different objects (strings, lists, dicts) and isn't unique to strings.

Does the Python range generator generate all values or yields them progressively? [duplicate]

This question already has answers here:
Why is "1000000000000000 in range(1000000000000001)" so fast in Python 3?
(12 answers)
Closed 7 years ago.
When you do something like this:
for i in range(5):
print i
What does Python do? Does it first generate an array with [0,1,2,3,4] and then goes over each item printing it?
Similar to:
for i in [0,1,2,3,4]:
print i
Or does it print each number as it generates them? Something like:
Generate 0 assign 0 to i print i
Generate 1 -> assign 1 to i -> print i
Generate 2 -> assign 2 to i -> print i
Generate 3 -> assign 3 to i -> print i
Generate 4 -> assign 4 to i -> print i
Update
I have added the tag for Python 2.7. I didn't think my question was version specific, but it seems it is!
Right now I am working with Python 2.7 so my question refers to that. But I find very valuable the information comparing Python 2 range against Python 3 range.
In Python 2, range() returns the whole list object then that list
object is iterated by for.
In Python 3, it returns a memory efficient iterable, which is an
object of its own with dedicated logic and methods, not a list. Now
for will get each value as it is generated by that iterable.
In Python 2 there is xrange() to get something like what Python 3's
range() do. Python 2's xrange is kind of halfway between Python 2's
range and Python 3's range. It's better than the former but not as
good as the latter.
Finally, in Python 3, if you want the whole list do:
>>> list(range(...))
In Python 2, it returns a list of the values. Try this to see
print range(4)
In Python 3, it returns an iterable object. You can convert it to list using this:
print(list(range(4))

Categories