Spotting the differences between Py2 and Py3

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 15803 Views - Last Post: 28 April 2012 - 08:38 PM Rate Topic: -----

#1 Nallo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 163
  • View blog
  • Posts: 255
  • Joined: 19-July 09

Spotting the differences between Py2 and Py3

Post icon  Posted 02 July 2011 - 10:23 AM

*
POPULAR

I would love to see people answer Python 2 question with Python 2 examples and Python 3 questions with Python 3 examples. For seasoned Python programmers it doesn't matter. They will figure out themselves. But for python beginners it can be quite frustrating:

-print "blah" doesn't work
-oops, print has become a function, so it should be print("blah")

-raw_input("input something") didn't work
-oops. raw_input has been renamed to input in Python 3

Don't make beginners jump through a firey ring :boat: . They usually won't tell you it's py2 or py3. But try your best to give an matching answer.

So how to figure it out which version a newbee used :chef: ?
Newbee questions often have print and/or input in them. So let's look at them:

-input instead of raw_input ---> weak hint at Python3. Unfortunately quite some newbees use input in py2 when they should have used raw_input.
-print with brackets ---> strong hint at Python3. While possible in py2 no one does there.
-print without brackets ---> guaranteed python2. It wouldn't work in python3.
-input and a print with brackets ---> almost guaranteed python3.
-raw_input ---> guaranteed python 2. raw_input doesn't exist in py3.

This post has been edited by atraub: 02 July 2011 - 11:49 AM


Is This A Good Question/Topic? 5
  • +

Replies To: Spotting the differences between Py2 and Py3

#2 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Spotting the differences between Py2 and Py3

Posted 02 July 2011 - 11:43 AM

Let's be honest: If I specifically notice which version someone is using, I'll answer with the corresponding one. However, often times I just don't notice. Sometimes it does add an extra step when someone goofs, and the OP comes back and says "Heeeey that didn't work! Here's my error: ". We see the error and we fix it. People make mistakes and it's inevitable. No one here has ever said "I don't care what version of Python you're using, I'm answering using x.x". Calling it a fiery ring is a bit dramatic Nallo ;)


That being said, the "Spotting the differences between Python 2 and Python 3" aspect of this post is quite valuable... Pinned and featured!

This post has been edited by atraub: 02 July 2011 - 11:50 AM

Was This Post Helpful? 0
  • +
  • -

#3 Nallo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 163
  • View blog
  • Posts: 255
  • Joined: 19-July 09

Re: Spotting the differences between Py2 and Py3

Posted 02 July 2011 - 01:07 PM

I have a favor for drama :innocent:

One more cause of misunderstanding between py2 and py3:
# py2
>>> range(5)[2:4]
[2, 3]
# py 3
>>> range(5)[2:4]
Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    range(5)[2:4]
TypeError: sequence index must be integer, not 'slice'



range in py3 doesn't create a list anymore. So list slicing won't work.
A range object in py3 behaves more like an iterator (+it allows indexing). Which is a fine thing, as thats what you want most of the time anyway.
To use it as in py2 one must first turn the range object into a list
>>> myli = list(range(5))
>>> myli[2:4]
[2, 3]



Edit: Btw. atraub, there was no need to give me credit on that first post. You already did in an other thread where it didn't really fit in.

This post has been edited by Nallo: 02 July 2011 - 01:11 PM

Was This Post Helpful? 1
  • +
  • -

#4 saimanoj  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 19-August 10

Re: Spotting the differences between Py2 and Py3

Posted 02 July 2011 - 10:17 PM

I am an amateur in Python, which syntax should i learn and practice. Or the other way, which version of Python should I use?
Was This Post Helpful? 0
  • +
  • -

#5 kiwi_steve  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 109
  • Joined: 26-September 09

Re: Spotting the differences between Py2 and Py3

Posted 03 July 2011 - 12:52 AM

The bigger problem is that you've classified us all as either beginners or seasoned programmers. I am neither. I have about 18 months of on-again-off-again experience with 2.6 and 2.7, but virtually none with 3. I don't use it, don't intend to use it in the near future, and don't have the time or inclination to learn it just to provide multi-versioned help. I figure the differences are minimal, and for small code snippets your average person will either figure it out or ask... or someone like you with more experience will jump in and point out potential pitfalls.

Unfortunately, expecting everyone to be able to answer in the version asked is probably not going to happen for a few years until 3 gets a good hold and 2 starts to die away. Until then, I guess the 'seasoned programmers' can jump in and point out the problems when people provide solutions... That will help us all learn.

Steve
Was This Post Helpful? 0
  • +
  • -

#6 milleja46  Icon User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 272
  • Joined: 17-December 09

Re: Spotting the differences between Py2 and Py3

Posted 16 July 2011 - 02:50 PM

Wow...that print("") thing annoys me though i could never figure out the math differences because i was testing how to do something i wanted to do at home in a classroom at school not realizing it was py3 and not py2 XD. But still tidies up the code a bit :D love python
Was This Post Helpful? 0
  • +
  • -

#7 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Spotting the differences between Py2 and Py3

Posted 16 July 2011 - 08:36 PM

Thanks to py3 making print a function, you can do cool stuff like this:

print("message" end=" ")

This makes it so that 'print' puts a blank space at the end of your line rather than a new-line character... you could also do

print("message" end=":^)\n")

if you want to cheer up your users on a crummy day :)

This post has been edited by atraub: 03 March 2012 - 10:00 PM

Was This Post Helpful? 0
  • +
  • -

#8 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 948
  • View blog
  • Posts: 2,357
  • Joined: 15-February 11

Re: Spotting the differences between Py2 and Py3

Posted 05 August 2011 - 08:29 AM

I just took up Python a few nights ago and it's quite a language. What's interesting is the need for indentation so you guys here in Python shouldn't really have the problem of mumbled code like the rest of us.

There's a lot of talk about the different versions of Python I see. Some people prefer one over the next for different reasons. I'm currently on v3.2.1. What advantages you get with v3 as opposed to v2 or vice versa?

This post has been edited by codeprada: 05 August 2011 - 08:30 AM

Was This Post Helpful? 0
  • +
  • -

#9 kiwi_steve  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 109
  • Joined: 26-September 09

Re: Spotting the differences between Py2 and Py3

Posted 07 August 2011 - 03:12 AM

View Postcodeprada, on 06 August 2011 - 04:29 AM, said:

What advantages you get with v3 as opposed to v2 or vice versa?


Its really just maturity. 2.x has been around a long time, and there is a huge amount of code out there for it. 3.x is newer. The idea is that eventually all Python code will port or die - and the same goes for Python programmers :)

Personally I am still using 2.x because my workplace is still using it. I can't see that changing in the next 12 months, possibly 24.

If you are just getting into it, then keep an open mind - because there are libraries out there in 2.x-land that haven't made the trip over to 3.x-ville yet. If you have coding experience, then either should work for you.

Steve
Was This Post Helpful? 1
  • +
  • -

#10 CMIT  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-February 12

Re: Spotting the differences between Py2 and Py3

Posted 26 February 2012 - 01:54 PM

I would way when possible code should be future-proofed.

In my 2.7.2+ versiion of python it accepts both print "blah" and print("blah"). The latter is however admissable under Python3+. Thus I will use that whenever possible. Just like raw_input and input are effectively the same. Using input is better because it's acceptable under Python3+
Was This Post Helpful? 0
  • +
  • -

#11 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Spotting the differences between Py2 and Py3

Posted 03 March 2012 - 09:58 PM

raw_input and input are not effectively the same in python 2. You should not use input in python 2 because it creates major security issues!
Was This Post Helpful? 0
  • +
  • -

#12 Douggie0001  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 03-November 11

Re: Spotting the differences between Py2 and Py3

Posted 21 March 2012 - 09:05 PM

Another thing, if a newbie is trying out tkinter, there is a difference

Python 2 has a capital T in tkinter and will import items like so:
from Tkinter import messagebox



Python 3 does the names different. the t in tkinter is lower case and the naming is shifted:
from tkinter import tkMessageBox


Was This Post Helpful? 0
  • +
  • -

#13 python122  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 1
  • Joined: 27-April 12

Re: Spotting the differences between Py2 and Py3

Posted 27 April 2012 - 12:55 AM

Please go through this blog.

Will help you a lot in the beginning time

mod: link removed

View Postsaimanoj, on 02 July 2011 - 10:17 PM, said:

I am an amateur in Python, which syntax should i learn and practice. Or the other way, which version of Python should I use?


please go through this blog, will help you in a beginning time

mod: link removed

This post has been edited by modi123_1: 28 April 2012 - 09:07 PM
Reason for edit:: removed spammy links

Was This Post Helpful? -4
  • +
  • -

#14 kiwi_steve  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 109
  • Joined: 26-September 09

Re: Spotting the differences between Py2 and Py3

Posted 27 April 2012 - 09:15 PM

Self-advertising on your first post, and it doesn't even relate to the topic... not a good way to start.

View Postpython122, on 27 April 2012 - 08:55 PM, said:

Please go through this blog.

Will help you a lot in the beginning time

Was This Post Helpful? 0
  • +
  • -

#15 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Spotting the differences between Py2 and Py3

Posted 28 April 2012 - 06:29 PM

Good job python122, you managed to make a blog that copy and pasted out of Tutorials Point.

Edit:
Also, I've alerted tutorials Point about what you've done. I expect they'll be in touch soon :)

This post has been edited by atraub: 28 April 2012 - 06:45 PM
Reason for edit:: EDIT

Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2