Tuesday, August 8, 2017

zip in Python

In this post I am going to talk about ZIP. ZIP is a function that takes elements from multiple iterables and aggregates them into one where we basically share the index value. Let’s take the following example:
x = [1, 2, 3, 4, 5, 6]
y = [11, 22, 33, 44, 55, 66]
z = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
##Let’s go ahead and combine x, y. 
for a, b in zip(x, y):
    print(a, b)
"""
1 11
2 22
3 33
4 44
5 55
6 66
"""
##We can also do multiple elements.
for a, b, c in zip(x, y, z):
    print(a, b, c)
"""
1 11 a
2 22 b
3 33 c
4 44 d
5 55 e
6 66 f
"""
##zip(x, y, z) is a zip object. And we can iterate through the zip object. 
for i in zip(x, y, z):
    print(i)    
"""
(1, 11, 'a')
(2, 22, 'b')
(3, 33, 'c')
(4, 44, 'd')
(5, 55, 'e')
(6, 66, 'f')
"""
##We can also convert zip object to dictionary.
print(dict(zip(z, x)))
{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6}
##We can also combine zip into list comprehension
[(a, b, c) for a, b, c in zip(x, y, z)]
"""
[(1, 11, 'a'),
 (2, 22, 'b'),
 (3, 33, 'c'),
 (4, 44, 'd'),
 (5, 55, 'e'),
 (6, 66, 'f')]
"""

Monday, August 7, 2017

Transforming Code into Beautiful, Idiomatic Python

#Transforming Code in Beautiful, Idiomatic Python

# Example 1
#   - looping over a range of numbers

for i in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]:
    print(i**2)
    
#Better way 
for i in range(0, 10, 1):
    print(i**2)
    
    
# Example 2
#   - Looping over a collection
colors = ['red', 'green', 'blue', 'yellow']
for i in range(len(colors)):
    print(colors[i])

#Better way
for color in colors:
    print(color)
    
# looping backwards
colors = ['red', 'green', 'blue', 'yellow']
for i in range(len(colors)-1, -1, -1):
    print(colors[i])
    
#Better way
for color in reversed(colors):
    print(color)
    
# Example 3
#   - Looping over a collection and indicics
colors = ['red', 'green', 'blue', 'yellow']
for i in range(len(colors)):
    print(i, '==>', colors[i])
    
#Better way
for i, color in enumerate(colors):
    print(i, '==>', color) 
    
# Example 4
#   - Looping over two collections   
colors = ['red', 'green', 'blue', 'yellow']
months = ['jan', 'feb', 'mar', 'apr']

n = min(len(colors), len(months))
for i in range(0, n, 1):
    print(months[i], '==>', colors[i])
 
#Better way
for month, color in zip(months, colors):
    print(month, '==>', color)
    
# Example 5
#   - Looping sorted order
colors = ['red', 'green', 'blue', 'yellow']
for color in sorted(colors):
    print(color)

# sort in reverse way
for color in sorted(colors, reverse=True):
    print(color)

Pi Digit Generator

def make_pi(itr=1000):
    q, r, t, k, m, x = 1, 0, 1, 1, 3, 3
    for j in range(1000):
        if 4 * q + r - t < m * t:
            yield m
            q, r, t, k, m, x = 10*q, 10*(r-m*t), t, k, (10*(3*q+r))//t - 10*m, x
        else:
            q, r, t, k, m, x = q*k, (2*q+r)*x, t*x, k+1, (q*(7*k+2)+r*x)//(t*x), x+2


digits = make_pi(itr=10000)
pi_list = []
my_array = []

for i in make_pi():
    my_array.append(str(i))

my_array = my_array[0:1] + ['.'] + my_array[1:]
big_string = "".join(my_array)

print("Here is the Pi:\n{}".format(big_string ))

Here is the Pi:
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034
82534211706798214808651328230664709384460955058223172535940812848111745028410270193852110
55596446229489549303819644288109756659334461284756482337