Tips & Tricks In Python- Part 2

Python has been adopted by many companies in the world because of its ease to use and it has even some tricks that make you be productive and even one line of python code can compensate for many lines in other languages such as Java. We’ve talked before in this series of python tips & tricks about a few of them and we will complete them in this tutorial.

1. Enter Multiple Values

In python, if you would like to take values from the user, you will need to use the input() function, which will automatically complete running the code after giving it one value. But you can make it accept multiple values at once. Let me show you an example:

a, b, c = input("Enter a Number: ").split()

print(a)
print(b)
print(c)

After running the previous code, you will need to enter the three values separated by a space to work correctly, and it will print them later on the screen using the print() function.

2. Remove Duplicate Values

Sometimes you have to remove duplicate values in a list, so most python developers will probably try to come up with a confusing code that contains the for loops and the if statement to remove these values. 

You can get creative developing this program to remove duplicates, but this is not friendly. I will show you two simple examples of doing this:

simple_list = [2, 67, 62, 3, 7, 7, 22, 44, 22, 9, 44]
print(simple_list)

new_simple_list = list(dict.fromkeys(simple_list))
print(new_simple_list)

You can see that it removes the duplicate values and works correctly. We’ve used the dict.fromkeys() that generate a dictionary of that list but removes all the same values. 

Then we convert it to a list again using the list() data type. There is also another more straightforward way of doing this. Let me show you an example of this:

simple_list = [2, 67, 62, 3, 7, 7, 22, 44, 22, 9, 44]
print(simple_list)

new_simple_list = list(set(simple_list))
print(new_simple_list)

We’ve used in this example the set() built-in python data type, which will remove any duplicate value of that list, and we convert the new generated set to a list using the list() data type. The sets are one of the four built-in python data types that can store multiple values in one variable, and it doesn’t accept any duplicate.

3. Big Nimbers Trick

Developers often work with large numbers like billions or even more. Developing an open-source project so other people can see & edit may not be easy to understand these numbers. Let me show you a how it would look like when you write big numbers:

a = 10000000000000
b = 10000000

print(a, b)

The first number is 10 trillion, which is too big, and the second one is 10 million, which both look confusing, and you can tell how much that number is. Let me show you the friendly & nice way for writing these numbers:

a = 10_000_000_000_000
b = 10_000_000

print(a, b)

We’ve put underscore “_” for every three zeros so you can understand what that number is. You can see that the second one looks much better and more readable than the previous one.

4. Revere a Word

Sometimes you need to reverse a string or word and you can perform this action using many different ways and the famous one will use the for loops to perform this action. Let me show you an example:

def rev(w):
    string = ""
    for i in w:
        string = i + string
    return string

rev("Python")

The code will work correctly but why you will need to use all of this code to make a simple action like reversing a word. There is a better and much simpler way for doing this:

name = "Python"[::-1]

print(name)

You can see that the second one is easier to understand than the first one. If you want to create a function for the second one to use it whenever you want in your code then you can use this code below:

def rev(word):
    word = word[::-1]
    return word

print(rev("Python"))

5. List Comprehension

We often use the list data type for loops for doing things like adding numbers to a new list or creating a new list from another list with some changes to name a few. Let me show you an example to understand:

even_numbers = []

for i in range(10):
    if i % 2 == 0:
        even_numbers.append(i)

print(even_numbers)

So we’ve created an empty list called even_numbers and we use the for loop and the range() function to create 10 numbers starting from 0 and the if statement tries to run the code only if the number is even. When the number is even, it will add it to the list using the .append() function.

The code above will only add the even numbers to the list and it worked but it contains many lines of codes and is also not the best way to do it. See this new example:

even_numbers = [i for i in range(10) if i % 2 == 0]

print(even_numbers)

It will work the same and whenever the condition is met, it will add the even number to the even_numbers list.

Conclusion

Thanks for reading! This article showed you many other tricks you can do in python to make your life better and be more productive just writing simple and shortcodes that will work the same as you were doing it before other than making you look like a professional developer. We will try to talk about more python tips & tricks in the next tutorials.

Scroll to Top