Every Smart Python Programmer Should Know These

Top 20 Tricks in Python That Can Make You a Smart Python Programmer

Nickson Joram
Analytics Vidhya
7 min readOct 12, 2021

--

What makes the difference between a good programmer and a smart programmer? Obviously, the way how the programmer performs. In this article, we are going to see some tricks which we can use in Python. These can make us look smarter than a good Python Programmer.

Photo by Kevin Canlas on Unsplash
  1. Reversing a String

We’ll use the straightforward concepts of string slicing and negative indexing. We know that Python can have a negative index, so we just slice it and don’t include any initial and final values, just a range of -1, which means it will start from the last index and go back to the first, thus reversing it.

Trust me, you can’t do this coolest way in any other language.

2. _ Operator

This returns the last output.

3. Numerous inputs from the user at once

The usual way is getting the input separately like this

So how to make it smarter?

The split() function is useful for obtaining multiple inputs from the user. It divides the given input using the separator specified. If no separator is specified, any white space is considered a separator.

4. The Walrus(:=) Operator

One of Python’s most recent additions is the Walrus operators. Python 3.8 is the most recent version to include it. It is an assignment interpretation that allows assignments to take place directly within the interpretation.

The usual way is

In this example, we declare a list and then declare a variable ’n’ to assign the value of the list’s length.

By employing Walrus operators,

We declare and assign the value at the same time in this case. That is the Walrus operator’s power.

5. List comprehension

In Python, list comprehension is a graceful way to define and create lists. Lists, like Mathematical statements, can be created in a single line. The syntax of list comprehension is simpler to understand. It’s a clever way to work with lists.

Let’s print square values of odd numbers in a range.

The normal method

Using List Comprehension

We’ve brought loops and the conditions in one single line and this is called List Comprehension.

6. Fibonacci series

That is the best-loved coding problem at any university in the world. It is resolved through recursion and dynamic programming approaches and other programming languages take a numerous number of codes. And once again, I have a one-line solution for you. Throw this code at your lecturer to astound him or her.

Don’t blame me for ruining your 20+ lines of code in Java or C++ or any other.

7. Removing duplicates from a List

Lists may contain duplicate elements. However, there are times when we don’t want duplicate elements in the list. Let’s take a look at how that can be accomplished.

Class is set. If you want to keep it as a list try this.

8. Manipulating Tuples

A tuple is a collection that is both ordered and immutable. When we say that tuples are ordered, we indicate that the items are in a specific order that will not change. Tuples are immutable, which means we can’t change, add, or remove items after they’ve been created.

But we can change a tuple. How? Check the below codes.

You can call list(tuple_a) also, the overall intention is to convert the tuple to a list and modify it before converting back as a tuple.

9. Lambda Function

This is one of the anonymous functions because these functions do not have names. These are extensively used in Data Science, Machine Learning, Django backends, and other areas. Let’s look at an example of adding two numbers.

Normal function,

Lambda Function

10. Palindrome

I’m sure you’ve encountered this question before. It is one of the university’s most popular problems. Have you ever considered doing something with just one line of code? Look at this.

11. Multi-args Function

It’s one of Python’s nicest tricks. Let’s say you’re writing a function and you’re not sure how many arguments will be passed in from the user. So, how do we declare the function’s parameters? Here’s an illustration of a Multi-args function that will add any number of values passed by the user.

12. Swapping numbers

When it comes to data structures and algorithms, swapping is a crucial concept. Let’s take a look at how we can swap numbers in Python.

In the normal method, we’ll need to create a temporary variable to temporarily store a value so that the other one becomes empty and we can swap the values.

Let me now demonstrate a simple Python trick for swapping numbers.

13. Concatenating Strings

Let’s see how we can create a string from a list of characters.

14. Using ZIP with lists

15. Converting lists into a dictionary

This is one of the most useful tricks when working on real-world Django and Machine Learning projects. As before, use the zip() function, but this time call it from the dictionary constructor.

16. Print list elements in any order

If you really need to print the values of a list in various combinations, you can assign the list to a sequence of variables and decide which order to print the list in programmatically.

17. Print a string n times

18. Merging two dictionaries

19. Mapping function

20. Shutting down a computer

Let’s see how we can shut down a computer with a single line of code. We will use the OS module for this. It is one of the most important Python modules, with numerous other functions.

CAUTION: BEFORE TRYING THIS, APPRECIATE MY WORK IF YOU LIKE AS YOUR MACHINE GOING TO BE SHUT DOWN!

That’s it.

Is that all about?

Don’t stay apart. Just do more practice and that has to be in an appropriate way. Never forget, Perfect practice makes a man perfect!

Hope this can help. Share your thoughts too.

--

--

Analytics Vidhya
Analytics Vidhya

Published in Analytics Vidhya

Analytics Vidhya is a community of Generative AI and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Nickson Joram
Nickson Joram

Written by Nickson Joram

MSc | UK | Ex - Virtusan | Learner

No responses yet