6 Useful Python Tricks
Tags: python, beginners, programming, codenewbie
Published: 2022-01-12 17:25:40

Hey folks 👋, welcome back to another post!!
![welcome](https://media.giphy.com/media/XD9o33QG9BoMis7iM4/giphy.gif)

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.

```python
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
```

## 2. Arguments Packing

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

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

print(sum(1)) #1
print(sum(1,2)) #3
```

## 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

```python
name = 'Luffy'
age = '19'

print("{} is {} years old".format(name, age)) # Luffy is 19 years old
```

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? 

```python
print(f"{name} is {age} years old") # Luffy is 19 years old
```

## 4. Swapping variables

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

```python
a = 10
b = 20

a, b = b, a

print(a, b) # 20, 10
```

## 5. Reversing a list

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

```python
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]

```

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.

```python
print(sum(i for i in range(10))) # 45
```

## 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](https://media.giphy.com/media/1xucXbDnMIYkU/giphy.gif)

I will be back with more useful articles!! 

Do not hesitate to share your feedback. I am on Twitter [@arunkc97](https://twitter.com/arunkc97) and LinkedIn [arun-kc](https://www.linkedin.com/in/arun-kc/). Give a follow and support!

## 💜 Thank you for reading 💜
## 🌏 Like | Follow | Share 🌏