Like many software developers, I have learned my share of software languages over the years. Two years ago, I wrote a post on LinkedIn where I identified 29 software languages I had learned in the 37 years prior; everything from Basic to Ruby to JavaScript and obscure languages like APL and Clipper. As all developers know, in the software engineering field it’s important to keep up with emerging programming languages, so even to this day, I’m learning new ones. Similarly, like many other developers, in my eagerness to quickly learn a new language, I start with similar and familiar code syntax based on programming languages I already know. Using familiar syntax from programming languages you’ve already worked with is initially easier and can jump-start your use of a new language, but there’s a price to pay for that. When doing so, you often avoid learning some of the subtleties and specific features unique to the new language you’re learning, and you may miss some of the differences between the new one and the current languages you know.

At Solution Street, we ask candidates interviewing for developer positions to write some code during the interview. Sometimes we will describe a candidate’s code after an interview as, “He writes Ruby like a Java developer.” This is just another way of saying that, if the candidate was writing in Ruby, he didn’t really make use of some of Ruby’s advantages. I am no different and I often will lazily rely on familiar syntax and control structures when I use a newly-learned language.

“Using familiar syntax from programming languages you’ve already worked with is initially easier and can jump-start your use of a new language, but there’s a price to pay for that.”

Having learned Python a few years ago, I have discovered some new tidbits and tricks that sometimes come in the form of different syntax that could be advantageous to a developer. I want to share some of them with you.

  1. List slicing – List processing is very powerful in Python and although there are many Python packages that enhance list processing, this core capability is very powerful to extract portions of a list. Typically, in other languages you might simply use an iterator to extract what you need, but this concise way is much easier:

Output (or run it)

olu
olution Street
Solu
olution St

  1. Operator chaining – Often, when writing code, you come across a conditional saying something like:

Specifying the condition slightly differently still works:

Python further allows you to take the above statement and simplify it. Some may say this is too terse, but after looking at this more, you may realize the original way is too verbose:

Output (or run it)

True

  1. Value swapping – Similar to above, there are more verbose ways of swapping values between variables, but Python’s terse way can be seen as either elegant or difficult to read:

Output (or run it)

(2, 1)
(2, 1)

  1. For else loops – In most languages, we are very familiar with for loops, but Python has this extra added benefit of an else conditional. Often within a loop, you are evaluating a condition and if that condition is true you typically perform some operation and leave the loop. However, you sometimes need to know that the condition was never met and perform some other operation. In most languages, you would do this manually via some additional boolean:

 

With for else loops, it’s more elegant with less code:

Output (or run it)

This is not a valid pizza.

  1. Function argument unpacking – Sometimes in functions/methods of other languages, you need to pass in a set of arguments, but your values are currently stored in an array or key/value pair object (dictionary). Python gives you the ability to pass in a structure and have it unpacked to satisfy the arguments:

Output (or run it)

x = 100
y = 42
x = 100
y = 42

  1. Special override called “missing” in dictionaries – How many times have you worked with a dictionary (object/key-value pair) and you needed the key to be initialized? Typically, in the course of your code, you check for null and then initialize it. While this is standard practice, Python has a way to override a special method __missing__ to deal with missing keys when you subclass the built-in dict:

Output (or run it)

{‘foo’: [1, 2]}

  1. Step argument in slice operators – Just a nifty way to walk through a list and skip elements:

Output (or run it)

Solution Street
tleeekrntaS fnforiuthutlroaS

  1. Enumerate a list with access to an index – Often, we need to iterate over a list of items but we also need the index. Some languages easily offer this option and some don’t. The simple, additional code method, is to maintain a separate counter while looping but that is extraneous with Python. Python’s enumerate function gives us an iterable where each element is a tuple that contains the index of the item and the original item value:

Output (or run it)

0 Python
1 Ruby
2 JavaScript
3 Java
4 C#
[(0, ‘Python’), (1, ‘Ruby’), (2, ‘JavaScript’), (3, ‘Java’), (4, ‘C#’)]

  1. List comprehensions – Although it may initially look different, Python allows for a concise and powerful way to create lists. The syntax is brackets containing an expression followed by a ‘for’ clause, then zero or more ‘for’ or ‘if’ clauses. These lists can be conditional or even include operation:

Output (or run it)

[0, 1, 2, 3, 4]
[0, 1, 2, 3, 4]
[‘e’, ‘o’, ‘i’, ‘i’, ‘a’, ‘e’]
[19.0, 28.5, 36.0, 90.0]

  1. Conditions in ternary operator – Frequently, in coding, you find yourself with the need to do a basic if/else condition and in most cases you write the simplistic but multi-line code block like this:

You can be more terse in your statement with Python:

Output (or run it)

does not qualify



Source link

Write A Comment