Python Convienences
Tags: python, productivity, shortercode
Published: 2020-07-30 19:35:25

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
```python
expr1 if condition else expr2
```
The above syntax is equivalent to the statement:
```python
condition ? expr1 : expr2
```
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:
```python
a,b = 4,5
if a > b:
   param = a
else:
   param = b
result = foo(param)
```
the objective of the code above can be achieved with the one below.
```python
a,b = 4,5
param = a if a > b else b
result = foo(param)
```
in fact, we can even make it shorter by not assigning the result to any variable rather passing the expression directly as an argument.
```python
a,b = 4,5
result = foo(a if a > b else b)
```
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.
```python
list1 = [1,2,3,4]
list2 = []
for num in list1:
    list2.append(num*num)
```
we can achieve the same result above in a shorter, cleaner way by making use of comprehension syntax for different conatainer types.
```python
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
```
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:
```python
values = 4,5,6,7
```
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.
```python
return x,y,z
```
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.
```python
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
```
### Simultaneous Assignments
a simultaneous assignment is a combination of packing and unpacking done simultaneously. 
```python
a,b,c = 1, 2, 3

```
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.
```python
a = 5
b = 6
temp = a # temp = 5
a = b # a is now 6
b = temp # b is now 5
```
the same code above can be achieved in a cleaner shorter way with just two lines of code as shown below.
```python
a,b = 5, 6
a,b = b, a

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