Sunday, March 18, 2018

Python One Liners

Many code structures that take multiple lines in a language like Java can be written using only one line of Python code. One liners of code save tons of space. They also show examples of Python's concise and structured language as I described in the first blog post.

We have already discussed how brief language is a special feature of Python, such as one line conditional statements. In this post, I will list some more examples of how Python uses one line of code to do things that take multiple lines in a language such as Java.

1. Swapping numbers

It's the tricky question that's bound to be on an exam in your coding 101 class. In Java, swapping numbers requires something like the following code, where a temp variable is created to store one of the variables before swapping:



In Python, however, we can simply use one line of code, rather than three to accomplish the swap. This is called 'unpacking for swapping variables':







2. Duplicating strings

In a language like Java, to duplicated a string, one would have to write code in a loop to concatenate a string:





But in Python, we can "multiply" a string like we would a number, all in just one line of code.





3. Store list values in separate variables

In Python, we can assign values in a list to variables using only one line of code.





Doing this in Java would require a loop to go through each position in the array. I will not show this code in Java, but you can imagine that it would take many more lines of code than we used in Python.

4. Combine items in 2 separate lists

Items in two lists can be joined in their respective positions using one line of Python code. The following is an example. Note that in this example, we use a two-liner for loop to 'zip' the lists together.








Doing this in another language would require looping through each list and concatenating the strings position by position, which would certainly take more than two lines of code.

5. Negative Indexing

Another unique feature to Python is negative indexing. Python recognizes negative indexes of a lists, and interprets them by counting through the list backwards. This is a cool feature that can be coded in one line, and it is not a feature in languages like Java.





And now for a (Monty) Python one-liner:
"My brain hurts!"
- Monty Python's Flying Circus

Until next week!

Sources:
https://www.codementor.io/sumit12dec/python-tricks-for-beginners-du107t193
http://sahandsaba.com/thirty-python-language-features-and-tricks-you-may-not-know.html


4 comments:

  1. If one was to write a very long one liner, would readability be an issue? In that case, would writing multiple lines be preferred over the one liner?

    ReplyDelete
    Replies
    1. At a certain point, if your one liner was really long, I'm sure it would be a pain to scroll to see all the content. In that case you would want the content to flow over onto the next line. In Python, you can do this, so you could still write the "one-liner" code but let the text flow onto the next line with proper indentation for easier readability. Or for things like conditionals, you might want to use the traditional layout structure instead of a one-liner.

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. How beautifully efficient these one-liners are. Though it took me a second to realize that the = sign in the swap code separated two expressions. I thought the comma was the separator and wasn't quite understanding the swap.

    ReplyDelete