5 Cool Python Tricks
Tags: python
Published: 2020-06-19 03:58:55

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/6qv83eingbzaf3gqow6d.png)

Python is an awesome programming language, specially for Data Analysis and Machine Learning.
I've worked with Python for a couple of years and some of its syntax is very different from other languages I've worked before, like JAVA or C#.

So here are some of the most useful tricks I've learned so far with Python:

Trick #1 - The Python Interactive Shell:
Python provides a Shell which is used to execute a single Python command and get the result. Python Shell waits for the input command from the user. As soon as the user enters the command, it executes and displays the result.

For this example I'm going to use a function in a file as it follows:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/a1f3nigrx4h4sl4lmd1u.JPG)

I'm using VIM, as you can see this function only adds two numbers.
Typically I'd run this program like this:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/l2hvjzgeb4wswjp7qbti.JPG)

Now, there's an alternative way of running this program, with the Interactive Shell:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/5mmzm3621tj4yqv44w2g.JPG)

This is a very useful tool when coding in Python, it'll load the Python script or file and instead of closing it after running it, it will give you access to all the variables and functions inside.

Trick #2 - The Python Debugger:
While "python -i" will will run the entire Python script or file, then provide an interface into the environment, the PDB can stop this file before it's finished executing, so you can debug.

Take a look at this example:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/7lhx6qig069ykl37enye.png)

This time I'm adding 1 and 3, and then adding 1 and 'three', where 'three' is a string instead of an integer.
 So if I try to run this file as it is, it'll throw an error:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/55djg7uyts22zvsvhp0h.JPG)

In this example, I'm using a very small file (less than 5 lines of code), but when using larger scripts this could cause problems, and detect inconsistencies will be a nightmare. Fortunately there's the PDB:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/xl6n318r10ibz7r4qju5.png)

By importing the pdb and using the .set_trace() method before the line you're interested in debug, in the execution you'll get the PDB interface, where you print the current values of variables or functions, or go to the next line with the "n".

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/1cpb11z30qda6zf5evn4.JPG)

Trick #3 - The virtual Environment:

There's a reason why working with virtual environments is a best-practice in Python: Dependency Management.

One headache of Python is ironically one its best features, since Python has so many libraries for like every possible problem you could imagine, you'll soon find out that dealing with them is also a big challenge.

So dependency management with Python could be annoying, but one elegant way of dealing with it, is using virtual environments; the most popular one is Virtualenv:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/4vgh0pmnl63zinxqs01i.JPG)

To create a new virtual environment type "virtualenv" <name of the folder>:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/bq5a73sbny0yedqs18vi.JPG)

Then just activate the virtual environment:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/p1tm0lme0xv4ifpr83je.JPG)

You'll notice a prefix on the shell, that's the indicator that you're using a virtual environment.
Now you can install dependencies in isolation at need:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/t3dbb0bufj8e3zemt48a.JPG)

Trick #4 - List and Dict Comprehension: 

Let's say you have some data in a list, with data of results of the Myers-Briggs personality test, with the names and total of percentages represented as a dict.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/pu2hmj94fl4sgv0iijbs.png)

Often you'd like to pull out a single property from an object, to do that in conventional Python syntax you'll do something like this:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/m0ftoukm6k0pj9k434as.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/0xa364jju4z1e94ig0du.JPG)

That get the job done, but also required 4 additional lines of code.

There's a more elegant way of doing this, in a single line of code, using list-comprehension:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ob8da2gar1b3x1bud7kg.png)

We can do even more with list-comprehension, like adding conditional statements to the end:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/7cya7g0bk4hxfrgh9mot.png)

Now, for the dist-comprehension example: Here we have a list where 'name' is the key and 'percentage' is the value.
Again, to show the list you would have to loop the entire list, but instead using dist-comprehension you con do it in a single line of code:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/052ajbiyp2jcya9fdmkp.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/g3yxi6ou1s0kfctqho29.JPG)

Trick #5 - Lambda Functions λ:

In a nutshell a lambda function is a small anonymous function that can take any number of arguments, but can only have one expression.

Let me show you what I'm talking about in this example creating a function called "add" and another function "add2" that has a lambda:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/wdv5zgde0bh9ev29j1fo.png)

Running this script with the interactive shell, turns out these two are exactly the same:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/v7reij2ely9b8guk24t0.JPG)

In a practical scenario, if you have multi-line complex functions go for a "def" for sure. 
But if you're defining a function withing the interactive shell use lambda:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/tsnqwdf4v436ga6ziiia.JPG)

So that's it... hope you'll find it useful (!)