6 Cool Python Tricks You Should Know | by Soner Yıldırım | Towards Data Science

Data science has experienced monumental growth in recent years. Python, being the most common programming language in data science domain, increased its popularity as well.
In this article, I will mention 6 Python tricks that I think are pretty cool. They will also make your life easier by providing practical ways of accomplishing certain tasks.
To become a good programmer, it is not enough just to write code to accomplish a given task. Your program should be efficient in terms of time and computational complexity. Furthermore, the code should be clean, easy to read and debug, and maintainable.
The tricks in this article will help you achieve the ultimate goal of writing clean and efficient code.
Slices are objects so they can be stored in variables. Some data structures allow for indexing and slicing such as lists, strings, and tuples.
We can use integers to specify the upper and lower bound of the slice or use a slice object.
s = slice(3,6)lst = [1, 3, 'a', 'b', 5, 11, 16]text = 'DataScience'tpl = (1,2,3,4,5,6,7)print(lst[s])['b', 5, 11]print(text[s])aScprint(tpl[s])(4, 5, 6)
The slice s represents a slice from the fourth element to the sixth element. We apply the same slice object to a list, string, and tuple.
We are likely to encounters cases where we need to swap the values of two variables. One way is to use an intermediate variable to temporarily hold values.
a = 'John'b = 'Emily'c = aa = bb = cprint(a)'Emily'print(b)'John'
This way is kind of tedious. Python provides a much better way for swapping.
a = 20b = 50a, b = b, aprint(a)50print(b)20
Consider we have a list of lists.
lst = [[2, 7], [7, 3], [3, 8], [8, 7], [9, 7], [4, 9]]
We can sort the list based on the first or second items of the inner lists by using the sort function with a lambda function.
lst.sort(key = lambda inner:inner[1])print(lst)[[7, 3], [2, 7], [8, 7], [9, 7], [3, 8], [4, 9]]
The list is sorted based on the second items. We can do the same with the first items just by changing the 1 to 0.
lst.sort(key = lambda inner:inner[0])print(lst)[[2, 7], [3, 8], [4, 9], [7, 3], [8, 7], [9, 7]]
Consider we have a function that multiplies the given numbers.
def mult(a, b, c):   return a * b * cmult(2, 3, 4)24
This function works fine if we need to multiply just three numbers. It must be given exactly three numbers. We can make the function more flexible by using argument unpacking.
def mult(*args):   result = 1   for i in args:      result *= i   return result
Now the mult function is able to multiply any number of values.
mult(2, 3)6mult(2, 3, 4)24mult(2, 3, 4, 5)120
Argument unpacking is very commonly used in Python. You must have seen *args and **kwargs if you read the documentation of a package or library.
Assume we have a function that returns a tuple of two values and we want to assign each value to a separate variable. One way is to use indexing as below:
tpl = (1, 2)x = tpl[0]y = tpl[1]print(x, y)1 2
There is a better option that allows us to do the same operation in one line.
x, y = tplprint(x, y)1 2
It can be extended to a tuple with more than 2 values or some other data structures such as lists or sets.
x, y, z = {1, 2, 3}print(x, y, z)1 2 3x, y, z = ['a', 'b', 'c']print(x, y, z)a b c
It is a common practice to add variables inside strings. F strings are by far the coolest way of doing it. To appreciate the f strings more, let’s first perform the operation with the format function.
name = 'John'age = 15print("{} is {} years old".format(name, age))John is 15 years old
We specify the variables that go inside the curly braces by using the format function at the end. F strings allow for specifying the variables inside the string.
print(f"{name} is {age} years old")John is 15 years old
F strings are easier to follow and type. Moreover, they make the code more readable.
We have covered 6 simple yet highly practical tricks. They cause small changes but small improvements add up. You will eventually be writing code that is more efficient, easier to read and debug, and more maintainable.
Thank you for reading. Please let me know if you have any feedback.