2026-02-23_this-is-python.html


001| #:name::This is Python! (part 7)
002| #:link::https://thehardcorecoder.com/2026/02/23/this-is-python-part-7/
003| #:date::2026-02-23
004| #:code::
005| message = "Hello, World! How are you?"
006| 
007| print(f"Message reads: '{message}'")
008| print()
009| 
010| #:code::
011| """This module is example_1.py"""
012| print("*** example_1: load")
013| 
014| def function_1 ():
015|     """The first function."""
016|     ...
017|     print('Function-1')
018|     ...
019| 
020| def function_2 ():
021|     """The second function."""
022|     ...
023|     print('Function-2')
024|     ...
025| 
026| def function_3 ():
027|     """The third function."""
028|     ...
029|     print('Function-3')
030|     ...
031| 
032| # Call the second function...
033| print("*** example_1: call function_2")
034| function_2()
035| 
036| print("*** example_1: exit")
037| 
038| #:code::
039| """This module is example_2.py"""
040| print("example_2: load")
041| 
042| print("example_2: import example_1")
043| import example_1 as ex
044| 
045| # Call the second function...
046| print("example_2: call function_2")
047| ex.function_2()
048| 
049| print("example_2: exit")
050| 
051| #:code::
052| """This module is an improved example_1.py"""
053| 
054| def function_1 ():
055|     """The first function."""
056|     ...
057|     print('Function-1')
058|     ...
059| 
060| def function_2 ():
061|     """The second function."""
062|     ...
063|     print('Function-2')
064|     ...
065| 
066| def function_3 ():
067|     """The third function."""
068|     ...
069|     print('Function-3')
070|     ...
071| 
072| 
073| # Test for executing vs loading...
074| if __name__ == '__main__':
075|     # Code here only runs when module is run...
076|     print('*** example_1: executing...')
077| 
078|     # Call the second function...
079|     print("*** example_1: call function_2")
080|     function_2()
081| 
082|     print("*** example_1: exit")
083| 
084| #:code::
085| # Basic module template...
086| 
087| def main ():
088|     '''Main function.'''
089|     ...
090|     ...
091|     ...
092| 
093| if __name__ == '__main__':
094|     print()
095|     main()
096|     print()
097| 
098| #:code::
099| """A module with some functions. (example_3.py)"""
100| from sys import argv
101| from math import sqrt, sin,radians
102| 
103| Ultimate = 42
104| 
105| Agents = (86, 99)
106| 
107| Colors = [
108|     None,
109|     'red', 'yellow', 'green',
110|     'cyan', 'blue', 'magenta',
111| ]
112| 
113| def hypotenuse (*args):
114|     '''Calculate a triangle's hypotenuse.'''
115|     x = float(args[0]) if 0 < len(args) else 1.0
116|     y = float(args[1]) if 1 < len(args) else 1.0
117|     return sqrt(pow(x,2) + pow(y,2))
118| 
119| def height (*args):
120|     '''Calculate height given a baseline and angle.'''
121|     base  = float(args[0]) if 0 < len(args) else 10.0
122|     angle = float(args[1]) if 1 < len(args) else 45.0
123|     return base * sin(radians(angle))
124| 
125| def colorname (*args):
126|     '''Get a color name from a index number.'''
127|     cx = int(args[0]) if 0 < len(args) else 0
128|     return Colors[cx]
129| 
130| 
131| def main (cmd, *args):
132|     '''Module's main function.'''
133| 
134|     if cmd == 'hypot': return hypotenuse(*args)
135|     if cmd == 'height': return height(*args)
136|     if cmd == 'clrname': return colorname(*args)
137| 
138| 
139| if __name__ == '__main__':
140|     print(f'running: {argv[0]}')
141| 
142|     command = argv[1] if 1 < len(argv) else ''
143|     arguments = argv[2:]
144| 
145|     rv = main(command, *arguments)
146|     print(f'{command} = {rv}')
147|     print()
148| 
149| #:code::
150| """Importing and using a module."""
151| 
152| # Import the entire module...
153| import example_3
154| 
155| print(f'{example_3.Ultimate = }')
156| 
157| print(f'{example_3.colorname(3) = }')
158| 
159| #:code::
160| """Importing and using a module."""
161| 
162| # Import entire module with an alias...
163| import example_3 as ex
164| 
165| print(f'{ex.Ultimate = }')
166| 
167| print(f'{ex.colorname(3) = }')
168| 
169| #:code::
170| """Importing and using a module."""
171| 
172| # Import only objects we'll use...
173| from example_3 import Ultimate, colorname
174| 
175| print(f'{Ultimate = }')
176| 
177| print(f'{colorname(3) = }')
178| 
179| #:code::
180| """Importing and using a module."""
181| 
182| # Import only objects we'll use with aliases...
183| from example_3 import Ultimate as Ult, colorname as cn
184| 
185| print(f'{Ult = }')
186| 
187| print(f'{cn(3) = }')
188| 
189| #:code::
190| """Importing and using a module."""
191| 
192| # Import all objects...
193| from example_3 import *
194| 
195| print(f'{Ultimate = }')
196| 
197| print(f'{colorname(3) = }')
198| 
199| #:end:code::
200|