Christmas Tree using Python
Shwetabh Shekhar

Shwetabh Shekhar @shwetabh1

About: Lead Software Development Engineer based in Bengaluru. I love to create large-scale, distributed web/cloud applications with a particular passion for Go, Python, Java, JavaScript, Node.js, and AWS.

Location:
Bangalore
Joined:
Nov 2, 2020

Christmas Tree using Python

Publish Date: Dec 27 '20
21 6

Merry Christmas Dev community. Here is a simple Christmas tree using Python.

# Christmas Tree in Python
for i in range(10):
    print(''.join(['.'] * (9 - i) + ['^'] * (1 + i * 2) + ['.'] * (9 - i)))
else:
    print(''.join(['-'] * 8 + ['| |'] + ['-'] * 8))

'''
                .........^.........
                ........^^^........
                .......^^^^^.......
                ......^^^^^^^......
                .....^^^^^^^^^.....
                ....^^^^^^^^^^^....
                ...^^^^^^^^^^^^^...
                ..^^^^^^^^^^^^^^^..
                .^^^^^^^^^^^^^^^^^.
                ^^^^^^^^^^^^^^^^^^^
                --------| |--------
          Merry Christmas to all of you. @shwetabh1
'''
Enter fullscreen mode Exit fullscreen mode

There is no use of the else clause but I have used it deliberately, to expose it to devs who are yet not aware of it.

The else clause executes after the loop completes normally. It is useful in cases when we break from a loop. You can read more about it here.

Comments 6 total

  • Chris Greening
    Chris GreeningDec 27, 2020

    lol I like that you added the else clause just so others can learn 🙌

    • Shwetabh Shekhar
      Shwetabh ShekharDec 27, 2020

      Thanks. I also discovered it a bit late so wanted to share it with others.

      • Chris Greening
        Chris GreeningDec 27, 2020

        I didn't know about it until almost 2 years of learning Python, certainly a nifty hidden gem; the same goes for try-except secretly being try-except-else-finally lol

        • Shwetabh Shekhar
          Shwetabh ShekharDec 28, 2020

          haha didn't know about the else part in the try-except as well. thanks

  • Waylon Walker
    Waylon WalkerDec 27, 2020

    Super fun project

Add comment