Member-only story

3 secret Python tricks you need to see

Manpreet Singh
3 min readAug 28, 2021

--

Welcome back! Python is an awesome programming language with a ton of capability, you can pretty much do anything with this language, so let’s talk about some secret Python tricks that you may not know about. Now, some of these tricks are a bit random, but you could implement most of these inside of your projects, with this long introduction out of the way, let’s get started!

Improve Your If Else’s

Starting off, one of the best ways to shorten the amount of lines within your Python program is to simply reformat your if-else statements, what does this mean? Let’s take a look at the following example:

if sam < 8:
print("sam")
elif sam == 10:
print("10")

There’s an easier way to write this out which takes up less space, here is an updated way to do this:

print("sam") if sam < 8 else print("10") if sam == 10

This is a very good way to decrease the amount of lines of your Python project.

Getting The Size Of Memory Used

Next up, one of my favorite little tricks with Python is checking the size of variables/datasets that are inside of our environment. We can use the getsizeof function to actually see the amount of bytes our specific attribute has, let’s take a look at…

--

--

Manpreet Singh
Manpreet Singh

No responses yet