Python’s Coolest Features (Basics)

Mehul Gala
6 min readNov 10, 2019

--

While the other programming languages were basking in the glory for being the eye candy of the mainstream software development industry and were widely used to solve complex problems, the scripting languages especially Python, were only looked at for specific use cases. They lingered in the shadows of their superior counterparts. Times have changed though, and so does their fortune. The latest popularity chart shows the two scripting languages are ruling today’s market by covering 50% of the total market share. Watch the upward curve of Python since the turn of the century.

Python is designed keeping code readability as its topmost priority. It is closer to the English language compared to the rest of them, in the sense that it uses a lot of English words instead of symbols to formulate its syntax. It uses strict indentation which can be a bit tricky to get used to initially, but as you move along, you realize how useful it is to not worry about the brackets. It results in making the code shorter.

Where python falls behind is the speed aspect. Some of the other languages have faster run-time. But it more than makes up for that by having a vast, rich standard library in its repertoire which can perform almost any task you can imagine in a software development process. Due to this comprehensiveness of its standard library, Python is often referred to as “Batteries included” language.

Most of the code written in Python appears like pseudocode because it uses a lot of English words. In fact, there is a joke in the market, that writing code in python is, probably as fast if not even faster, than writing the pseudo-code itself.

Coding in Python is a pleasing experience for many reasons. Its syntax is extremely developer-friendly, it has highly intuitive built-in data structures, a vast array of the standard library to make use of. But what I found really impressive is its coolest built-in features which are exceedingly useful to accomplish a lot by writing a considerably shorter code. A lot of this is built on the philosophy that a language should not pose any problem of its own, and let the developer focus on the real problem at hand.

Let’s explore some of those features starting from the very basic ones (operators, variables, loops, functions, etc) in this article, to the advanced ones (iterators, data structure comprehensions, packing & unpacking, etc) in another article.

Dynamic Typing

The variables are not statically typed meaning they are not declared with any ‘type’ upfront. Just name them, and assign values to them, and they will assume their type based on the values assigned. They will hold whatever you give them.

Arbitrary Integer Size

A lot of programming languages force you to choose the max size you need to store an integer upfront, also whether you need them as signed or unsigned (for positive and negative numbers). Any wrong decision can lead to severe consequences. The value of an integer in Python is not restricted by the number of bits and can expand to the limit of the available memory.

Swap two variables

Many programming languages have a basic program that asks them to swap two variables without using the third variable. There is a very popular meme on what Python has to say to that.

Reformed ternary operator

The syntax of the standard ternary operator is ingrained in our DNA, so now it seems intuitive to us, but it can take a good deal from any newbie to grasp to. Python makes it simple by making it seem like an English sentence.

This may seem counter-intuitive at first because we’re not used to it but if you read it a few repeated times you’ll see it is close to how we speak in English.

Return multiple variables from a function

I am sure, we must have come across this requirement many times during development, and we would have worked it out by having a few “Out” parameters in the function. Python has a “Just-chill” approach to solve this problem. You simply return them one after the other.

Cool, isn’t it? Although, I would like to tell you that it is an illusion. Under the hood, Python is actually accumulating all the returned variables in a tuple (one of its built-in data structures), and returning only one variable i.e. the Tuple. At the outer end, the variables are unpacked into individual variables with no tuple coming into the sight of the developer.

Wait, what? Take that for now. It will get clear when we cover packing and unpacking in the advanced section.

Negative indexing

We all know most of the programming languages use Zero-based index for an array or any iterable. To get the elements from the other end, we would usually start with the length minus one. Python allows us to use a negative index to make that traversal earlier.

Risk-free String Formatting

At the time of string formatting, it is a common mistake to pass the variable arguments in a different order than what is expected which leads to bizarre errors. Python allows us to specify named arguments at the time of formatting strings minimizing the chances of this error by a huge degree.

It is closely tied to Keyword arguments which we’ll cover in detail in the advanced section.

Else after Loops

We must have used else after the ‘if condition’ thousands of times but else after the loops? This is the first. Many times, we need to know whether the loop was successfully run till the end of the condition or something put a “break” to it. To accomplish this, we’d usually keep a flag that would be set when a break happened.

Python makes this flow extremely seamless by having an else statement after the loop. The else part will only get executed if the loop wasn’t broken midway.

Easy Array or String Slicing

To slice a part of an array or a string is a handy operation that is needed every now and then. Python gives us an easy way using bracket notation that is almost the same way we fetch individual elements.

You can slice the string the same way.

Variable hiding

We can use the underscore variable to hide the variable. This means that we are not planning to use this variable so we are not bothered to name them.

For example, here we are looping over 5 times to print something on the terminal, but we are not really using the loop index. So instead of naming the “i”, we can use the _ to denote we do not need this variable.

These were some of the coolest features which make the coding in Python a very efficient process. It increases our productivity by having to type less and achieve more, and we can concentrate on the problem at hand.

But, this feature list doesn’t stop here. These were only the basic ones. The real deals are the next ones, where we truly unlock the power of Python’s advanced features built on data structures and iterators. See you there.

--

--