DEV Community

Cover image for Python Convienences
James Ononiwu
James Ononiwu

Posted on

Python Convienences

There are several features of python which makes it more convienent for writng clean concise code. each of the features can be achieved with other python functionalities, however most times the features account for clear expression of the logic. let's look at them one after the other.

Conditional Expressions

expr1 if condition else expr2
Enter fullscreen mode Exit fullscreen mode

The above syntax is equivalent to the statement:

condition ? expr1 : expr2
Enter fullscreen mode Exit fullscreen mode

for those familiar with Java or C++. the compound expression evaluates to expr1, if the condition is True, else it evaluates to expr2.example usage are outlined below.
consider a goal of sending the maximum number as an argument to a function:

a,b = 4,5
if a > b:
   param = a
else:
   param = b
result = foo(param)
Enter fullscreen mode Exit fullscreen mode

the objective of the code above can be achieved with the one below.

a,b = 4,5
param = a if a > b else b
result = foo(param)
Enter fullscreen mode Exit fullscreen mode

in fact, we can even make it shorter by not assigning the result to any variable rather passing the expression directly as an argument.

a,b = 4,5
result = foo(a if a > b else b)
Enter fullscreen mode Exit fullscreen mode

sometimes, shorting of source code makes it easy to read and understand a cumbersome code. however, it is advised that conditional expression is used only when it improves the readability of the source code.

Comprehension Syntax

most times the task might be to produce one series of values based on the processing of another series. this can be achieved easily with comprehension syntax. there are different types of comprehension syntax according to the datatype involved. let's use the code below as an example of computing values from one list to another list.

list1 = [1,2,3,4]
list2 = []
for num in list1:
    list2.append(num*num)
Enter fullscreen mode Exit fullscreen mode

we can achieve the same result above in a shorter, cleaner way by making use of comprehension syntax for different conatainer types.

list2 = [ num*num for num in list1 ] #list comprehension
{ num*num for num in list1 } #set comprehension
( num*num for num in list1 ) #generator comprehension
{ num :num*num for num in list1 } #dictionary comprehension
Enter fullscreen mode Exit fullscreen mode

The generator syntax is particularly attractive when results do not need to be stored
in memory.

Packing and Unpacking of Sequences

packing and unpacking is used in tuples and other data types.
for example in the code below:

values = 4,5,6,7
Enter fullscreen mode Exit fullscreen mode

results in the identifier values being automatically assigned to the tuple (4,5,6,7). this behaviour is known as automatic packing of the tuple.
one of the uses is in returning multiple values from a function.

return x,y,z
Enter fullscreen mode Exit fullscreen mode

returns the tuple (x,y,z)

python also offers a way to unpack a tuple for instance if the values returned from a function foo is a tuple (x,y,z). this can be unpacked by assigning each value in the tuple to an identifier.

x,y,z = foo()

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

for x, y in [ (7, 2), (5, 8), (6, 4) ]: # x = 7, y = 2 | x = 5, y = 8 |... and so on
Enter fullscreen mode Exit fullscreen mode

Simultaneous Assignments

a simultaneous assignment is a combination of packing and unpacking done simultaneously.

a,b,c = 1, 2, 3

Enter fullscreen mode Exit fullscreen mode

good use of simultaneous assignment is in swapping values contained in variables. usually, to swap a value between two variables, a third variable temp is required.

a = 5
b = 6
temp = a # temp = 5
a = b # a is now 6
b = temp # b is now 5
Enter fullscreen mode Exit fullscreen mode

the same code above can be achieved in a cleaner shorter way with just two lines of code as shown below.

a,b = 5, 6
a,b = b, a

Enter fullscreen mode Exit fullscreen mode

We have seen the different ways to write better code while achieving the same goal. I hope this adds more value to your skill, thanks for reading.

Top comments (3)

Collapse
 
waylonwalker profile image
Waylon Walker

Great post, I use comprehensions and ternaries every day.

Packing/unpacking is such a tricky topic to wrap your head around. I made a post with my best effort here.

Collapse
 
jamesbright profile image
James Ononiwu

I just checked your post it's a great read.

Collapse
 
waylonwalker profile image
Waylon Walker

Thanks @jamesbright , much appreciated.