Top N Useful Python tips&tricks

Volkan Yurtseven
5 min readMay 6, 2022
Photo by Alejandro Piñero Amerio on Unsplash

I love coding so much; it is a passion for me. And i love using time-saver stuff when coding. I’ve been into Python since 2018 and from then on still learning terrific tips, which i’ll relay some of them to you here. Let’s cut it short and start.

Note: I didn’t want to include famous tips&tricks as you might have already seen them(I presume you are an experienced pythoner). However, some of those i include here might still be acquainted to you.

  • Snippet part in nbextensions

If you haven’t heard of jupyter nbextensions, please leave here and don’t come back again(just joking :) ). Anyway, I’m sure you are making use of it, but how about the snippets part? And how about your own snippets?

When you get to Snippets menu, custom json box will be waiting for you. Just put your own code there and voila. Surely, you need to open a new notebook for the codes to appear. (By the way, i prepare my json code in VS Code as it is super easy to do it there)

After you are done, it is available under the Snippets menu in Jupyter.

My whole snippets can be found here, sure you can download and use them.

  • Magics and custom magics

Of course, yo do know about the magics, i skip that part. But the chances are, you’re not aware that you can create yours, like below.

Let’s use this one, which is especially useful while working in a virtual environment. There are some others and you can find them here.

  • Package(Library)>Module>Class>Class members
    When importing things, I see even some instructors use the terms incorrectly. You can import a package directly(pandas), you can import a module(from sklearn import model_selection), you can import a class(from sklearn.linear_models import LinearRegression), you can import a function/method(from sklearn.model_selection import train_test_split)
    Things starting with a capital are Classes, not modules nor functions. For instance, LinearRegression is a class.
    In Spyder or any other advanced IDE such as VS Code, you can make a distinction between them by their shapes and colors.
First one is a method, and second one is a property.

Or in pandas,

First two are methods, whereas the last one is a Class.

One more thing about properties. If a property ends with “_” (especially in sklearn), it means it is available after you fit the model. For example: LinearRegression’s coef_ property.

  • Your own utility package

It is weird not to see this habit that frequent. The main idea behind the (functional) programming is to create reusable functions. This is a must, not a nice-to-have habit. So you must have your own frequently used functions file, which i call Utility package. I call it a package,because you can easily make them a package.

- create a folder under "site-packages". Let’s call it myutility
- create an empty __init__.py in it. (You can put "#pass" in it just in case)
- put your utility function file(s) in that folder
- that's all

You can now import it just like any other package.

from myutility import machinelearning as ml

And you can find mine here.

  • Extension methods

I use the extension methods in c# a lot, but there is no built-in method for this in python. Luckily with forbidden_fruit you can create extension methods for any type/class. For instance, following method is an extension for list type. With this, one can get the longest inner list in an outer list.

def getLongestInnerList(self):
longest=sorted(self, key=lambda x:len(x),reverse=True)[0]
index=self.index(longest)
return longest,len(longest),index
curse(list, "getLongestInnerList", getLongestInnerList)

We can use it like this:

As you see, it returns longest inner list, its length and its index.

You can find my whole extensions here.

  • semicolon at the end

Normally when you plot something it is highly you see a lot of babble before the chart.

But if you put “;” at the end, you get rid of all those jibber-jabber

  • startup codes

You can put any code that you want to run when jupyter starts, in “C:\Users\your_username\.ipython\profile_default\startup”. For example my extension methods and magic codes are here. And the good news is you don’t have to keep a copy or try to maintain the synchronization between the original one and the one here. Just create a symbolic link as follows.

on windows cmd prompt>
mklink C:\Users\volka\.ipython\profile_default\startup\mymagcis_j.py C:\Users\volka\AppData\Local\Programs\Python\Python38\Lib\site-packages\mypyext\mymagics.py

This way, i don’t need to import my magics and extension at every notebook, as they are always available.

--

--

Volkan Yurtseven

Once self-taught-Data Science Enthusiast,now graduate one