DEV Community

Cover image for 6 Useful Python Tricks
Arun K C
Arun K C

Posted on

6 Useful Python Tricks

Hey folks πŸ‘‹, welcome back to another post!!
welcome

Let me introduce myself to those who don't know me, I'm Arun, a Data Engineer and Web3 fanatic. And for the ones who know me, thank you so much for the support πŸ™Œ and love πŸ’œ

In this article, I will be sharing some of the tips and tricks I usually use while coding in Python. As a developer, we should be writing efficient code that's clean, easy to read and debug, and maintainable.

The tricks that I will be sharing in this article will help you achieve the ultimate goal of writing clean and efficient code. So without any ado let's jump into it!!

1. Executing string as a code

exec() function is used for the dynamic execution of Python code. It can take a block of code containing Python statements like loops, class, function/method definitions and even try/except block.

This function doesn’t return anything but you can reuse the variables used in the string as shown in the example below.

code_as_string = '''
a = 1
b = 2
return_value = a + b
'''

exec(code_as_string)
print(return_value) #using return_value variable that's defined inside the string
Enter fullscreen mode Exit fullscreen mode

2. Arguments Packing

Argument unpacking is very commonly used in Python. You must have seen *args and **kwargs in some functions.

def sum(*args):
   result = 1
   for i in args:
      sum += i
   return result

print(sum(1)) #1
print(sum(1,2)) #3
Enter fullscreen mode Exit fullscreen mode

3. F String

It is a common practice to add variables inside strings. F strings are by far the coolest way of doing it.

Let's first see how we will be performing the operation with the usual format function

name = 'Luffy'
age = '19'

print("{} is {} years old".format(name, age)) # Luffy is 19 years old
Enter fullscreen mode Exit fullscreen mode

We specify the values that go inside the curly braces by using the format function. Now F string allow us to specify the variables inside the string itself. Cool isn't it?

print(f"{name} is {age} years old") # Luffy is 19 years old
Enter fullscreen mode Exit fullscreen mode

4. Swapping variables

Python have an easier way for swapping without using any temporary variable.

a = 10
b = 20

a, b = b, a

print(a, b) # 20, 10
Enter fullscreen mode Exit fullscreen mode

5. Reversing a list

We can easily reverse a list using reverse() function or slicing technique

list1 = [1, 2, 3, 4]
list2 = [5, 6, 7, 8]

list1.reverse()
print(list1) # [4, 3, 2, 1]

list3 = list2[::-1]
print(list3) # [8, 7, 6, 5]

Enter fullscreen mode Exit fullscreen mode

One thing to keep in mind is that reverse() will reverse the original list itself, unlike the slicing technique.

6. Using generator inside a function

We can use generators directly inside a function to write shorter and cleaner code.

print(sum(i for i in range(10))) # 45
Enter fullscreen mode Exit fullscreen mode

Conclusion

So now we have covered 6 simple yet highly practical tricks that you can use. At a glance these are small changes, but small improvements added up always gives magical results. You will eventually be writing code that is more efficient, easier to read and debug, and more maintainable as you find more practical tricks like these.

I hope you enjoyed this article. If you did show some love πŸ’œ by liking πŸ‘, and sharing πŸ”ƒ this article in your socials.

bye bye

I will be back with more useful articles!!

Do not hesitate to share your feedback. I am on Twitter @arunkc97 and LinkedIn arun-kc. Give a follow and support!

πŸ’œ Thank you for reading πŸ’œ

🌏 Like | Follow | Share 🌏

Top comments (0)