The new print function in Python 3

The print statement has gone. A function replaces it in Python 3.0. Apart from the other changes, this one probably needs the most mind and finger retraining, cause its something I use a lot while fiddling with the interpreter.

So far, till Python 2.6

So this is how you all have been happily typing away in Python 2.x:

print "Print this and print a newline"
print "Print this, but not a newline",

Now, if you had a collection of elements you wanted to print with certain formatting, say with punctuation, here’s how you would do it:

basket = ('Apple', 'Oranges', 'Banana')
print ", ".join(basket) + "."

# Which produces the output:
"Apple, Oranges, Banana."
About the print() function in Python 3.0
About the print() function in Python 3.0

Some would even prefer to use a logical loop instead, for readability’s sake. But this method does require a string operation to happen, and loops probably would take up lines, or worse, in certain cases.

Now, in Python 3.0 onwards

The print statement has gone for good and the print() function comes in.

This decision is well explained in the PEP (Python Enhancement Proposals) and the specific paper on it can be found here (Numbered 3105)

Digging into this function’s documentation would reveal all about it in simple text (If you are an avid reader of the Python Documentation). Of what’s continued here is all just for newbies-only, with some demonstrations.

A simple demo equivalent to the first as above:

print ("Print this line, and print a newline")
print ("Print this line, but not a newline", end="")

You might notice that its got a little complex here, with a keyword argument being supplied instead of an ending comma as before.

But lets see the full power of this new print() function by doing the same punctuation to the fruit basket as before:

basket = ('Apple', 'Oranges', 'Banana')

print (*basket, sep=", ", end=".\n")

# Which produces the same output as desired:

"Apple, Oranges, Banana."

We do the same thing as before, except that we don’t require a loop, nor string operations. The function’s two keyword arguments sep and end handle the complex jobs for us. Basically, this is what they mean:

  • sep – Seperator string – Defines the string that is to be placed between every two values printed.
  • end – Ender string – Defines the string to be printed at the end of the print function.

By default, sep has a space (‘ ‘) and end is a newline (‘ \n ‘). So a simple signature of this new print function would be like:

print ( [object(s)], sep=' ', end='\n' )

And finally, the Star of this show

The one last addition to the print function made by the Python team was the file keyword argument. That’s right, one more keyword argument.

  • file – Object Name – Specify file/object to print to.

This one is a really cool addition, and it defaults to sys.stdout, (i.e.) your terminal. Thus, a more complete print() signature is:

print ( [object(s)], sep=' ', end='\n' , file=sys.stdout)
# No, you obviously don't have to import sys for this.
# Its just to describe what file its printing to.
# (/dev/stdout in UNIX's case)

All the file argument needs is an object that supports any write(string) method.

Now lets try printing the fruit basket to a file than to the terminal as default:

fruits = open("fruitsfile.txt", "w")
basket = ('Apple', 'Oranges', 'Banana')
print (*basket, sep=", ", end=".\n", file=fruits)

# Effectively prints the punctuated line,
# to the file named fruitsfile.txt

This could have a great use, especially with logging! And since it supports any object with a write method, the possibilities could be endless.

Now tell me if you still hate that its got parentheses? Look at the power of this new function!


Welcome to yet another random, rand(), whatever 😛

First off, music.

3 Doors Down

3 Doors Down
3 Doors Down

This band is pure awesomeness, rock awesomeness specifically!

Though their latest self-titled album had released a few months ago, I only got them this week and every track on it is splendid and not even one makes me skip to the next! The only other album that I’ve enjoyed so much would be Flipsyde’s We The People. Here’s their page if you are interested in trying them out.

However, there is always a favorite no matter how much you like all the tracks on an album and mine were It’s Not My Time and She Don’t Want The World.

Their older album, Away from the Sun was also good, with great tracks like When I’m Gone and Here Without You. Nice music!

Didn’t like the even-older album The Better Life though.

Off to Programming.


Ruby Programming Language
Ruby Programming Language

I messed around with Ruby a few days. It’s brilliant, and nearly as easy as Python is. But it didn’t fit me so well that I’d shift from Python to it. My prime reason to try it was for seeing how good Ruby On Rails development could be, my interest being sparked by the rave reviews its been getting. It sure is good, web-dev stuff but I’ll go ahead with Django finally.

But doing things in Ruby is sorta easier than in Python. Sort of. But I haven’t poked around much to be sure if it wins over Python or not. Ruby is definitely more Object-Oriented than Python, with every darn thing being an object. With a good editor, ruby files look great and are easy to read as well. Might explore more with it at Project Euler perhaps.

Now for some more KDE4 mixup.

KDE’s Dolphin is a boon for external-storage device users with its neat split screen feature and not to mention the tabbing power. I’d say this is overkill, giving both! And if that were not enough, there’s a button

KDEs Dolphin File Manager
KDE's Dolphin File Manager

that would launch the terminal below the window in another splitter pane. This power feels good to handle, too good. Ironically for me, its philosophy page says otherwise. Ark, the KDE’s default archive-extract/create program is just not good. It fails at basic tasks and stalls while extracting from split RAR files, but thanks to it am more comfortable with the unrar and tar command line programs now.

Am building Amarok 2 (Alpha 2 – 1.86) as I type this now. Will write about it in a later post, if I manage to get it built and running properly. KTouch is another nice application, for improving your touch-typing skills and am addicted to KBattleShip and KHangMan in my free time, for some educative-arcade fun.

Finally, about my life. (Hey who said all above is for people with no life?!)

Not much is happening at college except for some mild interest of mine rising up for IBM’s TGMC 2008, but I most probably won’t be doing any worthwhile thing in it, I don’t like being forced into Java and accompanying technologies from IBM. This Java thing can form another post actually, haha.

Implementing those OS Job-scheduling algorithms in a preemptive manner in C is a nice practice though the syllabus doesn’t clearly require it. And what is this whole Rational Rose thing, I never get it why designing the construct of a software project is easier this way than hard-coding it down from scratch, I find it too confusing drawing diagrams!

Saw some old Sonic the Hedgehog videos thats been doing some spikes lately, and also saw Hancock and The Dark Knight off which the latter was the most awesome movie ever! Its IMDb rating is justified IMO, with Heath Ledger’s death clearly contributing a lot to it.

Thats all for this syscall of random. *Urgh, goes back to the OS Concepts book*

GTK themes and other stuff

Found that GTK’s Aurora engine is pretty better at theming than any other engine.

You can look for the package gtk-engines-aurora in your distribution and install it. I loved the tooltips shading and other nice bluish things it had to offer, and also liked the dark theme that accompanies the package. Pretty neat stuff. :)

Back from mars, I haven’t been doing much lately, and have moved my residence. The downtime of internet due to that also rid me of my internet addiction strangely and I feel like cooking in the evenings. Yes, very scary!

And about scary, 11:37 doesn’t follow me as often as before but I do notice it every other day. But do notice the post’s time.

Watched Indiana Jones and The Kingdom of the Crystal Skull in the theaters and found it to be very bad. I did not like the alien touch given to it, not to mention the legend of the crystal skull. Previous movies (With the possible exception of temple of doom – but hey it had great action!)  had better artifacts more historically popular than the crystal skull and that’s why I like em more. This one was meh.

Iron Man was a total mechanical candy, it was sweeter than Robocop, much sweeter. The boss fight not counting, the movie satisfied my interests with just the scene where he first wears his Mark III suit. It was the smoothest thing I’ve ever seen on CGI. I’d watch it on the big screen repeatedly for that sequence alone. Heck, maybe get a HD copy of it too, when its out.

Finally, saw Forbidden Kingdom today. It was good too, if not great. But I do hope there’s no sequel to it. It ended well for once, I don’t wish a sequel to spoil it up. Well unless Jet Li decides to return somehow. The best moments in the movie were only when Jet and Jackie do their attacks against one another. Rest, including the vengeful girl and the monkey king, were sort of meh but did not spoil the fun much. Though there could have been lesser oriental decors and make up. Jackie ought to do a Splinter role in any Turtles movie, he would totally suit Splinter.

Now getting back to the interstellar space, the place where most of you never bother (Bother, not dare) to go, it feels good to have settled down to a distribution and even better to know its Gentoo, of all Linux variants there are. Sure, configuring it took around a week (Only and still my first try) but the system now no longer needs to update via those ISO methods, like Ubuntu or others need for a better experience. I’m rolling with it as it goes!

Beginning to respect cross-platform-ness, I have jumped down into the Qt/KDE river, though I still haven’t managed to get KDE 4.1 installed (Yeah Gentoo does have SOME bad points about it, but otherwise its the best for settling down upon, no version numbers nothing, you are what you’ve got.)

Programming with Qt is quite a pain for someone not used to Object Oriented stuff, but the stuff it can do does not make you give up and let go. Have started a blog on the same, and you will find the link here and content there when its ready enough.

Sorry am I getting too random here? I’ll continue over in another post then. 😉

And thats the second year that just went…

I know I rant a lot about my college, the studies that men do, and general stuff in that regard. But underneath all that hate I seem to spew around, I do actually love my course.

It does get tedious when some subject you have no real interest for gets in the way and you have to learn it no matter what but if seen clearly it does teach you something you never knew all your life. And am sure somewhere you would be able to apply the same, if not immediately, in your life.

I shouldn’t be speaking examples here cause cases are all relative to what people study but I’ll push on with it. I had no idea how or what JPEG compression worked or did until this semester. And when I did learn that and GIF, TIFF, MPEG and other stuff like it, I realized how important digital signals were and how much you can achieve by them. I had to remove the mental block that had grown out of my hatred towards any subject that considered a signal x(n) and derived seemingly useless equations out of it a.k.a Digital Signal Processing.

What I mean here is, once I got to know how large the application area of the subject I hated was, I felt shame I did that. But its not entirely my mentality at fault, its also the syllabus outline we have prescribed. The subjects hardly focus on the practical part of a paper, and by practical I do not mean laboratory or stuff like that, but real-world applications. At max all I see is a small 4-5 point block about “Used as”, “Used in” and etc. Never do they include an detailed explanation about at least one of the various interesting applications of the topic.

Not that it would make a difference to the majority who wish to graduate for work purposes alone but to those who actually have joined with an interest it would mean a lot.

Back to normal topics, (i.e. if you’re still reading this far), I’m loving this Gentoo Linux, it didn’t turn out to be hard as predicted at all. Of course I just did the stage3 install via the Handbook but I’ve compiled my own custom kernels and applications and it all seem so easy and nice to do. Well, so much for a normal topic huh?

Gonna watch Iron Man or Speed Racer within the week and both within the next. Just for summer’s sake of course. I really wish The Dark Knight released sooner, its too long the wait since Batman Begins to watch Bale take up the Wayne role again with his perfect straight face, heh.

Anyway, me off now for some stuff I must have done long ago. Got to catch up with the skilled world. 😛

Ice Age 3 – The Third Ice Age – Err …

I loved Ice Age (The Movie), the first part was very nice to watch with the humans involved and being a ‘real’ Ice Age themed movie.

Then came the second movie, Ice Age 2: The Meltdown, which was based on the melting of the ice. And Noah’s Ark. This one was way boring than its predecessor. Yet, the Scrat’s attempts to protect preserve and store his acorn never fails to amuse me. So’s gonna be the case for the upcoming Ice Age movie.

Say hello to Ice Age 3: Dawn of the Dinosaurs.

Yup its Dinos alright, and they are gonna meet the rest of the crew – A sabretooth, a Sloth Bear and a Mammoth. Not to mention the amazing Scrat.

The movie’s gonna come in July next year (2009)

Jump for the poster (Thanks to DezinerFolio)

Continue reading Ice Age 3 – The Third Ice Age – Err …