Advertisement
Python253

hello_python

Jun 5th, 2024 (edited)
457
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.11 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Filename: hello_python.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. Description:
  9.    - This script demonstrates 24 different ways to print "Hello, Python!" in Python, showcasing a variety of techniques from simple to advanced.
  10.    - Additionally, it includes an example of encoding and decoding the message using arbitrary ASCII transformations.
  11.    - The script highlights the versatility and flexibility of Python through multiple coding techniques.
  12.  
  13. Requirements:
  14.    - Python 3.x (minimum version 3.6)
  15.    - Required modules:
  16.        - os
  17.        - sys
  18.        - functools (Specifically the 'partial' function)
  19.  
  20. Functions:
  21.    - encode_char(char):
  22.        Encodes a character.
  23.    - decode_char(encoded_char):
  24.        Decodes a character.
  25.    - obscure_message(message):
  26.        Encodes an entire message.
  27.    - reveal_message(obscured_message):
  28.        Decodes an entire message.
  29.    - construct_message_part1():
  30.        Constructs the first part of the message.
  31.    - construct_message_part2():
  32.        Constructs the second part of the message.
  33.    - combine_message_parts(part1_func, part2_func):
  34.        Combines the message parts.
  35.    - print_obscured_message(message_func):
  36.        Prints the revealed message.
  37.  
  38. Usage:
  39.    - Run the script directly to see the various methods of printing "Hello, Python!".
  40.    - Call print_obscured_message(combine_message_parts) to see the encoded and decoded message.
  41.  
  42. Additional Notes:
  43.    - This script serves an educational purpose to demonstrate different Python techniques.
  44.    - The encoding and decoding functions use arbitrary ASCII transformations for illustration purposes and are not intended for actual encryption.
  45. """
  46.  
  47. # Essential Module Imports
  48. import os
  49. import sys
  50. import functools
  51.  
  52. # Specific import needed from functools module
  53. from functools import partial
  54.  
  55. # Define the language global variable as 'l'
  56. l = "Python"
  57.  
  58. # Header
  59. print(":: FUNCTION ::\t\t\t\t:: RESULTS ::\n")
  60. print("_" * 55)
  61.  
  62. # Basic print statement
  63. print("\n 1: Basic Print Statement:")
  64. print("\t\t\t\t\tHello, Python!")
  65.  
  66. # Using string concatenation
  67. print("\n 2: String Concatenation:")
  68. print("\t\t\t\t\tHello" + ", " + "Python!")
  69.  
  70. # Using string formatting (old style)
  71. print("\n 3: String Formatting (Old Style):")
  72. print("\t\t\t\t\t%s, %s!" % ("Hello", "Python"))
  73.  
  74. # Using str.format() method
  75. print("\n 4: Using 'str.format()' Method:")
  76. print("\t\t\t\t\t{}, {}!".format("Hello", "Python"))
  77.  
  78. # Using f-strings (Python 3.6+)
  79. print(f"\n 5: Using 'f-strings': {l}:")
  80. print(f"\t\t\t\t\tHello, {l}!")
  81.  
  82. # Using triple quotes for the string
  83. print("\n 6: Triple Quotes:")
  84. print("""\t\t\t\t\tHello, Python!""")
  85.  
  86. # Using a list and unpacking with * operator
  87. print("\n 7: List & Unpacking With * Operator:")
  88. print("\t\t\t\t\t", end="")
  89. print(*["Hello", "Python!"], sep=", ", end="")
  90. print()
  91.  
  92. # Using a tuple and unpacking with * operator
  93. print("\n 8: Tuple & Unpacking With * Operator:")
  94. print("\t\t\t\t\t", end="")
  95. print(*("Hello", "Python!"), sep=", ")
  96.  
  97. # Using a generator expression
  98. print("\n 9: Generator Expression:")
  99. print("\t\t\t\t\t", end="")
  100. print(*(word for word in ["Hello", "Python!"]), sep=", ")
  101.  
  102. # Using sys.stdout.write() (requires importing sys)
  103. print("\n10: Using 'sys.stdout.write()':")
  104. print("\t\t\t\t\t", end="")
  105. sys.stdout.write("Hello, Python!\n")
  106.  
  107. # Using __import__ to import sys
  108. print("\n11: Using '__import__' To Import 'sys':")
  109. print("\t\t\t\t\t", end="")
  110. __import__("sys").stdout.write("Hello, Python!\n")
  111.  
  112. # Using map with a lambda to combine the strings first
  113. print("\n12: Using 'map' With 'lambda':")
  114. print("\t\t\t\t\t", end="")
  115. print("".join(list(map(lambda x: x, ["Hello, ", "Python!"]))))
  116.  
  117. # Using functools.reduce (requires importing functools)
  118. print("\n13: Using 'functools.reduce':")
  119. print("\t\t\t\t\t", end="")
  120. print(functools.reduce(lambda x, y: x + y, ["Hello, ", "Python!"], ""))
  121.  
  122. # Using exec with a string
  123. print("\n14: Using 'exec' With A String:")
  124. print("\t\t\t\t\t", end="")
  125. exec('print("Hello, Python!")')
  126.  
  127. # Using eval with a string
  128. print("\n15: Using 'eval' With A String:")
  129. print("\t\t\t\t\t", end="")
  130. eval('print("Hello, Python!")')
  131.  
  132. # Using os.system (requires importing os)
  133. print("\n17: Using 'os.system':")
  134. print("\t\t\t\t\t", end="")
  135. os.system("echo Hello, Python!")
  136.  
  137. # Using print as a variable
  138. print("\n18: Using 'print' As A Variable:")
  139. print("\t\t\t\t\t", end="")
  140. _pr = print
  141. _pr("Hello, Python!")
  142.  
  143. # Using functools.partial (requires importing functools)
  144. print("\n19: Using 'functools.partial':")
  145. print("\t\t\t\t\t", end="")
  146. partial(print, "Hello, Python!")()
  147.  
  148. # Using a class with __call__ method
  149. print("\n20: Class With '__call__' Method:")
  150. print("\t\t\t\t\t", end="")
  151.  
  152. class Printer:
  153.     def __call__(self, message):
  154.         print(message)
  155.  
  156. Printer()("Hello, Python!")
  157.  
  158. # Using a decorator
  159. print("\n21: Using A Decorator:")
  160. print("\t\t\t\t\t", end="")
  161.  
  162. def print_decorator(func):
  163.     """
  164.    Decorator function to print "Hello, Python!".
  165.  
  166.    Args:
  167.    func (function): The function to be decorated.
  168.  
  169.    Returns:
  170.    function: The wrapped function.
  171.    """
  172.  
  173.     def wrapper():
  174.         func("Hello, Python!")
  175.  
  176.     return wrapper
  177.  
  178. @print_decorator
  179. def display(message):
  180.     """
  181.    Display function that prints the provided message.
  182.  
  183.    Args:
  184.    message (str): The message to be printed.
  185.    """
  186.     print(message)
  187.  
  188. display()
  189.  
  190. # Using list comprehension to print without newline
  191. print("\n22: List Comprehension:")
  192. print("\t\t\t\t\t", end="")
  193. print(*(word for word in ["Hello,", "Python!"]), end="")
  194. # To add a newline at the end add an empty print statement
  195. print("\n")
  196.  
  197. def encode_char(char):
  198.     """
  199.    Encodes a single character by transforming its ASCII value through an arbitrary
  200.    complex mathematical function to obscure its representation.
  201.  
  202.    Args:
  203.    char (str): A single character to be encoded.
  204.  
  205.    Returns:
  206.    str: Encoded character.
  207.    """
  208.     return chr(ord(char) + 1)  # Increment ASCII value by 1
  209.  
  210. def decode_char(encoded_char):
  211.     """
  212.    Decodes a single character that was previously encoded by reversing the
  213.    arbitrary complex mathematical function.
  214.  
  215.    Args:
  216.    encoded_char (str): A single encoded character to be decoded.
  217.  
  218.    Returns:
  219.    str: Decoded character.
  220.    """
  221.     return chr(ord(encoded_char) - 1)  # Decrement ASCII value by 1
  222.  
  223. def obscure_message(message):
  224.     """
  225.    Obscures an entire message by encoding each character using the encode_char function.
  226.  
  227.    Args:
  228.    message (str): The message to be obscured.
  229.  
  230.    Returns:
  231.    str: The fully obscured message.
  232.    """
  233.     return "".join(encode_char(char) for char in message)
  234.  
  235. def reveal_message(obscured_message):
  236.     """
  237.    Reveals an obscured message by decoding each character using the decode_char function.
  238.  
  239.    Args:
  240.    obscured_message (str): The obscured message to be revealed.
  241.  
  242.    Returns:
  243.    str: The fully revealed original message.
  244.    """
  245.     return "".join(decode_char(char) for char in obscured_message)
  246.  
  247. def construct_message_part1():
  248.     """
  249.    Constructs the first part of the message "Hello" through a series of unnecessary
  250.    variable assignments and transformations.
  251.  
  252.    Returns:
  253.    str: The first part of the message "Hello".
  254.    """
  255.     return "".join(encode_char(char) for char in "Hello")
  256.  
  257. def construct_message_part2():
  258.     """
  259.    Constructs the second part of the message "Python" through a series of unnecessary
  260.    variable assignments and transformations.
  261.  
  262.    Returns:
  263.    str: The second part of the message "Python".
  264.    """
  265.     return "".join(encode_char(char) for char in "Python")
  266.  
  267. def combine_message_parts(part1_func, part2_func):
  268.     """
  269.    Combines the two parts of the message with a space character in between,
  270.    obscuring the space as well.
  271.  
  272.    Args:
  273.    part1_func (function): Function that constructs the first part of the message.
  274.    part2_func (function): Function that constructs the second part of the message.
  275.  
  276.    Returns:
  277.    str: The combined obscured message.
  278.    """
  279.     space = " "
  280.     comma = encode_char(",")
  281.     exclamation = encode_char("!")
  282.     part1 = part1_func()
  283.     part2 = part2_func()
  284.     combined_obscured_message = part1 + comma + space + part2 + exclamation
  285.     return combined_obscured_message
  286.  
  287. def print_obscured_message(message_func):
  288.     """
  289.    Prints the revealed message by first obtaining the obscured message from the
  290.    message_func and then revealing it.
  291.  
  292.    Args:
  293.    message_func (function): Function that combines the obscured parts of the message.
  294.    """
  295.     obscured_message = message_func(construct_message_part1, construct_message_part2)
  296.     revealed_message = reveal_message(obscured_message)
  297.     print("_" * 55)
  298.     print("\n\t     :: Using Encoding/Decoding ::")
  299.     print(
  300.         f"\n\n23: Obscured Message:\t\t\t{obscured_message}\n\n24: Revealed Message:\t\t\t{revealed_message}"
  301.     )
  302.     print("_" * 55)
  303.  
  304. # Execute the function to print "Hello, Python!"
  305. print_obscured_message(combine_message_parts)
  306.  
  307. # Exit Program
  308. print("\n\n\t     Exiting Program...   GoodBye!\n")
  309.  
  310.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement