Python tips and tricks: Make your code cleaner and more Pythonic

Python is one of the languages that people like to use the most. All programmers use it because it is short and easy to understand. So, here are a few things you can do to improve your Python programming skills.

Python is very popular right now, mostly because it is simple and easy to learn. You can use it for many different things, like data science and machine learning, web development, scripting, automation, etc.
In this video, we’ll show you eight quick refactoring tips that will make your code look much cleaner and more Pythonic.

1. Merge nested if statements

Let’s start with something simple. Instead of having nested if statements like this, just combine them into one.

2. Instead of a loop, use Any
Here, we want to see if a list has at least one positive item. The longer way to do this is to go through all Numbers in a loop. check the current number and break when the condition is met, but python already has a method for this: any function. We can just write that positives equal any n greater than zero for n in numbers. any returns true if any element of the iterable is true. This is much shorter and more pythonic than manually looping.

3. Choose from among the four
Choose from among the four Or while loops. A lot of the time, a variable is set inside a loop, but it doesn’t change. These things don’t need to be done, so just take them out of the loop and you’ll only have to make them once.

4. Remove inline variables
Remove inline variables that are only used once and are immediately returned. This is something we see a lot: a variable is defined at the end of a function, and one line later, it is immediately returned. If it’s clear what the function is doing, just return the result directly. This is shorter and gets rid of an unnecessary variable. The only time a variable is still useful is if the function’s purpose isn’t clear at first glance. In that case, you can give the last variable a meaningful name and use it as code that explains itself.

5. Use an if Expression instead of an if statement
Replace an if statement with an if expression. Instead of using the if else statement to set the value of a variable, you can just set this in one line with the if expression, like and so on. Some people still like the first choice, which is fine.

6. Add a guard clause
A safety net When you first look at this code, it’s hard to tell what’s going on because there are a lot of if-then-else statements and indents. However, if you look more closely, you might notice that the first if statement covers almost the whole function code. The corresponding else clause is at the end, where we just return false. This else clause can be moved to the beginning. In this case, the condition is if not is Instance, and then we return this is.

7. Move jobs closer to where they’ll be used
This is better than the last example, but it still takes a few seconds to figure out what’s going on. We want to check if we should wear a hat or not, and the logic is that if it’s raining, we always say true, and if it’s not raining, we say true if the head is stylish. One easy way to make this logic much easier to understand is to move the assignments closer to where they are used. Let’s get the weather right before using the Result, but in this case, it’s fine to leave the extra variable. The stylish name lets you know that you should say true if the head is stylish and false otherwise.

8. Make sequence checks easier
This is another thing that we see a lot. When you need to check if there are elements in a collection, like a list, you don’t have to write if the length of your list is greater than zero. Instead, you can just say if your list. This is the way that Pep 8 recommends, and it’s also called truth value testing. It is possible because empty sequences and collections are evaluated as false in Python. This can be used with strings, tuples, lists, dictionaries, and sets. As I said at the beginning, there is an extension for VS Code and pycharm that helps you find these refactoring patterns. This new feature is magic. Sorcery is a free add-on that is easy to install.

For more such content, visit:



India's Largest Tech Community | 4.2 Million+ Developers | Guinness World Record Winner | Limca Book of Records

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store

India's Largest Tech Community | 4.2 Million+ Developers | Guinness World Record Winner | Limca Book of Records