Top Uncommon Python Tricks You Should Learn

Top Uncommon Python Tricks You Should Learn

  • 2404

4 Uncommon Python Tricks You Should Learn .Note that in Python, everything has a truth value, including strings, numbers, and lists. Each type implements this in a different way.

1. Multiple Assignment

When you want to give several variables the same values, you will often see code that repeats the assignment statement for each variable.

It may look something like the following code, which uses one separate assignment per variable:

a = 1
b = 1
c = 1

In Python, we can simplify this, and assign to every variable at once.

a = b = c = 1
# a == 1
# b == 1
# c == 1

After doing this, every variable was assigned to the right-most value in the chain. Since it just takes the right-most value, we can also replace 1 with a variable.

val = 1
a = b = c = val
# a == 1
# b == 1
# c == 1

2. Sequence Unpacking

In Python, if we have a sequence, such as a list, we can directly assign the elements of that sequence to individual variables without having to use a loop or separately index each element of the sequence.

our_list = [1, 2, 3]
a, b, c = our_list
# a == 1
# b == 2
# c == 3

Here we see that each element in our_list is assigned to the corresponding variable. Note that this only works when we provide the correct number of variables on the left side — there must be one for each element in our_list.

Variable swapping

A nice consequence of this is that we can swap variables without using a temporary variable.

a, b = (1, 2)
# a == 1
# b == 2
a, b = b, a
# a == 2
# b == 1

Since b, a is equivalent to the tuple (b, a), we can unpack this sequence and re-assign it to a and b as we did above. By changing the tuple on the right side, we can re-assign these variables to any other set of values.

3. Extended Slicing

You may already know how to get a slice of a sequence with the our_list[begin:end] notation, which creates a sub-sequence of the original. But this can go even further with extended slicing, which provides a third argument that controls the step size.

The extended slice syntax is our_list[begin:end:step].

The step argument can be either positive or negative. A positive value will start from the beginning and skip ahead step elements at a time until it reaches the end.

Conversely, a negative value will start from the end and skip step elements at a time until it reaches the beginning.

Reversing a list

We can use the extended slice syntax to quickly reverse a list.

our_list = [1, 2, 3]
reversed_list = our_list[::-1]
# reversed_list == [3, 2, 1]

By providing a step size of -1, it starts from the last element in our_list and goes backward one element at a time until it reaches the beginning. This creates a list that is the reverse of the original list.

4. All and any

Python provides two unique built-in functions that allow us to check if all elements in an iterable are True, and if any element is True. These functions are aptly named all and any.

all_true = [True, True, 1, 'hello']
any_true = [0, False, True, '', []]
# all(all_true) == True
# any(all_true) == True
# all(any_true) == False
# any(any_true) == True

Note that in Python, everything has a truth value, including strings, numbers, and lists. Each type implements this in a different way.

For example, in integers, anything non-zero is True, and for strings and lists, anything non-empty is True.

I hope this helps. Thanks for reading!