Saturday, 23 December 2023

Merry Christmas using Python 🧡

 


Code : 

from colorama import Fore

def heart_shape(msg="Merry Christmas"):

    lines = []

    for y in range(15, -15, -1):

        line = ""

        for x in range(-30, 30):

            f = ((x * 0.05) ** 2 + (y * 0.1) ** 2 - 1) ** 3 - (x * 0.05) ** 2 * (y * 0.1) ** 3

            line += msg[(x - y) % len(msg)] if f <= 0 else " "

        lines.append(line)

    print(Fore.RED+"\n".join(lines))

    print(Fore.GREEN+msg)

heart_shape()  # Call the function to create the heart

#clcoding.com


Explnation of the code in details :

This code generates a text-based heart shape using the Colorama library for colored output in the terminal. Here's a breakdown:

Imports:
from colorama import Fore
The code imports the Fore class from the Colorama library, which is used to set text color in the terminal.

Function Definition:

def heart_shape(msg="Merry Christmas"):
The code defines a function named heart_shape that takes an optional parameter msg with a default value of "Merry Christmas".

Creating the Heart Shape:

lines = []
for y in range(15, -15, -1):
    line = ""
    for x in range(-30, 30):
        f = ((x * 0.05) ** 2 + (y * 0.1) ** 2 - 1) ** 3 - (x * 0.05) ** 2 * (y * 0.1) ** 3
        line += msg[(x - y) % len(msg)] if f <= 0 else " "
    lines.append(line)
The nested loops iterate over y-coordinates and x-coordinates to create a heart shape. The mathematical expression within the inner loop defines the shape. If f is less than or equal to 0, it adds a character from the message; otherwise, it adds a space.

Printing the Heart Shape in Red:

print(Fore.RED + "\n".join(lines))
The heart shape is printed in red by concatenating the lines into a single string using "\n".join(lines) and using Fore.RED from Colorama.

Printing the Message in Green:

print(Fore.GREEN + msg)
The message is printed in green using Fore.GREEN from Colorama.

Function Invocation:

heart_shape()
The function is called without passing any arguments, so it uses the default message "Merry Christmas". The heart shape and message are printed with colored text in the terminal.

Note: The comment #clcoding.com at the end is a comment and doesn't affect the code's functionality. It seems to be a reference to a website.



0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (51) AI (34) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (173) C (77) C# (12) C++ (82) Course (67) Coursera (226) Cybersecurity (24) data management (11) Data Science (128) Data Strucures (8) Deep Learning (20) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (59) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (3) Pandas (4) PHP (20) Projects (29) Python (931) Python Coding Challenge (358) Python Quiz (22) Python Tips (2) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (3) Software (17) SQL (42) UX Research (1) web application (8) Web development (2) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses