Python is cool. Really cool. However, a lot of us started with a different programming language and although we picked up Python really easily, we still aren’t good at the pythonic way of doing things. This article introduces some tricks I’ve learnt over the years and practical examples for the same. Hope you enjoy it.
To indent code
To unindent code
Press Shift + Tab.
To comment or uncomment a bunch of code, select it and
Press Contrl + / or Command + / for Mac.
To put quotation marks around something, select it and then
Press Shift + ' or Shift + ".
Zip can be used to iterate over 2 lists together.
The best part about Python is that you can accomplish so much in so less code. Take list comprehensions for example. If you want to create a list of numbers in a certain range you can easily do it as follows:
You can also apply conditions on it very easily.
One really cool use case of list comprehensions is to convert a number to its individual digits. The trick is to convert the number to a string, iterate over the individual characters, convert them to int and store them in a list. We can either follow all the steps
or do it all together
This also reminds me of the map() function.
The * operator can be used to repeat strings. For example,
Now you probably don’t want to print “Python is cool” a whole lot of times but you should use it for something like this
The * operator can also be used to unpack iterables like lists.
You can also do something like
This operator is usually used when we have a function where we don’t know the number of arguments in advance. We use it with args and kwargs.
The arguments that we pass to the function are stored in *args. **kwargs will store named arguments or dictionaries.
Something else you can do with functions is create partial functions. What are they? Suppose we have a function to calculate simple interest. We can set default values for some parameters (from right to left).
However, we cannot set the default value of just p in this way**.**
We can do so using a partial function. In a partial function, we set default values for some arguments, left to right and then use that as a function. Let’s set a default value for just p.
Although partials work from left to right, we can also skip parameters in between by using named arguments.
Test driven development means you write tests and then you write code to pass those tests. You can write mini-tests in Python using assert. For example, you might want to make sure the shape of a certain object is what you expect it to be.
You can also write a custom error message after a comma. Writing these mini-tests will be super helpful in making sure parts of your code work as expected. It will also help you debug things efficiently.
We can use the yield keyword instead of return keyword in Python to create a generator. The advantage of using a generator is that is generates things on the fly and forgets them. This saves memory.
That will be it for this article. It’s a work in progress. I will keep adding more things as I come across them. If you have any suggestions that have made your life easier, drop them in the comments section below and we can all benefit from them.