Important notice about this site

I graduated from the University of Texas at Austin in December 2020. This blog is archived and no updates will be made to it.

Perks of Python

May 14, 2019

I've had the great pleasure of learning quite a few programming languages over the past several years. I started with PHP in 8th grade. (I maintain that it is a necessary evil for my job, but that is a discussion for later.) Then, I learned Java in 9th grade in my AP Computer Science class, like most computer science students do in the United States. When I changed schools in 11th grade, I took two semesters of introductory programming in C++. Along the way, I've picked up JavaScript on my own. These few languages cover a great variety of applications. However, something has always been missing in the mix: Python.

Why do I know all of these languages but never bothered to learn Python?

To start off with, I'm not a big fan of Python's syntax. Astute observers may notice that all of the languages I mentioned in the prior paragraph are certainly in the C family in terms of syntax. Python's syntax is a significant departure from the bread and butter of C syntax to which I was accustomed, and I wasn't comfortable with this, to be honest. It prevented me from appreciating Python and therefore prevented me from exploring it too. Furthermore, everything that Python is used for can be done just as well by other languages. I hurled excuses at each potential application of Python. Web server? Don't use Flask, just use Express.js instead. Statistics? Don't use Python, just use R. General-purpose programming? Not even a question, Java or C++ is the way to go.

However, over the past year, I've come to realize that I need to step away from my comfort zone and start to learn Python. This semester, I was taking a Competitive Programming class, and we had the option to submit our assignments in either C++, Java, or Python. Ironically, I went from using Java (which is slow) to trying out Python (which is even slower) instead of moving to C++. The reason I made this change is because I got tired of the bloat that Java syntax had. (C++ syntax would not have been better.) Every time I have to write Scanner sc = new Scanner(System.in);, a little bit of me dies inside.

Thus, I became determined to finally learn Python. The syntax may not be my cup of tea, but I could recognize that there were benefits to using Python's syntax over other languages' syntax for the same constructs. For instance, .charAt() and .substring() are two very common String methods in Java that would be much better served if there were some special shortcut syntax for them. Thankfully, Python offers just that: splicing. varname.charAt(i) becomes varname[i] and varname.substring(0, j) becomes varname[:j]. Quite refreshing!

Where does this come handy? There are often simple actions that we want to achieve in programming, but they can be quite verbose to write out. For instance, if I were given an array of strings and asked to find the shortest string and return its length, I would think to myself "this is a really easy thing to ask for". Unfortunately, it's not really as simple to write out in Java:

String[] strs = new String[]{'string1', 'string2', 'string3', ...};
int minLen = Integer.MAX_VALUE;
for ( int i = 0; i < strs.length; i++ ) {
    if ( strs[i].length() < minLen ) {
        minLen = strs[i].length();
    }
}
// result stored in minLen

However, the same thing is extremely easy to accomplish in Python using list comprehensions. These are basically embedded loops that do a simple function to generate/modify a loop.

Here's what I did: I used a list comprehension to get the length of each string in the list, and then used the built-in min() function in Python to return the minimum.

strs = ['string1', 'string2', 'string3', ...]

minLen = min([len(x) for x in strs])
# result stored in minLen

I admit, this might not be space efficient, but the elegance of the syntax to convey the same meaning as the Java code I just wrote cannot be denied.

Another part of Python I really like is assigning multiple variables at once, or multiple assignment, which is accomplished through tuples. This is also possible in PHP, but let's be honest, why would I use PHP just for multiple assignment benefits?

For instance, this code in Java:

int a = -3;
int b = 25;
int c = 47;

would be this in Python:

a, b, c = -3, 25, 47

This came in handy in Competitive Programming whenever I needed to load in multiple variables on one line:

n, j, k = input().split(' ')

If we wanted to convert each input from a string to an integer, using list comprehensions comes in handy. The key to what I'm about to do lies in the fact that Python uses tuples to represent multiple assignment. In the below code sample, the input is split by spaces into a list. Then, each input is converted into an int and stored into a list. Finally, this list is converted into a tuple. The element at index 0 is stored in n, the element at index 1 is stored in j, and the element at index 2 is stored in k.

n, j, k = tuple([int(x) for x in input().split(' ')])

I started freshman year recognizing Python as something that exists that I dislike, but now I see why it might be at least somewhat useful. Python is slowly wooing me over with things like list comprehensions and multiple assignment. I hope to share what I find next and incorporate them into my programming repetoire.

Back to blog

Back to top