Scripts and Python 3.0, part 1
Python 3.0 December 6th, 2008Yes, Python 3.0 was released earlier than Perl … what version was it? 6? 7? Anyway, I decided to go back to most of the scripts that were posted here. In the github repo we have 50 files in the “original scripts” directory. Let’s check how do they fare on Python 3.0 and what type of changes we need to do in order to make them work. Starting with code_01.py, which is a couple of lines long
myDNA = "ACGTACGTACGTACGTACGTACGT" print myDNA
Here we have one of the most evident differences between Python 2.x and 3.0. Now print is a function not a statement anymore, so whatever we want to print now should be passed as a function parameter. The above code would be changed to
myDNA = "ACGTACGTACGTACGTACGTACGT" print(myDNA)
That simple ins this case. But what are the advantages of print being a function over a statement? More flexibility, as can be seen in the link above. It is possible now to send different parameters to print and make the output richer by customizing separators between items, directing the output, etc.
A similar change would have to be made n our code_02.py. There are two print statements there that should be translated to the function. Trivial, so far. The original code
myDNA = "ACGTACGTACGTACGTACGTACGT" myDNA2 = "TCGATCGATCGATCGATCGA" print "First and Second sequences" print myDNA, myDNA2 myDNA3 = myDNA + myDNA2 print "Concatenated sequence" print myDNA3
and to work on Python 3
myDNA = "ACGTACGTACGTACGTACGTACGT"
myDNA2 = "TCGATCGATCGATCGATCGA"
print("First and Second sequences")
print(myDNA, myDNA2)
myDNA3 = myDNA + myDNA2
print("Concatenated sequence")
print(myDNA3)
This is would be the biggest (or at least the most common) change that we would need to make in the scripts posted here. Follow the repo to get the newer versions.
December 6th, 2008 at 1:17 pm
Have you tried running your programs through the 2to3 converter to see whether that might save you some work?
December 6th, 2008 at 1:42 pm
Code blocks 3 and 4 are exactly the same
Otherwise, good introduction to one of the larger differences between the old and new.
December 6th, 2008 at 2:11 pm
You last example has the same print statements.
December 6th, 2008 at 2:18 pm
It appears you’ve pasted your Python 2 example as your Python 3 solution in error.
December 6th, 2008 at 2:27 pm
Your python3 code does not have the brackets its the same as the original code.
December 6th, 2008 at 4:10 pm
Corrected, pasted from the wrong file and didn’t notice the error. Thanks everyone.
@Steve: not yet, these are very simple examples and I’m planning to do that for more complex things. Thanks for the suggestion.
December 8th, 2008 at 7:29 am
It’s worth to note that all the code above runs under Python 2.
The differences has been overhyped.
December 8th, 2008 at 5:41 pm
Indeed, it runs on 2.x, I’m just trying to point out the small differences and build up to a near future when large scripts would be translated into 3.0. A 500-line code can be easily changed by using 2to3, you might have 30 or 40 print statements that need to be converted to functions.