How to be fancy with Python

How to be fancy with Python

  • 352

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.

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.

1. Useful keyboard shortcuts

To indent code Press Tab.

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 + ".

2. Zip

Zip can be used to iterate over 2 lists together.

zip

3. List comprehensions

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:

list

You can also apply conditions on it very easily.

easily

Practical example:

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

the steps

or do it all together

together

This also reminds me of the map() function.

map

4. Using * operator

The * operator can be used to repeat strings. For example,

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

python is cool

The * operator can also be used to unpack iterables like lists.

unpack

You can also do something like

also

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.

operator

The arguments that we pass to the function are stored in *args. **kwargs will store named arguments or dictionaries.

5. Partials

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).

partials

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.

value

Although partials work from left to right, we can also skip parameters in between by using named arguments.

skip

6. Asserts

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.

asserts

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.

7. Generators

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.

generators

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.

~happy learning.