Member-only story

Merry Christmas using Python — Tree

Python Coding
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 alias np.
  • 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 from 7 up to (but not including) 16:
    x = [7, 8, 9, 10, 11, 12, 13, 14, 15]
  • y: Creates an array of integers starting from 1 up to (but not including) 18, with a step of 2:
    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 the x array. Now x[::-1] = [15, 14, 13, 12, 11, 10, 9, 8, 7].
  • np.column_stack((x[::-1], y)): Combines the reversed x and y 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] ]

--

--

Python Coding
Python Coding

Written by Python Coding

Learn python tips and tricks with code I Share your knowledge with us to help society. Python Quiz: https://www.clcoding.com/p/quiz-questions.html

No responses yet