The Frameworks

The Frameworks

Share this post

The Frameworks
The Frameworks
Crafting Christmas Trees with Code: A Festive Guide for All
Copy link
Facebook
Email
Notes
More
User's avatar
Discover more from The Frameworks
Stories and insights on engineering, data, privacy, and the human side of tech.
Already have an account? Sign in

Crafting Christmas Trees with Code: A Festive Guide for All

As the holiday season approaches, why not sprinkle some festive cheer into your coding sessions? Let’s learn how to craft a Christmas tree in Python and, for the adventurous, in Rust! Merry Christmas!

Johnny Unar's avatar
Johnny Unar
Dec 23, 2024
1

Share this post

The Frameworks
The Frameworks
Crafting Christmas Trees with Code: A Festive Guide for All
Copy link
Facebook
Email
Notes
More
Share

Level 1: Beginner Python - A Simple Tree

This tree is perfect for those starting their coding journey. It uses basic loops and string manipulation to draw a simple Christmas tree.

height = 5  # Number of rows for the tree

for i in range(1, height + 1):
    # The number of spaces decreases as the level increases,
    # while the number of stars grows symmetrically.
    print(" " * (height - i) + "*" * (2 * i - 1))

print(" " * (height - 1) + "|")  # Tree trunk

Overview

  • The outer loop determines the height of the tree.

  • Spaces align the tree centrally, and stars form the tree shape.

  • The trunk is added at the end.

Expected Output

    *
   ***
  *****
 *******
*********
    |

Level 2: Intermediate Python - Decorated Tree

Add some holiday sparkle with decorations and random colors using Python’s random module.

import random

def decorated_tree(height):
    for i in range(1, height + 1):
        row = " " * (height - i)
        for j in range(2 * i - 1):
            row += random.choice(["*", "o", "x"])
        print(row)
    print(" " * (height - 1) + "|")  # Tree trunk

decorated_tree(8)

Overview

  • Decorations (stars *, ornaments o, and baubles x) are placed randomly.

  • The random.choice function gives each part of the tree a unique look.

  • You can specify the tree’s height using the height argument.

Expected Output (Example)

       *
      *x*
     *o*o*
    *x*o*o*
   *x*o*o*o*
  *o*o*x*o*o*
 *x*o*x*o*o*x*
*o*o*o*x*o*x*x*
       |

Level 3: Advanced Python - Fully Dynamic Tree

Take it up a notch with a tree that allows customization and dynamic decoration placement.

import random

def colorful_tree(height):
    decorations = [
        "\033[31mo", "\033[33mx", "\033[32m*", "\033[34m@"]  # ANSI escape codes for colors
    
    for i in range(1, height + 1):
        row = " " * (height - i)
        for j in range(2 * i - 1):
            row += random.choice(decorations)
        print(row + "\033[0m")  # Reset color after each row
    print(" " * (height - 1) + "|")  # Tree trunk

colorful_tree(10)

Overview

  • ANSI escape codes are used to add colors directly without external libraries. If you would like to use one, try colorama.

  • \033[0m ensures the color resets after each row.

Expected Output (Example):

Pro Level: Rust - A Sturdy Evergreen

For those ready to branch out, here’s how you can code a blazingly fast tree in Rust.

use rand::Rng;

fn decorated_tree(height: usize) {
    let decorations = ["*", "o", "x", "@"];
    let mut rng = rand::thread_rng();

    for i in 1..=height {
        let spaces = " ".repeat(height - i);
        let mut row = String::new();

        for _ in 0..(2 * i - 1) {
            let deco = decorations[rng.gen_range(0..decorations.len())];
            row.push_str(deco);
        }
        println!("{}{}", spaces, row);
    }
    println!("{}|", " ".repeat(height - 1));
}

fn main() {
    let height = 8;
    decorated_tree(height);
}

Overview

  • Rust’s strict typing ensures your tree is robust.

  • The rand crate handles random decoration generation.

  • Strings and loops create a balanced and well-aligned tree.

Expected Output (Example):

       *
      *@*
     *o*@*
    *@*x*@*
   *@*o*@*x*
  *@*x*@*o*@*
 *@*o*@*o*@*x*
*@*x*@*o*@*x*@*
       |

This holiday season, celebrate coding and creativity by crafting your digital Christmas tree. Maybe take it to another level by crafting something in real life? 👀

Happy coding and Happy Holidays! 🎄


Subscribe to The Frameworks

By Johnny Unar · Launched 5 months ago
Stories and insights on engineering, data, privacy, and the human side of tech.
Viktorie's avatar
1 Like
1

Share this post

The Frameworks
The Frameworks
Crafting Christmas Trees with Code: A Festive Guide for All
Copy link
Facebook
Email
Notes
More
Share

Discussion about this post

User's avatar
We Are Standing On The Edge (Let’s not f*ck this up)
This is a letter to all the folks who need to hear this, to all the people who feel lost and overpowered by the algorithms thinking for them, for us.
Dec 15, 2024 • 
Johnny Unar
3

Share this post

The Frameworks
The Frameworks
We Are Standing On The Edge (Let’s not f*ck this up)
Copy link
Facebook
Email
Notes
More
Český Tarzan: Přirozený pohyb, biohacking a síla mysli
Vítej u mého historicky prvního a s trochou štěstí snad ne posledního rozhovoru na této platformě.
Feb 22 • 
Johnny Unar
1

Share this post

Copy link
Facebook
Email
Notes
More
48:00
Why Coding Is Even More Relevant For You In The AI Era: A Story
I have been in the software engineering space for quite some time now and this letter is for anyone who thinks there is no point even trying.
Dec 29, 2024 • 
Johnny Unar
1

Share this post

The Frameworks
The Frameworks
Why Coding Is Even More Relevant For You In The AI Era: A Story
Copy link
Facebook
Email
Notes
More

Ready for more?

© 2025 Jan Unar
Privacy ∙ Terms ∙ Collection notice
Start writingGet the app
Substack is the home for great culture

Share

Copy link
Facebook
Email
Notes
More

Create your profile

User's avatar

Only paid subscribers can comment on this post

Already a paid subscriber? Sign in

Check your email

For your security, we need to re-authenticate you.

Click the link we sent to , or click here to sign in.