Skip to content

Commit db9e3bf

Browse files
authored
Merge pull request #150 from braboj/codex/analyze-a10-and-provide-python-examples
Add module examples
2 parents 886dc29 + 75c5d8b commit db9e3bf

File tree

7 files changed

+75
-0
lines changed

7 files changed

+75
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Using a custom module defined in the same directory
2+
# ------------------------------------------------------------------------------
3+
# A *module* is simply a Python file that defines variables, functions, or
4+
# classes. This example imports ``mymodule`` and calls its functions. Modules
5+
# help organize related code and can be reused across projects.
6+
7+
import mymodule
8+
9+
print(mymodule.greet('World'))
10+
print('Area of circle:', mymodule.area_circle(3))
11+
print('Module name:', mymodule.__name__)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Using built-in Python modules
2+
# ------------------------------------------------------------------------------
3+
# Python ships with a rich *standard library* of modules that you can use
4+
# without installing anything extra. This example demonstrates ``math`` and
5+
# ``random``.
6+
7+
import math
8+
import random
9+
10+
print('Square root of 16 is', math.sqrt(16))
11+
print('Value of pi is', math.pi)
12+
print('Random number between 1 and 6:', random.randint(1, 6))
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Importing from a package
2+
# ------------------------------------------------------------------------------
3+
# A *package* is a directory containing an ``__init__.py`` file and one or more
4+
# modules. Packages allow related modules to be grouped together. This example
5+
# imports the ``hello`` function from ``my_package`` defined in the same
6+
# directory.
7+
8+
from my_package import hello
9+
10+
hello()

examples/A10_modules/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Examples related to Python modules and packages
2+
# ------------------------------------------------------------------------------
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Example package with a single submodule
2+
# ------------------------------------------------------------------------------
3+
# Packages are directories containing an ``__init__.py`` file. This file is
4+
# executed when the package is imported and can expose objects from submodules.
5+
# Packages allow modules to be organized hierarchically.
6+
7+
from .submodule import hello
8+
9+
__all__ = ['hello']
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Submodule inside ``my_package``
2+
# ------------------------------------------------------------------------------
3+
# This module defines the ``hello`` function. ``__init__.py`` re-exports this
4+
# function so it can be imported directly from ``my_package``.
5+
6+
7+
def hello():
8+
print('Hello from submodule')

examples/A10_modules/mymodule.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Example custom module with variables and functions
2+
# ------------------------------------------------------------------------------
3+
# Modules are Python files that can define variables, functions and classes.
4+
# They allow code to be organized into reusable components. When a module is
5+
# imported, its top-level code executes once.
6+
7+
PI = 3.14159
8+
9+
10+
def greet(name):
11+
"""Return a greeting string."""
12+
return f"Hello, {name}"
13+
14+
15+
def area_circle(radius):
16+
"""Return the area of a circle with the given radius."""
17+
return PI * radius * radius
18+
19+
20+
if __name__ == '__main__':
21+
# Code executed only when running this module directly
22+
print(greet('module'))
23+
print('Area:', area_circle(2))

0 commit comments

Comments
 (0)