Member-only story
Merry Christmas using Python — Tree
2 min readDec 23, 2024
import numpy as np
x = np.arange(7,16); y = np.arange(1,18,2)
z = np.column_stack((x[:: -1],y))
for i,j in z:
print(' '*i+'*'*j)
for r in range(3):
print(' '*13, ' || ')
print(' '*12, end = '\======/')
print('')
Code Explanation
Importing numpy
:
import numpy as np
- The
numpy
library is imported and given the aliasnp
. - This is commonly done for brevity when using
numpy
functions.
Creating arrays x
and y
:
x = np.arange(7, 16) y = np.arange(1, 18, 2)
x
: Creates an array of integers starting from7
up to (but not including)16
:x = [7, 8, 9, 10, 11, 12, 13, 14, 15]
y
: Creates an array of integers starting from1
up to (but not including)18
, with a step of2
:y = [1, 3, 5, 7, 9, 11, 13, 15, 17]
Combining x
and y
into z
:
z = np.column_stack((x[::-1], y))
x[::-1]
: Reverses thex
array. Nowx[::-1] = [15, 14, 13, 12, 11, 10, 9, 8, 7]
.np.column_stack((x[::-1], y))
: Combines the reversedx
andy
arrays into a 2D array (z
), column-wise:z = [ [15, 1], [14, 3], [13, 5], [12, 7], [11, 9], [10, 11], [ 9, 13], [ 8, 15], [ 7, 17] ]