martes, 12 de septiembre de 2017

Differences between Python 2.7.3 and Python 3.0

Differences between Python 2.7.3 and Python 3.0

 http://web.cs.wpi.edu/~heineman/html/teaching_/cs110x/days/27-30.html

 

Differences between Python 2.7.3 and Python 3.0

Language shapes the way we think, and determines what we can think about.
Benjamin Lee Whorf

1 Language differences

The book has been upgraded to Python 3.0, the latest version of this language. However, the code for the course will be Python 2.7.3, and there will be some annoying differences.
The reason for this discrepancy is that Python has "grown up", given its widespread usage. With any technology or language, there is always a starting up period, during which the designers make key changes that sometimes radically change the technology. Python is undergoing similar growing pains.
There are numerous places on the web where you can find extremely technical detailed explanations for the differences. Here I only want to identify a (potentially growing) list of differences that you need to understand.
The primary issue is that the textbook contains examples that will not work if you type them in. I understand this is frustrating, but it allowed me to keep the costs for this course down.

1.1 Print with multiple arguments

On page 10 of the book, the following example is given.
print ("2 + 3 = ", 2+3)
Type this in, and you will get the following output:
(’2 + 3 = ’, 5)
Which is different from the expected text 2 + 3 = 5 as shown in the book. What is going on? Well, in Python 2.7.3, the print function actually works without parantheses. Try it! Type the following, and it works:
print 19+3
Python was designed to be simple, and since print was such a common function, the designers initially thought to just leave out the unnecessary parantheses (they thought) in the function call. However, this is bad program design.
What happens here is that Python 2.7.3 treats print special, and instead processes the remaining ("2 + 3 = ", 2+3) as if you were declaring a tuple. Recall that a tuple is a non-mutable grouping of elements, which can be identified by the open/closed parantheses used to declare it. The equivalent Python 3 code is as follows.
x = ("2 + 3 = ", 2+3) print (x)
And this leads to the printing of the parantheses, which is not what we are looking for.
It is for this reason that in all of our assignments, you will instead do the following:
print ("2 + 3 = " + str(2+3))
which uses string concatenation, and the str operator to convert numbers into string values.

1.2 Input from the keyboard

Throughout the book, you will see the author use the following construct when reading input from the keyboard:
x = eval(input("Enter a number: "))
Unfortunately, this is a new addition to Python 3.0 and will not work as expected for Python 2.7.3; fortunately, there is an easy work-around:
x = input("Enter a number: ")
Just leave out the eval(...) and it will work as expected.

1.3 Integer Division

Let me first say that I am not making this up. Of course, you won’t believe me. Since the dawn of the computer programming era (1950s) the division operator (/) has been handled specially. If you are computing with integer values, then division is a truncate operator of the form "int1 / int2 yields an int3". Thus, in nearly every programming language, the following statement x = 4 / 3 would result in x being set to 1.
However, the designers of Python have decided to change this behavior for Python 3.0 and, in doing so, they have introduce a severe backward compatibility concern (for more, see this posting).
The only way to avoid the problem is to think really hard whenever you write code that uses division. For example, do you intend for the result to maintain floating point accuracy? Then use floating point values, such as x = 4.0 / 3.0. If you don’t know whether a variable is an int or a float, then be sure to multiply by 1.0. For example, given the normal equation x = y / z you would be sure to say x = 1.0*y / z to force the computation to yield a floating point number.

1.4 Version : 2013/01/07


 

No hay comentarios:

Publicar un comentario