Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # Filename: hello_python.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- Description:
- - This script demonstrates 24 different ways to print "Hello, Python!" in Python, showcasing a variety of techniques from simple to advanced.
- - Additionally, it includes an example of encoding and decoding the message using arbitrary ASCII transformations.
- - The script highlights the versatility and flexibility of Python through multiple coding techniques.
- Requirements:
- - Python 3.x (minimum version 3.6)
- - Required modules:
- - os
- - sys
- - functools (Specifically the 'partial' function)
- Functions:
- - encode_char(char):
- Encodes a character.
- - decode_char(encoded_char):
- Decodes a character.
- - obscure_message(message):
- Encodes an entire message.
- - reveal_message(obscured_message):
- Decodes an entire message.
- - construct_message_part1():
- Constructs the first part of the message.
- - construct_message_part2():
- Constructs the second part of the message.
- - combine_message_parts(part1_func, part2_func):
- Combines the message parts.
- - print_obscured_message(message_func):
- Prints the revealed message.
- Usage:
- - Run the script directly to see the various methods of printing "Hello, Python!".
- - Call print_obscured_message(combine_message_parts) to see the encoded and decoded message.
- Additional Notes:
- - This script serves an educational purpose to demonstrate different Python techniques.
- - The encoding and decoding functions use arbitrary ASCII transformations for illustration purposes and are not intended for actual encryption.
- """
- # Essential Module Imports
- import os
- import sys
- import functools
- # Specific import needed from functools module
- from functools import partial
- # Define the language global variable as 'l'
- l = "Python"
- # Header
- print(":: FUNCTION ::\t\t\t\t:: RESULTS ::\n")
- print("_" * 55)
- # Basic print statement
- print("\n 1: Basic Print Statement:")
- print("\t\t\t\t\tHello, Python!")
- # Using string concatenation
- print("\n 2: String Concatenation:")
- print("\t\t\t\t\tHello" + ", " + "Python!")
- # Using string formatting (old style)
- print("\n 3: String Formatting (Old Style):")
- print("\t\t\t\t\t%s, %s!" % ("Hello", "Python"))
- # Using str.format() method
- print("\n 4: Using 'str.format()' Method:")
- print("\t\t\t\t\t{}, {}!".format("Hello", "Python"))
- # Using f-strings (Python 3.6+)
- print(f"\n 5: Using 'f-strings': {l}:")
- print(f"\t\t\t\t\tHello, {l}!")
- # Using triple quotes for the string
- print("\n 6: Triple Quotes:")
- print("""\t\t\t\t\tHello, Python!""")
- # Using a list and unpacking with * operator
- print("\n 7: List & Unpacking With * Operator:")
- print("\t\t\t\t\t", end="")
- print(*["Hello", "Python!"], sep=", ", end="")
- print()
- # Using a tuple and unpacking with * operator
- print("\n 8: Tuple & Unpacking With * Operator:")
- print("\t\t\t\t\t", end="")
- print(*("Hello", "Python!"), sep=", ")
- # Using a generator expression
- print("\n 9: Generator Expression:")
- print("\t\t\t\t\t", end="")
- print(*(word for word in ["Hello", "Python!"]), sep=", ")
- # Using sys.stdout.write() (requires importing sys)
- print("\n10: Using 'sys.stdout.write()':")
- print("\t\t\t\t\t", end="")
- sys.stdout.write("Hello, Python!\n")
- # Using __import__ to import sys
- print("\n11: Using '__import__' To Import 'sys':")
- print("\t\t\t\t\t", end="")
- __import__("sys").stdout.write("Hello, Python!\n")
- # Using map with a lambda to combine the strings first
- print("\n12: Using 'map' With 'lambda':")
- print("\t\t\t\t\t", end="")
- print("".join(list(map(lambda x: x, ["Hello, ", "Python!"]))))
- # Using functools.reduce (requires importing functools)
- print("\n13: Using 'functools.reduce':")
- print("\t\t\t\t\t", end="")
- print(functools.reduce(lambda x, y: x + y, ["Hello, ", "Python!"], ""))
- # Using exec with a string
- print("\n14: Using 'exec' With A String:")
- print("\t\t\t\t\t", end="")
- exec('print("Hello, Python!")')
- # Using eval with a string
- print("\n15: Using 'eval' With A String:")
- print("\t\t\t\t\t", end="")
- eval('print("Hello, Python!")')
- # Using os.system (requires importing os)
- print("\n17: Using 'os.system':")
- print("\t\t\t\t\t", end="")
- os.system("echo Hello, Python!")
- # Using print as a variable
- print("\n18: Using 'print' As A Variable:")
- print("\t\t\t\t\t", end="")
- _pr = print
- _pr("Hello, Python!")
- # Using functools.partial (requires importing functools)
- print("\n19: Using 'functools.partial':")
- print("\t\t\t\t\t", end="")
- partial(print, "Hello, Python!")()
- # Using a class with __call__ method
- print("\n20: Class With '__call__' Method:")
- print("\t\t\t\t\t", end="")
- class Printer:
- def __call__(self, message):
- print(message)
- Printer()("Hello, Python!")
- # Using a decorator
- print("\n21: Using A Decorator:")
- print("\t\t\t\t\t", end="")
- def print_decorator(func):
- """
- Decorator function to print "Hello, Python!".
- Args:
- func (function): The function to be decorated.
- Returns:
- function: The wrapped function.
- """
- def wrapper():
- func("Hello, Python!")
- return wrapper
- @print_decorator
- def display(message):
- """
- Display function that prints the provided message.
- Args:
- message (str): The message to be printed.
- """
- print(message)
- display()
- # Using list comprehension to print without newline
- print("\n22: List Comprehension:")
- print("\t\t\t\t\t", end="")
- print(*(word for word in ["Hello,", "Python!"]), end="")
- # To add a newline at the end add an empty print statement
- print("\n")
- def encode_char(char):
- """
- Encodes a single character by transforming its ASCII value through an arbitrary
- complex mathematical function to obscure its representation.
- Args:
- char (str): A single character to be encoded.
- Returns:
- str: Encoded character.
- """
- return chr(ord(char) + 1) # Increment ASCII value by 1
- def decode_char(encoded_char):
- """
- Decodes a single character that was previously encoded by reversing the
- arbitrary complex mathematical function.
- Args:
- encoded_char (str): A single encoded character to be decoded.
- Returns:
- str: Decoded character.
- """
- return chr(ord(encoded_char) - 1) # Decrement ASCII value by 1
- def obscure_message(message):
- """
- Obscures an entire message by encoding each character using the encode_char function.
- Args:
- message (str): The message to be obscured.
- Returns:
- str: The fully obscured message.
- """
- return "".join(encode_char(char) for char in message)
- def reveal_message(obscured_message):
- """
- Reveals an obscured message by decoding each character using the decode_char function.
- Args:
- obscured_message (str): The obscured message to be revealed.
- Returns:
- str: The fully revealed original message.
- """
- return "".join(decode_char(char) for char in obscured_message)
- def construct_message_part1():
- """
- Constructs the first part of the message "Hello" through a series of unnecessary
- variable assignments and transformations.
- Returns:
- str: The first part of the message "Hello".
- """
- return "".join(encode_char(char) for char in "Hello")
- def construct_message_part2():
- """
- Constructs the second part of the message "Python" through a series of unnecessary
- variable assignments and transformations.
- Returns:
- str: The second part of the message "Python".
- """
- return "".join(encode_char(char) for char in "Python")
- def combine_message_parts(part1_func, part2_func):
- """
- Combines the two parts of the message with a space character in between,
- obscuring the space as well.
- Args:
- part1_func (function): Function that constructs the first part of the message.
- part2_func (function): Function that constructs the second part of the message.
- Returns:
- str: The combined obscured message.
- """
- space = " "
- comma = encode_char(",")
- exclamation = encode_char("!")
- part1 = part1_func()
- part2 = part2_func()
- combined_obscured_message = part1 + comma + space + part2 + exclamation
- return combined_obscured_message
- def print_obscured_message(message_func):
- """
- Prints the revealed message by first obtaining the obscured message from the
- message_func and then revealing it.
- Args:
- message_func (function): Function that combines the obscured parts of the message.
- """
- obscured_message = message_func(construct_message_part1, construct_message_part2)
- revealed_message = reveal_message(obscured_message)
- print("_" * 55)
- print("\n\t :: Using Encoding/Decoding ::")
- print(
- f"\n\n23: Obscured Message:\t\t\t{obscured_message}\n\n24: Revealed Message:\t\t\t{revealed_message}"
- )
- print("_" * 55)
- # Execute the function to print "Hello, Python!"
- print_obscured_message(combine_message_parts)
- # Exit Program
- print("\n\n\t Exiting Program... GoodBye!\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement