Why Python

by admin

Why Python? If it’s not 10 times better than other languages (except for the running speed), I won’t bother it.

Let me give you an example to show Python’s awesomeness and attractiveness.

Refer to this page for the problem. We are asked to compute the first ten digits of the sum of one-hundred 50-digit numbers:

37107287533902102798797998220837590246510135740250
46376937677490009712648124896970078050417018260538
74324986199524741059474233309513058123726617309629

Suppose you are using C/C++, you probably need to use array to handle each digit of these long numbers, since these languages don’t support such long integers. If you are using Python, things will be very easy.

Just copy/paste these numbers into a file. Edit it with Vim. Use Vim command:

:%s/$/,/g

to add a comma to the end of each line. Remove the last comma. Then insert “array = [” to the beginning of the first line and add a “]” to the end of the last line.

Add the following to another line:

print sum(array)

Save this file and run it with Python and here we are done.

Here you can see that Python can handle arbitrary large number, as long as you memory can hold it. This is just a tip of the iceberg of Python’s advantage. Use it and you will get more and more.